How to display confirmation that failed in my Rails unit test?

Summary . Failed unit tests tell me which argument assert (file: line) was unsuccessful, but not which check led to an error.

Additional information . I have 11 checks in one of my models. Testing the device is excellent, regardless of whether I perform rake test:units --traceor ruby -Itest test/unit/mymodel_test.rb. However, despite the fact that he tells me which one assertI managed, I did not say which check failed. I have to miss something obvious, because I cannot ask Google this question well enough to get an answer.

Thank:)

+3
source share
3 answers

I think you need the following:

assert person.valid?, person.errors.full_messages.inspect

If the person’s model is invalid, error messages are printed for you.

+6
source

To print one or more failed checks, use this test:

  test "post with neither name nor title" do
    p = Post.new
    assert p.valid?, p.errors.full_messages.inspect
  end
+3
source

You can always look at object errors for an invalid attribute.

class Person
   validates_presence_of :name
end

person = Person.new
person.valid?  # => false

person.errors[:name]  # =>  "can't be blank"
person.errors.full_messages  # => ["Name can't be blank"]

Take a look at ActiveRecord :: Errors for more information.

+2
source

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


All Articles