做者: 梅桦 发表于 2010-05-11 11:17 原文连接 阅读: 92 评论: 0html
(一)安装结果(SetupResult)程序员
有时候在模拟对象中须要一个方法的返回值,而不在乎这个方法是否被调用。就能够经过安装结果(SetupRestult)来设置返回值,而绕开指望安装,且可使用屡次。从依赖的角度来讲是这样的:方法a(或属性)被方法b使用,而在其它的位置c处方法a又会被使用,而在c处使用以前,不保证是否在b处使用且修改了方法a的返回值。意思就是保证方法a的返回结果是固定的,是忽略它的依赖,而在它该用的位置使用它恒定的值。安装结果能够达到这种效果。 测试
属性 DesriptionId 被方法 PrintDescription() 依赖。
从这段测试中能够看到,对customer的DescriptionId属性进行告终果安装,只让这个属性返回10。而在随后对依赖它的方法进行了指望安装,且能够被调用2次。但DescriptionId的值还是10。网站
Expect.Call(delegate { customer.PrintDescription(); }).Repeat.Times(2);this
这句是对不带返回值的方法进行指望安装,固然可使用Lambda来进行。这个匿名方法就是一个不带参数,没有返回值的委托,这个就至关于Action<>,经过lambda就是:()=>customer.PrintDescription(),完整就是:spa
Expect.Call(()=>customer.PrintDescription()).Repeat.Times(2);code
关于匿名方法和Action委托可见:orm
http://www.cnblogs.com/jams742003/archive/2009/10/31/1593393.html视频
http://www.cnblogs.com/jams742003/archive/2009/12/23/1630737.htmlhtm
安装结果有两种方法:
For和On,For在上边已经使用,On的参数是mock object。对于上边的示例中的粗体部分用On来实现为:
这个也能够经过指望的选项来实现。例如:
其中的粗体部分,能够屡次使用,且忽略参数。
(二)约束(Constraints)
约束用来对指望的参数进行规则约束。系统提供了大量内建的约束方法,固然也能够自定义。这里直接贴一张官网给出的列表,一目了然:
约束 |
说明 |
例子 |
接受的值 |
拒绝的值 |
Is |
任何 |
Is.Anything() |
{0,"","whatever",null, etc} |
Nothing Whatsoever |
等于 |
Is.Equal(3) |
3 |
5 |
|
不等于 |
Is.NotEqual(3) |
null, "bar" |
3 |
|
无 |
Is.Null() |
null |
5, new object() |
|
不为无 |
Is.NotNull() |
new object(), DateTime.Now |
null |
|
指定类型 |
Is.TypeOf(typeof(Customer)) or Is.TypeOf() |
myCustomer, new Customer() |
null, "str" |
|
大于 |
Is.GreaterThan(10) |
15,53 |
2,10 |
|
大于等于 |
Is.GreaterThanOrEqual(10) |
10,15,43 |
9,3 |
|
小于 |
Is.LessThan(10) |
1,2,3,9 |
10,34 |
|
小于等于 |
Is.LessThanOrEqual(10) |
10,9,2,0 |
34,53,99 |
|
匹配 |
Is.Matching(Predicate) |
|
|
|
相同 |
Is.Same(object) |
|
|
|
不相同 |
Is.NotSame(object) |
|
|
|
Property |
等于值 |
Property.Value("Length",0) |
new ArrayList() |
"Hello", null |
无 |
Property.IsNull ("InnerException") |
new Exception ("exception without inner exception") |
new Exception ("Exception with inner Exception", new Exception("Inner") |
|
不为无 |
Property.IsNotNull ("InnerException") |
new Exception ("Exception with inner Exception", new Exception("Inner") |
new Exception ("exception without inner exception") |
|
List |
集合中包含这个元素 |
List.IsIn(4) |
new int[]{1,2,3,4}, new int[]{4,5,6} |
new object[]{"",3} |
集合中的元素(去重) |
List.OneOf(new int[]{3,4,5}) |
3,4,5 |
9,1,"" |
|
等于 |
List.Equal(new int[]{4,5,6}) |
new int[]{4,5,6}, new object[]{4,5,6} |
new int[]{4,5,6,7} |
|
Text |
以…字串开始 |
Text.StartsWith("Hello") |
"Hello, World", "Hello, Rhino Mocks" |
"", "Bye, Bye" |
以…字串结束 |
Text.EndsWith("World") |
"World", "Champion Of The World" |
"world", "World Seria" |
|
包含 |
Text.Contains("or") |
"The Horror Movie...", "Either that or this" |
"Movie Of The Year" |
|
类似 |
Text.Like ("rhino|Rhinoceros|rhinoceros") |
"Rhino Mocks", "Red Rhinoceros" |
"Hello world", "Foo bar", Another boring example string" |
例子:
它的意思就是若是参数以cnblogs开头,则返回指望值。能够比较一下Moq的参数约束设置方法:
http://www.cnblogs.com/jams742003/archive/2010/03/02/1676197.html
除了上述方法外,rhinomock约束还支持组合,即与,非,或。还以上例进行:
参数的条件就是以cnblogs开头,且以!号结束。
最新新闻:
· 电子商务网站之信任度(2010-10-09 17:02)
· 马云:管理的核心在于“抓住人性的本真”(2010-10-09 16:52)
· 另外一 Windows Phone Live 主页截图现身 Windows Phone 7 视频(2010-10-09 16:38)
· 谷歌首名员工:公司成功归结于运气不错(2010-10-09 16:32)
· 神奇小子Geohot带着“limera1n”回归(2010-10-09 16:29)
编辑推荐:远离.NET