Gabrijel Šimunović Gabrijel Šimunović - 1 year ago 93
Ruby Question

Why 'assert_not' and not '!' when writing Rails tests?

Consider this example, we'd like to check whether it's allowed for user to have blank name (it shouldn't be):

test "name should be present" do = " "
assert_not @user.valid?

The question is why does
exists? Wouldn't it be better if we just use
like this:

test "name should be present" do = " "
assert !@user.valid?

Answer Source

It likely exists to remove modifiers from your assertions, which may change their results or obscure what you're actually asking. In reality, it's mostly a style choice.

It's kind of the same motivation for having unless in the language, instead of writing this:

if !@user.valid?
  # do stuff

You would do:

unless @user.valid?
  # do stuff

Granted, the if/unless differences read way better than assert_not, alas that's what Minitest unit tests are going to get you. If you want things to read more naturally, take a look at Minitest specs or RSpec.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download