Why do I get an undefined method 'have' error when running Rspec?

Tintin81 picture Tintin81 · Dec 5, 2013 · Viewed 10.9k times · Source

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

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.

Thanks for any pointers.

Answer

Peter Alfvin picture Peter Alfvin · Dec 6, 2013

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.