我如何通过 NMock3 对 GetData 进行单元测试?
如果可以模拟 prcessA.Run 和 ProcessA 中的“结果”,那就太好了。
IAnotherService 不能是 GetData 的参数,因为它取决于 GetData 中的已处理值。
有任何想法吗?
服务1
public class Service1 : IService1
{
public Service1()
{
}
public string GetData(int a)
{
// some process depends on input
int b = a * new Random().Next();
IAnotherService anotherService = new AnotherService(b);
ProcessA processA = new ProcessA(anotherService);
processA.Run();
return processA.result;
}
}
简化流程A
public class ProcessA
{
public string result;
private IAnotherService anotherService;
public ProcessA(IAnotherService anotherService)
{
this.anotherService = anotherService;
}
public void Run()
{
// Some other process here
this.result = this.anotherService.Run();
}
}
测试方法1
[TestMethod]
public void TestMethod1()
{
using (mockFactory.Ordered())
{
// Nothing to Mock
}
IService1 service1 = new Service1();
string aaa = service1.GetData(1);
Assert.AreEqual("XXX", aaa);
}