Search code examples
oopsmalltalkpharo

Using assert:equals: in pharo


I have the following program in Pharo: 2 classes Yacht and YachtRental, Test Class and YachtRental test. I need to implement the following: on the 4th day, the customer gets discount = 10 % on the daily rate. Here is my code:

I need to implement the following: on the 4th day, the customer gets discount = 10 % on the daily rate. Here is my code:

| yachtRental myCruise |
    yachtRental := YachtRental new.
    myCruise := Yacht cruise.
    self assert: (yachtRental priceFor: myCruise days: 4) = 890

Basically, I need to be able to implement the discount of 10 % here, however there is a message "Using assert:equals: produces better context on rule failure", could you please help me explain what is wrong with it.


Solution

  • assert: takes a boolean, whereas assert:equals: takes two expressions. And, assert: doesn't know what you are testing, but assert:equals: knows that you are testing for two things to be equal.

    In case of your test failing, assert: cannot print a meaningful failure message since all the information it has access to is false, so all it can print is "I expected something to be true but it wasn't."

    assert:equals: has access to the values of the two expressions and can thus print something like "I expected foo to be equal to bar".

    Since good failure messages are one of the most important aspects of a test library, the authors of the test library are guiding you to use the more expressive assertions in their library instead of a generic "is this true?"

    [Note: I am ignoring reflection here. Both methods could, of course, reflectively inspect the source code of the test.]