small medium large xlarge

Generic-user-small
21 Nov 2011, 17:35
Allan Berry (8 posts)

<Disclaimer> Please tell me if this is not the right venue for newb questions like this, and I’ll take it elsewhere. Not sure if this forum is just for errata, et al. </Disclaimer>

Trying to slog my way through the Agile Rails depot tutorial. I’m confused about assert_equal… This might be my (intermediate) exposure to Java in the past, and my discomfort with Ruby’s lack of parentheses, but in Ruby-Doc, the assert_equal method is structured like this:

` ~~~ assert_equal( expected, actual, failure_message = nil ) ~~~

in the Agile Rails PragProg book, on page 84, it looks like this:

~~~ assert_equal "must be greater than or equal to 0.01", product.errors[:price].join('; ') ~~~

Is the string "must be greater..."` the expected value? What is being tested for equality?

Samr_small_pragsmall
21 Nov 2011, 18:56
Sam Ruby (634 posts)

Parenthesis are optional in method calls, so the above is exactly equivalent to

assert_equal( "must be greater than or equal to 0.01",
    product.errors[:price].join('; '), nil )
Generic-user-small
22 Nov 2011, 05:11
Allan Berry (8 posts)

Still confused (probably being dense). So we’re trying to make sure that the string matches a concatenated set of errors? Why is this necessary?

And what does this have to do with the previous statement assert product.invalid? ? How is this equality measure used to return an error message?

Samr_small_pragsmall
22 Nov 2011, 07:32
Sam Ruby (634 posts)

product.errors[:price] is an array of errors, after all, there may be multiple errors associated with that one field.

We could separately verify that the length of this array (and therefore the number of errors associated with this field) is exactly one AND that the first such error is the one that we are expecting, or we can simply concatenate all of the errors and verify that the result is what we are expecting.

assert product.invalid? is arguably redundant in this particular example, and certainly less precise than checking for a specific error message. It does have some minimal value in that it checks that there not only is a message, but that this field has been flagged as in error.

If no error message is passed, assert_equal will simply state the obvious when there is a failure, namely that expected is not equal to actual.

You must be logged in to comment