Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我有一个 B 类,其构造函数参数为 A 类。
我希望在为 B 类创建模拟时模拟 A 类。
我怎样才能做到这一点?我尝试了 MockBehavior Loose/Strict 但这没有帮助!
如果您正在模拟类,则可以在调用时传入构造函数参数new Mock<T>:
new Mock<T>
因此,如果您有课程:
public class A {} public class B { private readonly A a; public B(A a) { this.a = a; } }
以下代码使用模拟 A 创建模拟 B:
var mockA = new Mock<A>(); var mockB = new Mock<B>(mockA.Object);