Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to fix my RSpec syntax for version 2.99?

I recently upgraded my Rails 4 application from RSpec 2.X to 2.99 and despite having run Transpec already, some of my tests are still failing.

require 'spec_helper'

describe Invoice, :type => :model do

  before :each do
    @user = FactoryGirl.create(:user)
    @invoice = FactoryGirl.create(:invoice, :user => @user)
  end

  it "is not open" do
    expect {
      FactoryGirl.create(:payment, :invoice => @invoice, :amount => 100)  
    }.to change{@invoice.reload.open?}.from(true).to(false)
  end

  it "is open" do
    expect {
      FactoryGirl.create(:payment, :invoice => @invoice, :amount => 99.99)  
    }.to_not change{@invoice.reload.open?}.to(false)
  end

end

The first test passes just like before the RSpec upgrade.

The second test, however, throws an error:

Failure/Error: expect {
   `expect { }.not_to change { }.to()` is deprecated.

What must I change my syntax to?

I've already tried a couple of things like not_to, be_falsey etc. Nothing worked so far.

Thanks for any help.

like image 740
Tintin81 Avatar asked Jan 10 '23 09:01

Tintin81


1 Answers

Don't assert that the value doesn't change to something, just assert that it doesn't change:

it "is open" do
  expect {
    FactoryGirl.create(:payment, :invoice => @invoice, :amount => 99.99)  
  }.to_not change { @invoice.reload.open? }
end

That doesn't test the initial value of @invoice.reload.open?, but you should have a separate test for that anyway. You don't need to test it again in this test.

Nonetheless, in RSpec 3 you can use .from by itself to test that the value that doesn't change has a given initial value:

it "is open" do
  expect {
    FactoryGirl.create(:payment, :invoice => @invoice, :amount => 99.99)  
  }.to_not change { @invoice.reload.open? }.from(false)
end

You can't do that yet in RSpec 2; .to_not change {}.from passes if the value passed to .from is not what is expected. In RSpec 2.99 that causes a warning.

like image 157
Dave Schweisguth Avatar answered Jan 18 '23 02:01

Dave Schweisguth