Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I display the validation which failed in my Rails unit test?

Summary: Failed unit tests tell me which assert (file:line) failed, but not which validation resulted in the failure.

More info: I have 11 validations in one of my models. Unit testing is great, whether I run rake test:units --trace or ruby -Itest test/unit/mymodel_test.rb. However, despite the fact that it tells me exactly which assert failed me, I am not told which validation failed. I must be missing something obvious, because I can't ask Google this question well enough to get an answer.

Thanks :)

like image 593
JD. Avatar asked Dec 10 '22 16:12

JD.


2 Answers

I think that what you want is this:

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

if the person model is not valid, the error messages are printed out for you to see.

like image 102
JBK Avatar answered Dec 12 '22 05:12

JBK


In order to print one or more failed validations, use a test like this:

  test "post with neither name nor title" do
    p = Post.new
    assert p.valid?, p.errors.full_messages.inspect
  end
like image 41
JD. Avatar answered Dec 12 '22 05:12

JD.