我第一次尝试用一个新的应用程序使用rspec3。我定义了两个简单的模型:
class Match < ActiveRecord::Base
end
class Article < ActiveRecord::Base
has_many :matches
end当我从控制台访问这些模型时,我可以创建关联并按预期将它们存储在DB中。但是,当我试图运行一个规范来测试这一点时,它似乎不起作用:
require 'spec_helper'
describe Article do
it { is_expected.to have_many(:matches) }
# it { should have_many(:matches) }
end我尝试同时运行测试的isexpected版本和should版本,但在这两种情况下,我都得到了以下错误:
> ./bin/rspec spec/models/article_spec.rb
Warning: Running `gem pristine --all` to regenerate your installed gemspecs (and deleting then reinstalling your bundle if you use bundle --path) will improve the startup performance of Spring.
F
Failures:
1) Article should have many :matches
Failure/Error: it { is_expected.to have_many :matches }
expected #<Article:0x007fa14c276f38> to respond to `has_many?`
# ./spec/models/article_spec.rb:4:in `block (2 levels) in <top (required)>'
# -e:1:in `<main>'
Finished in 0.09484 seconds (files took 0.63853 seconds to load)
1 example, 1 failure
Failed examples:
rspec ./spec/models/article_spec.rb:4 # Article should have many :matches
Randomized with seed 24636下面是Gemfile.lock中的相关依赖项:
rspec-core (3.0.4)
rspec-support (~> 3.0.0)
rspec-expectations (3.0.4)
rspec-support (~> 3.0.0)
rspec-mocks (3.0.4)
rspec-support (~> 3.0.0)
rspec-rails (3.0.2)
rspec-core (~> 3.0.0)
rspec-expectations (~> 3.0.0)
rspec-mocks (~> 3.0.0)
rspec-support (~> 3.0.0)
rspec-support (3.0.4)
spring-commands-rspec (1.0.2)
rspec-rails (~> 3.0.0)
spring-commands-rspec
...
shoulda-matchers (2.7.0)
shoulda-matchers看起来应该很直截了当。我是不是做错了什么导致我犯了这个错误?
发布于 2014-10-02 18:57:38
这是由于运行在安装Spring时创建的binstub:
./bin/rspec spec/models/article_spec.rb显然,这与运行RSpec安装的可执行文件的方式不同,这只是:
rspec spec/models/article_spec.rb发布于 2015-03-03 20:35:09
我不知道根本原因是什么,但我只是遇到了同样的问题-- bin/rspec和rspec。我设法让它为rspec工作,但没有为bin/rspec工作。
我必须将require 'shoulda/matchers'添加到规范文件中,还必须添加rails_helper.rb才能通过测试。它通过了一次,之后我可以从spec文件中删除它,然后测试就通过了。奇怪和不满意的解决办法。
https://stackoverflow.com/questions/26166851
复制相似问题