如何模仿这个概念

时间:2011-09-21 18:32:06

标签: unit-testing c#-4.0 moq

class Test : ITest
{
   public string MyValue { get; }
   public string Detail {get; set;}
}

我想以这种方式模拟方法MyValue

Test myTest = new Test() { Detail = "My Test" };
MockRepository repositoryTest = new MockRepository(MockBehavior.Default);
Mock<Test> mockTest = repositoryTest.Create(myTest);

mockTest.Setup(t => t.MyValue).Returns("Some text");
return myTest;

N.B。这段代码不起作用,只是为了理解我想要的东西。

其他方式如何模拟现有对象的实例?我使用Google Moq。

1 个答案:

答案 0 :(得分:0)

你可能应该在界面上嘲笑,而不是实现类。

使用Mock<T>.Get(T)Mock<T>获取T

ITest test = new Mock<ITest>().Object;
Mock<ITest> mockTest = Mock<ITest>.Get(test);

编辑:您可以使用Moq.Stub命名空间来存根属性。请查看此页面:http://blog.theagileworkshop.com/2009/03/24/stubs-in-moq/