Hamcrest-as3 语法 接上一篇使用(Flash Builder 4和FlexUnit进行测试驱动开发)

Hamcrest
1. Hamcrest是一个创建匹配器的库,每个匹配器都可以设置到你的断言方法的匹配条件里面去。
2. Hamcrest-as3 它自身不是一个单元测试框架,但是它为简单的flexUnit4 中提供的一些
断言方法 例如:assertEquals()样式不同的设想提供了可扩展的能力,
在每个matcher中,都可以为你的断言设置匹配条件。
3. Hamcrest-as3库能够与MXML类兼容。该类叫做MatherValidator,它扩展了Validator基类,
有典型的验证属性
4. assertThat的基本语法如下:mathcher是使用Hamcrest匹配符来表达的对前面变量值所期望值的声明,如果
actual值与matcher所表达的期望值是相符合的,。则断言成功,否则,断言失败
assertThat(T actual, Matcher matcher)
assertThat(String reason, T actual, Matcher matcher)
一个简单的例子:
// 如果测试的字符串testedString包含子字符串”taobao”则断言成功
assertThat( testedString, containsString( “taobao” ) );
新的断言语法会默认提供一些可读的描述信息,如果上述断言失败
5.跟mathcer匹配符联合使用
Mathcher提供了功能强大的匹配符,asserThat结合这是匹配符使用可更灵活更准确的表达测试思想。

//验证字符串 s是否含有子字符串“taobao”或“qa”中间的一个
//旧的断言,不直观,需要分析代码逻辑明白验证意图
assertTrue(s.indexOf(“taobao”)>-1 || s.indexOf(“qa”)>-1);

//新的断言,准确表达测试思想
assertThat(s,anyOf(containsString(“taobao”),containsString(“qa”)));
// anyOf满足条件之一即成立,containsString包含字符串则成立
6. Hamcrest-as3带有一个有用的匹配器库,每一个flexUnit‘s的断言方法都能被assertThat
和一个适当的匹配器所代替。
例如
1)assertEquals 能被equalTo 匹配器代替
assertThat(actualValue,equalTo(expectedValue));
2)assertStrictlyEquals 能被 strictlyEqualTo 匹配器代替
assertThat(actualValue, strictlyEqualTo(expectedValue));
3)assertTrue 能被 equalTo匹配器代替:
assertThat(actualValue, equalTo(true));
4)assertFalse能被 equalTo 匹配器代替:
assertThat(actualValue, equalTo(false));
5)assertNull 能被nullValue 匹配器代替
assertThat(actualValue, nullValue());
6)assetNotNull能被 a combination of not and the nullValue 匹配器代替,或者 the shortcut notNullValue匹配器代替:
assertThat(actualValue, not(nullValue()));
assertThat(actualValue, notNullValue());

7.各种类型相关的匹配符
A.字符相关匹配符

1)emptyString  匹配符断言被测的字符串是否为空
assertThat(textInput.text, not(emptyString()));

2)containsString匹配符断言被测的字符串“the quick brown fox”是否包含了“fox”
assertThat(“The quick brown fox”, containsString(“fox”));

3)startsWith 匹配符断言被测的字符串”The quick brown fox”是否以“The”开始.
assertThat(“The quick brown fox”, startsWith(“The”));

4)endsWith 匹配符断言被测的字符串”The quick brown fox”是否以“The“结束
assertThat(“The quick brown fox”, endsWith(“fox”));

5)re 匹配符断言被测的字符串 if it matches the given RegExp.
assertThat(“has some whitespace”, “The quick brown fox”, re(/\s+/));

/**equalTo匹配符断言被测的testedValue等于expectedValue,在org.hamcrest.object.equalTo包中
* equalTo可以断言数值之间,字符串之间和对象之间是否相等,相当于Object的equals方法
*/
assertThat(testedValue, equalTo(expectedValue));
/**equalToIgnoringCase匹配符断言被测的字符串testedString
*在忽略大小写的情况下等于expectedString
*/
assertThat(testedString, equalToIgnoringCase(expectedString));
/**equalToIgnoringWhiteSpace匹配符断言被测的字符串testedString
*在忽略头尾的任意个空格的情况下等于expectedString,
*注意:字符串中的空格不能被忽略
*/
assertThat(testedString, equalToIgnoringWhiteSpace(expectedString);
/**containsString匹配符断言被测的字符串testedString包含子字符串subString**/
assertThat(testedString, containsString(subString) );

B.一般匹配符
  • /**nullValue()匹配符断言被测object的值为null*/
    assertThat(object,nullValue());
  • /**notNullValue()匹配符断言被测object的值不为null*/
    assertThat(object,notNullValue());
  • /**is匹配符断言被测的object等于后面给出匹配表达式*/
    assertThat(testedString, is(equalTo(expectedValue)));
  • /**is匹配符简写应用之一,is(equalTo(x))的简写,断言testedValue等于expectedValue*/
    assertThat(testedValue, is(expectedValue));
  • /**is匹配符简写应用之二,is(instanceOf(SomeClass.class))的简写,
    *断言testedObject为Cheddar的实例
    */
    assertThat(testedObject, is(Cheddar.class));
  • /**not匹配符和is匹配符正好相反,断言被测的object不等于后面给出的object*/
    assertThat(testedString, not(expectedString));
  • /**allOf匹配符断言符合所有条件,相当于“与”(&&)*/
    assertThat(testedNumber, allOf( greaterThan(8), lessThan(16) ) );
  • /**anyOf匹配符断言符合条件之一,相当于“或”(||)*/
    assertThat(testedNumber, anyOf( greaterThan(16), lessThan(8) ) );
  • anything 匹配符断言总是通过的
    assertThat(“the great void”, anything());
  • isA匹配符断言断言前面的对象是否是后面的对象的类型
    assertThat(”waffles”,isA(String));
  • both,和either组合的一个匹配符断言
    assertThat断言“good”是否与”good“相等并且不等于”bad“
    断言“good”是否与“good”相同或者与“bad”不相同
    assertThat(“good”, both(equalTo(“good”)).and(not(equalTo(“bad”));
    assertThat(“good”, either(equalTo(“good”)).or(not(equalTo(“bad”));
  • describedAs wraps another Matcher to return a modified description.(这个没看明白)
    assertThat(3, describedAs(“%0 is a magic number”, equalTo(3), 3);
  • throws matches if the item under test is a Function, and throws an Error matching the given Matcher.
    throws匹配符断言前面的函数抛出的异常是否与后面给的异常是否相同
    assertThat(function():void {
    systemUnderTest.methodCall(given, bad, args);
    },
    throws(
    allOf(
    instanceOf(OhNoItsAnError),
    hasPropertyValue(“message”, “oh no”))));

C.数值相关匹配符 org.hamcrest.numbers.*;

  • /**closeTo匹配符断言被测的浮点型数testedDouble在20.0¡À0.5范围之内*/
    assertThat(testedDouble, closeTo( 20.0, 0.5 ));
  • /**greaterThan匹配符断言被测的数值testedNumber大于16.0*/
    assertThat(testedNumber, greaterThan(16.0));
  • /** lessThan匹配符断言被测的数值testedNumber小于16.0*/
    assertThat(testedNumber, lessThan (16.0));
  • /** greaterThanOrEqualTo匹配符断言被测的数值testedNumber大于等于16.0*/
    assertThat(testedNumber, greaterThanOrEqualTo (16.0));
  • /** lessThanOrEqualTo匹配符断言被测的testedNumber小于等于16.0*/
    assertThat(testedNumber, lessThanOrEqualTo (16.0));、
  • between匹配符断言被测的值是否在最大值与最小值之间
    assertThat(2, between(2, 4));
D.集合相关匹配符 org.hamcrest.Array.*;
/**hasEntry匹配符断言被测的Map对象mapObject含有一个键值为”key”对应元素值为”value”的Entry项*/
assertThat(mapObject, hasEntry(“key”, “value” ) );
/**hasItem匹配符表明被测的迭代对象iterableObject含有元素element项则测试通过*/
assertThat(iterableObject, hasItem (element));
/** hasKey匹配符断言被测的Map对象mapObject含有键值“key”*/
assertThat(mapObject, hasKey (“key”));
/** hasValue匹配符断言被测的Map对象mapObject含有元素值value*/
assertThat(mapObject, hasValue(value));
array matches if each of the matchers given are satisfied by the element at the same index in the array that is being matched.
// explicit definition of matchers
assertThat([1, 1.9, 4], array(equalTo(1), closeTo(2, 0.1), equalTo(4));

// implicit using equalTo()
assertThat([1, 1.9, 4], array(1, 2, 4));

arrayWithSize matches an Array with the expected number of items.
assertThat([1, 2, 3], arrayWithSize(3));
emptyArray matches an Array with zero items.
assertThat([], emptyArray())
everyItem matches an Array if every item matches the given Matcher.
assertThat([1, 2, 3], everyItem(isA(Number)));
hasItem matches an Array that contains at least one item that matches the given Matcher.
assertThat([1, 2, 3], hasItem(equalTo(3));
hasItems matches an Array if each of the matchers given are satisfied by any item in the array.
assertThat([1, 2, 3, 4], hasItems(equalTo(2), equalTo(4)));
E.日期相关匹配符 org.hamcrest.Dates.*;
dateAfter 断言被测的日期是否在这个期望的日期之后(即1920,1,1)
assertThat(new Date(), dateAfter(new Date(1920, 1, 1)));
dateBefore 断言被测的日期即1920年1月1日是否在今天之前
assertThat(new Date(1920, 1, 1), dateBefore(new Date()));
dateBetween 断言今天是否在期望的日期之间
assertThat(new Date() , dateBetween(new Date(1920, 1, 1), new Date(2200, 1, 1)));
dateEqual 断言今天是否与期望的日期相同
assertThat(new Date(), dateEqual(new Date()));
F.对象相关匹配符 org.hamcrest.Objects.*;
equalTo matches if the item is equal (==) to the given value.
assertThat(“hi”, equalTo(“hi”));
strictlyEqualTo 匹配符断言匹配的对象是否与期望的对象是否相等
var o1:Object = {};
var o2:Object = {};
assertThat(o1, strictlyEqualTo(o1″));
assertThat(o1, not(strictlyEqualTo(o2)));
hasProperty 匹配符断言匹配的对象是否包含了期望对象给的属性
assertThat({ id: 1234, data: null }, hasProperty(“data”));
hasProperty 匹配符断言匹配的对象中的属性与值是否与期望给的属性与值相等
assertThat({ id: 1234, data: null }, hasProperty(“data”, nullValue()));
hasProperties matches an item if it hasProperty for each of the given fields and matchers.
assertThat({ id: 1234, data: null }, hasProperties({
id: greaterThan(1000),
data: notNullValue()
}));
instanceOf if the item is an instance of the given type.
assertThat(“waffles”, instanceOf(String));
nullValue if the item is null
assertThat(null, nullValue( ));
notNullValue if the item is not null
assertThat(“hi”, notNullValue( ));

 

Advertisements

发表评论

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / 更改 )

Twitter picture

You are commenting using your Twitter account. Log Out / 更改 )

Facebook photo

You are commenting using your Facebook account. Log Out / 更改 )

Google+ photo

You are commenting using your Google+ account. Log Out / 更改 )

Connecting to %s

%d 博主赞过: