为什么运行Rspec时会出现未定义的方法'have'错误?

时间:2022-08-22 22:34:58

I recently upgraded to Rails 4 and everything works fine except for my Rspec tests.

我最近升级到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

When running these tests I get this error:

运行这些测试时,我收到此错误:

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)>'

Can anybody tell me what I am missing here?

谁能告诉我这里缺少什么?

I googled it already but nothing came up. The have method is actually fairly standard in Rspec and I can't see why it shouldn't work.

我已经google了,但没有出现。在Rspec中,have方法实际上是相当标准的,我不明白为什么它不应该工作。

Thanks for any pointers.

谢谢你的任何指示。

3 个解决方案

#1


38  

The have family of matchers was deprecated in RSpec 2.99 and has been moved to a separate rspec-collection_matchers gem as of RSpec 3.0. This is discussed in http://myronmars.to/n/dev-blog/2013/11/rspec-2-99-and-3-0-betas-have-been-released, which also gives the suggested approach to migrating to 3.0. Specifically, it recommends installing/using RSpec 2.99 in order to see the deprecation messages associated with items that were removed/moved in 3.0.

有一系列匹配器在RSpec 2.99中被弃用,并且自RSpec 3.0起已被移至单独的rspec-collection_matchers gem。这在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中删除/移动的项目相关的弃用消息。

#2


1  

OK, got it.

好的,我知道了。

I had the wrong version number in my Gemfile.

我的Gemfile中有错误的版本号。

Before:

之前:

gem 'rspec-rails', '~> 3.0.0.beta'

After:

后:

gem 'rspec-rails'

#3


1  

In the latest versions of rspec "have" being deprecated, but you still can use it via rspec-collection_matchers gem.

在最新版本的rspec中,“have”已被弃用,但你仍然可以通过rspec-collection_matchers gem使用它。

# Gemfile
...
gem 'rspec-collection_matchers', group: :test
...

# spec/spec_helper.rb
...
require 'rspec/collection_matchers'
....

#1


38  

The have family of matchers was deprecated in RSpec 2.99 and has been moved to a separate rspec-collection_matchers gem as of RSpec 3.0. This is discussed in http://myronmars.to/n/dev-blog/2013/11/rspec-2-99-and-3-0-betas-have-been-released, which also gives the suggested approach to migrating to 3.0. Specifically, it recommends installing/using RSpec 2.99 in order to see the deprecation messages associated with items that were removed/moved in 3.0.

有一系列匹配器在RSpec 2.99中被弃用,并且自RSpec 3.0起已被移至单独的rspec-collection_matchers gem。这在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中删除/移动的项目相关的弃用消息。

#2


1  

OK, got it.

好的,我知道了。

I had the wrong version number in my Gemfile.

我的Gemfile中有错误的版本号。

Before:

之前:

gem 'rspec-rails', '~> 3.0.0.beta'

After:

后:

gem 'rspec-rails'

#3


1  

In the latest versions of rspec "have" being deprecated, but you still can use it via rspec-collection_matchers gem.

在最新版本的rspec中,“have”已被弃用,但你仍然可以通过rspec-collection_matchers gem使用它。

# Gemfile
...
gem 'rspec-collection_matchers', group: :test
...

# spec/spec_helper.rb
...
require 'rspec/collection_matchers'
....