问题
I have a class B with a constructor parameter of Type class A.
I want that class A is mocked when I create a mock for class B.
How can I do this? I tried MockBehavior Loose/Strict but this did not help!
回答1:
If you are mocking classes you can pass in the constructor arguments when calling new Mock<T>
:
So if you have the classes:
public class A {}
public class B
{
private readonly A a;
public B(A a) { this.a = a; }
}
The following code creates a mock B with a mock A:
var mockA = new Mock<A>();
var mockB = new Mock<B>(mockA.Object);
来源:https://stackoverflow.com/questions/15420618/mock-a-constructor-with-moq