ruby-on-railstestingautomated-testsassert

rails assert_difference without specific difference value


I'm new to rails tests and I'm trying to understand the call to assert_difference. From the documentation, I deduce that the method requires a numerical value for the difference between the previous and the final value of the expression. What if I just want to assert that there is a difference, no matter how big it is? Maybe something like assert_not (assert_no_difference ... )?


Solution

  • If you look at the documentation you'll notice that the signature for the method is:

    assert_difference(expression, difference = 1, message = nil, &block)
    

    The difference = 1 indicates that the method sets 1 as the default value for the difference argument if no value is provided.

    The general idea behind this method is that there will be a specific change in the value of something that you want to verify in your test. If you want to just test that a value has changed you can use your own variables and asserts.

    Maybe something like the following will suffice:

    value = 0
    new_value = some_function
    assert_not_equal value, new_value
    

    Best of luck!