[英]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.