[英]How to mock a class in C# unit testing project
I have a class I want to moq:我有一个 class 我想要最小起订量:
public class ODriver
{
public int DriverId { get; set; }
public virtual bool CanDrive()
{
return false;
}
}
She is used like this in the method I want to test:她在我要测试的方法中是这样使用的:
var driver = new ODriver();
// Must be false except in the unit test
var canDrive = driver.CanDrive();
I mock the method like this in my unit test:我在单元测试中模拟了这样的方法:
var driverMoq = new Mock<ODriver>();
driverMoq.Setup(d => d.CanDrive()).Returns(true);
When the test is runned, the result of the CanDrive method returns false.运行测试时,CanDrive 方法的结果返回 false。 What do I have to do in order to have the mocked class used?
为了使用模拟的 class,我需要做什么?
Thank you all !谢谢你们 !
So the answer is: I adapted the code of the method I tested and added the ODriver object as parameter.所以答案是:我修改了我测试的方法的代码,并添加了 ODriver object 作为参数。
Thank you for your help, I didn't know how it works.感谢您的帮助,我不知道它是如何工作的。
PierreLuc皮埃尔卢克
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.