Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Upgrade to rspec 3 cause error when using should have(1).error_on

Since I updated my Gemfile and moved to rspec 3, in many tests, I'm getting a error for: way:

it "should reject attribute that are too short" do
      short = "a" * 3
      hash = @attr.merge(:details => short)
      Deal.new(hash).should have(1).error_on(:details)
    end

I'm getting this error:

Failure/Error: Deal.new(hash).should have(1).error_on(:details)
     NoMethodError:
       undefined method `have' for #<RSpec::ExampleGroups::Deal_2::TestsOnDealsModelsValidations>

I read I should now be using "expect" instead of should but here with have(1).error_on, how should I write it to comply with rspec 3?

I tried the following but it still does not work:

it "should reject attribute that are too short" do
      short = "a" * 3
      hash = @attr.merge(:details => short)
      expect(Deal.new(hash).error_on(:details).size).to eq(1)
    end
like image 883
Mathieu Avatar asked Jun 07 '14 10:06

Mathieu


2 Answers

I have replaced the likes of

Deal.new(hash).should have(1).error_on(:details)

with

deal = Deal.new(hash)
expect(deal.valid?).to be_falsey
expect(deal.errors[:details].size).to eq(1)

The first expectation with valid? is necessary as it initializes the errors list.

like image 135
Eero Avatar answered Oct 16 '22 09:10

Eero


have and other similar matchers have been moved out of rspec core and into another gem, rspec-collection-matchers.

I recommend following the upgrade path from rspec 2 -> 3 as detailed in the rspec docs: https://relishapp.com/rspec/docs/upgrade

  • Upgrade to rspec 2.99
  • Run your test suite
  • Fix deprecation warnings
  • Upgrade to rspec 3.

If you had done this you would have received a deprecation error with your code that would have also told you what to do to fix it.

like image 8
sevenseacat Avatar answered Oct 16 '22 07:10

sevenseacat