我最近升级到Rails 4,除了我的Rspec测试外,一切正常。
require 'spec_helper'
describe Invoice do
before :each do
@user = FactoryGirl.create(:activated_user)
person = FactoryGirl.create(:person, :user => @user, :company => nil)
@project = FactoryGirl.create(:project, :user => @user, :person_ids => [person.id], :invoice_recipient_id => person.id)
end
it "has a valid factory" do
expect(FactoryGirl.build(:invoice, :project => @project, :user => @user)).to be_valid
end
it "is invalid without a number" do
expect(FactoryGirl.build(:invoice, :project => @project, :user => @user, :number => nil)).to have(1).errors_on(:number)
end
end
运行这些测试时出现此错误:
Failure/Error: expect(FactoryGirl.build(:invoice, :project => @project, :user => @user, :number => nil)).to have(1).errors_on(:number)
NoMethodError:
undefined method `have' for #<RSpec::ExampleGroups::Invoice_2:0x009ge29360d910>
# ./spec/models/invoice_spec.rb:16:in `block (2 levels) in <top (required)>'
有人可以告诉我这里缺少什么吗?
我已经用谷歌搜索过但没有出现过。have
方法在Rspec中实际上是相当标准的,我不明白为什么它不起作用。
感谢您的任何指示。
答案 0 :(得分:43)
have
系列匹配器已在RSpec 2.99中弃用,并且自RSpec 3.0起已移至单独的rspec-collection_matchers宝石。这在http://myronmars.to/n/dev-blog/2013/11/rspec-2-99-and-3-0-betas-have-been-released中讨论,它还提供了迁移到3.0的建议方法。具体来说,它建议安装/使用RSpec 2.99,以查看与在3.0中删除/移动的项目相关的弃用消息。
答案 1 :(得分:3)
在最新版本的rspec中,“have”已被弃用,但你仍然可以通过Story Board gem使用它。
this.signUpForm = new FormGroup({
'username': new FormControl(null, [Validators.required, Validators.minLength(6), Validators.maxLength(15)]),
'email': new FormControl(null, [Validators.required, Validators.email, Validators.minLength(5)]),
'password': new FormControl(null, [Validators.required]),
'confirmedPassword': new FormControl(null, [Validators.required])
}, this.pwdMatchValidator);
答案 2 :(得分:1)
好的,明白了。
我的Gemfile中的版本号错了。
在:
gem 'rspec-rails', '~> 3.0.0.beta'
后:
gem 'rspec-rails'