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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With