繁体   English   中英

模拟一个应该在其构造函数中使用MoQ进行操作的对象

[英]Mock an object that should do an action in its constructor with MoQ

我有一个正在做非常具体工作的类:实例化一个对象,并确保将该对象放置在已创建的同一线程上(我有一些需要此行为的锁,但这不是主题)。

ManagedLifeCycleObjectInstantier<SomeType> myInstance = ManagedLifeCycleObjectInstantier<SomeType>.Instantiate()){
myInstance.InstantiatedObject //Here my instantiated object is available

//Some other thread calls
//Then when the instantier is disposed, it ensure that the instance also get disposed, but on the correct thread.
myInstance.Dispose();

我想对我的ManagedLifeCycleObjectInstantier ,我的情况之一就是SomeType的构造函数抛出了异常,我必须确保收到此异常,并且ManagedLifeCycleObjectInstantier不会停留在该异常上。

无需完整的模拟。 只需创建一个用于测试的类,即可将其构造函数和

Assert.That(
    () => ManagedLifeCycleObjectInstantiator.Instantier<MyType>(),
    Throws.TypeOf<MyException>());

对于线程测试,不清楚要测试什么。 如果将其放在错误的线程上会发生什么?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM