我想用新的app第一次使用rspec3。我已經定義了兩個簡單的模型:使用rspec3測試has_many與shoulda-matchers
class Match < ActiveRecord::Base
end
class Article < ActiveRecord::Base
has_many :matches
end
當我從控制檯訪問這些模型,我可以創建協會,並將它們存儲在數據庫中的預期。但是,當我嘗試運行規範來測試這一點,似乎並沒有工作:
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
現在看來似乎應該是相當直截了當。我是否做錯了那導致我得到這個錯誤?
有趣的想法,但不幸的是,當我這樣做時,我會得到同樣的確切錯誤。同樣值得注意的是,對於rspec2,我從來不需要明確地定義主體,以便能夠以這種方式使用關聯匹配器。 – 2014-10-02 19:49:55
你能發佈錯誤嗎? – zetetic 2014-10-02 19:55:29
哎喲,你是對的,我忘了應該如何配合工作。我將刪除此響應,但是可以添加Rails和RSpec的版本嗎?我的代碼在Rails 4.1.5和RSpec 3.1.5上工作。 – zetetic 2014-10-02 20:27:48