Skip to content
drewbourne edited this page Sep 13, 2010 · 1 revision

Hamcrest is available in FlexUnit tests through the assertThat() function. assertThat takes its arguments in a different order than assertEquals in order to make the tests using assertThat and matchers more readable.

The two common signatures for assertThat are:

  assertThat(valueToMatch:Object, matcher:Matcher);
  assertThat(assertionDescription:String, valueToMatch:Object, matcher:Matcher);

A little example should make it clearer.

  var car:Car = new Car({ 
    engine: new Engine({ type: petrol, cylinders: 6 }), 
    wheels: [new Wheel(), new Wheel(), new Wheel(), new Wheel()]
    });

  assertThat(car, isA(Car));
  assertThat(car, hasProperty("engine", hasProperty("cylinders", between(4, 8))));
  assertThat(car, hasProperty("wheels", allOf(arrayWithSize(4), everyItem(isA(Wheel)))));  

FlexUnit Assertions

Every one of FlexUnit’s Assert methods can be replaced with assertThat and an appropriate matcher.

assertEquals can be replace with the equalTo matcher:

  assertThat(actualValue, equalTo(expectedValue));

assertStrictlyEquals can be replace with the strictlyEqualTo matcher:

  assertThat(actualValue, strictlyEqualTo(expectedValue));

assertTrue can be replace with equalTo:

  assertThat(actualValue, equalTo(true));

assertFalse can be replace with equalTo:

  assertThat(actualValue, equalTo(false));

assertNull can be replace with nullValue matcher:

  assertThat(actualValue, nullValue());

assetNotNull can be replace with a combination of not and the nullValue matcher, or the shortcut notNullValue:

  assertThat(actualValue, not(nullValue()));
  assertThat(actualValue, notNullValue());