我在我的項目中使用Autofac IoC容器類的類,但我不能做一個單元測試在一個特定的類。我如何單元測試使用
考慮以下情形:
//Class to be tested
public Class A
{
private SomeAutoFacClass B;
public void DoSomething()
{
B = scope.Resolve<ClassName>();// Resolve the object needed
// Do something with instance B
}
}
// Test class
public Class ATest
{
private A a;
[test]
public void TestMethod()
{
a.DoSomething();//*This method causes a null reference exception as it tries to resolve the objects*
}
}
在上面的代碼,我不能夠到單元測試用例由於依賴注入其僅專用於該特定的類。 我該如何解決這個問題?我也嘗試使用Moq創建一個autofactainer。 但這也失敗了。
這是非常好的解釋!更多這樣的SO。 – Seabizkit