[英]How to use mocked method that in another class Unit Test in MOQ
class CurrentClass
{
public Task OnStep()
{
this.Property = ClassStatic.Method();
}
}
我有2个问题:
对不起我的英语!
使用Microsoft Shims测试静态方法。 但是通常最好不要使用静态类和方法。 像这样使用依赖注入:
class MyClass
{
IUtility _util;
public MyClass(IUtility util)
{
_util = util;
}
public Task OnStep()
{
this.Property = _util.Method();
}
}
public TestMethod()
{
IUtility fakeUtil = Mock.Of<IUtility>();
MyClass x = new MyClass(fakeUtil);
}
但是,如果要使用静态类,请使用垫片:
using (ShimsContext.Create())
{
// Arrange:
// Shim ClassStatic.Method to return a fixed date:
Namespace.ShimClassStatic.Method =
() =>
{
// This will overwrite your static method
// Fake method here
};
// Instantiate the component under test:
var componentUnderTest = new MyComponent();
// Act:
// Assert:
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.