Rspec not_to change from not behaving as expected

I am somewhat perplexed in rspec and rspec-rails 2.11.0. I reproduced the behavior of a collaborative application running on 2.7.1 (both on ruby ​​1.9.3)

These tests work as expected (crash):

it "should not change i" do i = 0 expect { i = 2 }.not_to change { i } end it "should not change i" do i = 0 expect { i = 2 }.not_to change { i }.from( 0 ) end 

Error message "the result should not be changed, but in both cases it has changed from 0 to 2"

Changing the β€œfrom” while waiting for another value inexplicably causes it to be skipped, and not crash, regardless of what happens to the value i in the wait block:

 it "should not change i" do i = 0 expect { i = 2 }.not_to change { i }.from( 1 ) end 

I just recently upgraded to 1.9.3, and I can say with confidence that I would have noticed this behavior before if I had it. Can someone explain this and / or what am I doing wrong?

+4
source share
1 answer

I don’t think it does what you expect, but the way the test is written doesn't really reveal it. Your last value fails because it has never been defined as 1.

The main step in identifying this is inverting the test.

 describe "stuff" do it "should not change i" do i = 0 expect { i = 2 }.to change { i } end it "should not change i" do i = 0 expect { i = 2 }.to change { i }.from( 0 ) end it "should not change i" do i = 0 expect { i = 2 }.to change { i }.from( 1 ) end end 

This causes the first two tests to pass, and the last test with an error result should have initially been 1, but was 0

So, if we take the test test as is, you say: i should not change from 1 , and since I will never be 1, this test will never end.

 it "should not change i" do i = 0 expect { i = 2 }.not_to change { i }.from( 1 ) # i goes from 0 to 2, not 1 to 2, so the test is valid end 
+5
source

Source: https://habr.com/ru/post/1437454/


All Articles