How to compare two things that should be different?

I was reviewing a testcase and came across this construct:

true = NewMeasGap /= MeasGap,

The context is that we want to verify that the MeasGap has changed after we updated a thing that should affect the MeasGap. It’s sufficient to know that it has changed, we don’t need to know the new value.

Is there a better way to express this? MeasGap is a tuple (record).

Yes there is: ?assertNotMatch/2,3 and assertNotEqual/2,3 (from "stdlib/include/assert.hrl") provide much more information if something doesn’t work the way it’s supposed to.

6 Likes

I was going to say that those are from eunit, but apparently they’re not! Thanks, that seems to be what I’m looking for.

2 Likes

?assertNotMatch/2,3 and ?assertNotEqual/2,3 are the right way:tm: to do this, but… if you don’t feel like using macros (or if you don’t like your macros writtenInCamelCase :person_facepalming:), this is what I would use with common test:

case NewMeasGap of
    MeasGap ->
        ct:fail(
            "The <what MeasGap represent> should've changed, but it is still ~p",
            [MeasGap]);
    _ -> cool
end

And… of course… you can put that in a function…

assert_not_match(Value, Value, Meaning) ->
    ct:fail("~ts should've changed, but it is still ~p", [Meaning, Value]);
assert_not_match(NewValue, Value, Meaning) -> cool.

…and then…

    assert_not_match(MeasGap, NewMeasGap, "The thing")
2 Likes