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

Consider this example, we would like to check if the user is allowed to have an empty name (it should not be):

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

The question is why assert_not exists? It would be better if we simply used assert as follows:

 test "name should be present" do @user.name = " " assert !@user.valid ? end 
+6
source share
1 answer

There is probably an opportunity to remove modifiers from your statements that may change their results or overshadow what you are actually asking. In fact, it is basically a style choice.

This is a kind of motivation to have unless in the language, instead of writing this:

 if !@user.valid ? # do stuff end 

You would do:

 unless @user.valid? # do stuff end 

Suppose that if / if differences differ from each other better than assert_not , alas, that will conduct the Minitest unit tests. If you want something more natural, take a look at the Minitest or RSpec specifications.

+3
source

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


All Articles