是否可以使用最小起订量模拟 System.Windows.Window
Is it possible to mock System.Windows.Window using moq
我有一个 AsyncDelegateCommand
接受 Window
的参数,我尝试模拟 class 但我以异常结束 The calling thread must be STA, because many UI components require this.
删除门命令:
private AsyncDelegateCommand<Window> okCommand;
public AsyncDelegateCommand<Window> OkCommand => this.okCommand ?? (this.okCommand = new AsyncDelegateCommand<Window>(this.OkAsync));
方法实现:
private async Task OkAsync(Window win)
{
//Logic
}
我目前模拟它的方式是使用:
private Mock<Window> mockWindow;
this.mockWindow = new Mock<Window>();
await this.sut.OkCommand.ExecuteAsync(this.mockWindow.Object);
在调试对象时,我可以看到对象值是异常 this.mock Window.Object' threw an exception of type 'System.Reflection.TargetInvocationException
如果不可能,是否有其他方法可以实现这一目标?
我能够按照评论 [Apartment(ApartmentState.STA)]
对我的测试方法
添加属性的建议来解决问题
例如:
[Test]
[Apartment(ApartmentState.STA)]
public async Task TestMethod()
{
//Logic
}
我有一个 AsyncDelegateCommand
接受 Window
的参数,我尝试模拟 class 但我以异常结束 The calling thread must be STA, because many UI components require this.
删除门命令:
private AsyncDelegateCommand<Window> okCommand;
public AsyncDelegateCommand<Window> OkCommand => this.okCommand ?? (this.okCommand = new AsyncDelegateCommand<Window>(this.OkAsync));
方法实现:
private async Task OkAsync(Window win)
{
//Logic
}
我目前模拟它的方式是使用:
private Mock<Window> mockWindow;
this.mockWindow = new Mock<Window>();
await this.sut.OkCommand.ExecuteAsync(this.mockWindow.Object);
在调试对象时,我可以看到对象值是异常 this.mock Window.Object' threw an exception of type 'System.Reflection.TargetInvocationException
如果不可能,是否有其他方法可以实现这一目标?
我能够按照评论 [Apartment(ApartmentState.STA)]
对我的测试方法
例如:
[Test]
[Apartment(ApartmentState.STA)]
public async Task TestMethod()
{
//Logic
}