CAL-单元测试-跨线程事件调用 [英] CAL - Unit Testing - Cross Thread Event Invocation

查看:84
本文介绍了CAL-单元测试-跨线程事件调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我正在使用复合应用程序库进行Windows应用程序开发
我正在单元测试中遇到问题
这就是发生了什么
1.我继承了Bootstrapper并通过测试类运行它,然后我可以使用Container了,现在我可以解析所需的对象进行测试
2.我将测试每种功能,例如启动将值分配给属性等的命令
问题发生在2
问题在于将值分配给我在委托命令"中调用RaiseCanExecuteChanged()的属性
它会引发错误,因为调用线程无法访问该对象,因为其他线程拥有它."
我该如何解决这个问题?
我的测试班的示例代码


Hi,
I am Using Composite Application Library for My Windows Application Development
and i am Facing problem in Unit Testing
This is what going on
1.I am inheriting the Bootstrapper and running it from by test class and then i will have the Container available to me and now i resolve the required object to test
2. i will test each functionality like initiating the command assigning values to properties etc
the problem occurs in 2
the problem is on assigning value to the property i am calling RaiseCanExecuteChanged() in Delegate Command
it is throwing error as "The calling thread cannot access this object because a different thread owns it."
how do i resolve this issue??
sample code From my Testing Class


[TestFixtureSetUp]
[RequiresSTA]
public void TestSetup()
{
_thread = new Thread(new ThreadStart(() =>
{
_application = new MockApplication();
//System.Windows.Application.
_application.Run(true);
}));
_thread.SetApartmentState(ApartmentState.STA);
_thread.Start();
_thread.Join(100000);
model = _application.Container.Resolve(typeof(NewCaseViewModel)) as NewCaseViewModel;
}
[Test]
public void TestSearchFunctionality()
{
Assert.True(model != null);
if (model != null)
{
model.MemberToSearch = "raghu";
if (model.SearchCommand.CanExecute(""))
{
model.SearchCommand.Execute("");
}
Assert.True(model.SearchedMembers != null);
}
}
}
here is my code here the nunit framework is calling the test method and i cant make the _thread Execute the line <code>model.MemberToSearch = "raghu";</code>


this is the code written in the Setter of the MemberToSearch Property

<pre lang="cs">
                _memberToSearch = value;
                OnPropertyChanged("MemberToSearch");
                ((DelegateCommand)_searchCommand).RaiseCanExecuteChanged();




问候
Raghunandan




Regards
Raghunandan

推荐答案

您应该通过在与用于实例化该对象的线程相同的线程中解决该对象来解决此问题.这可能是一个简单的重新设计问题.只需在同一线程中移动实例化代码即可. (您没有显示足够的代码,但这很简单.)

—SA
You should resolve this issue by addressing the object in question in the same thread as the thread used to instantiate it. This could be a matter is simple re-design. Just move the instantiation code in the same thread. (You don''t show enough code, but this is a simple as that.)

—SA


这篇关于CAL-单元测试-跨线程事件调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆