Skip to content

2. Expectations

Chesko edited this page Dec 6, 2016 · 20 revisions

Expectations are what you expect to happen in your code. If all expectations are true in a test case, the spec passes. If an expectation is not true, the spec fails. These can be things like "I expect this integer to be 5", or "I expect this string to exactly match 'bob'", or "I expect this form variable to contain myCoolSword".

They are usually declared before calling the function under test to verify your pre-conditions, and after calling the function under test in order to verify that its output or effects match what you expect. (For example, you expect a form variable to be empty before you call a function, and to have a value after you call the function.)

Expectations are built with one of the included expect functions. It has 4 parameters: the actual, a condition, a matcher, and the expected. Sounds complicated, but it's really not. They are read in the form of "expect actual to / not to match expected". In the below example, we "expect 5 to be less than 7." A reasonable thing to expect!

expectInt(5, to, beLessThan, 7)

Actual

An actual is the value under test, and is the first parameter in an expectation.

Condition

There are two conditions: to and notTo. notTo inverses the result of the matcher (we expect the actual "not to" be something).

; This expectation would pass
expectBool(True, to, beEqualTo, True)

; This expectation would fail
expectBool(True, notTo, beEqualTo, True)

Matcher

Each matcher implements a boolean comparison between the actual value and the expected value. It is responsible for determining if the expectation is true or false. Lilac will then pass or fail the spec.

Any matcher can evaluate to a negative assertion by using the notTo condition before the matcher.

Examples of matchers include beEqualTo, beGreaterThan, and contain.

Which matchers are available depends on which expect function you use. See the API Reference for a list of all expect functions and their available matchers.

Expected

An expected is the last parameter of an expectation, and is the value you expect the actual to be.

int n = 15
expectInt(n, to, beEqualTo, 15)

Some expectations don't require an expected, as they are built into the matcher. beTruthy, beFalsy, and beNone are such matchers.

expectBool(True, to, beTruthy)
expectBool(False, to, beFalsy)
expectForm(None, to, beNone)

Clone this wiki locally