微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

c# – 用犀牛嘲讽两次摧毁一个属性

对于某些对象,我想创建认存根,以便公共属性包含值.但在某些情况下,我想覆盖我的认行为.我的问题是,我可以以某种方式覆盖已经存根的值吗?
//First I create the default stub with a default value
var foo = MockRepository.GenerateStub<IFoo>();
foo.Stub(x => x.TheValue).Return(1);

//Somewhere else in the code I override the stubbed value
foo.Stub(x => x.TheValue).Return(2);

Assert.AreEqual(2,foo.TheValue); //Fails,since TheValue is 1

解决方法

使用Expect而不是Stub和GenerateMock而不是GenerateStub将解决这个问题:
//First I create the default stub with a default value
var foo = MockRepository.GenerateMock<IFoo>();
foo.Expect(x => x.TheValue).Return(1);

//Somewhere else in the code I override the stubbed value
foo.Expect(x => x.TheValue).Return(2);

Assert.AreEqual(1,foo.TheValue);
Assert.AreEqual(2,foo.TheValue);

原文地址:https://www.jb51.cc/csharp/99621.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐