[英]C# unit tests get stuck on COM objects
I am working on a project in C# and i have a piece of cod that work perfectly in main()
but in tests the debugger get stuck on a COM object call and test can't pass that line. 我正在用C#开发一个项目,我有一条鳕鱼可以在
main()
中正常工作,但是在测试中,调试器卡在COM对象调用上,并且测试无法通过该行。
This is the code: 这是代码:
private ICMSDWorkset CMSWorkset;
private ICMSDProject CMSProject;
[TestMethod()]
public void CMSConnector_EntityProviderTest()
{
string connectionString = @"PATH";
CMSAPI capi = new CMSAPI();
//capi.Init(connectionString, "USER", "Project name");
Init(connectionString, "@SETUP", "iDB_P01");
Terminate();
}
private void Init(String connectionString, string currentUser, string currentProject)
{
CMSWorkset = new CPLTWorkset() as ICMSDWorkset;
CMSWorkset.Init("", "", connectionString);
if (!CMSWorkset.IsInitialized())
throw new ArgumentException(
new StringBuilder().AppendFormat("")
.AppendFormat("initialization failed for connection string '{0}'", connectionString)
.ToString()
);
// set user
ICMSDOwnCollection tmpColl = CMSWorkset.GetAllUsers() as ICMSDOwnCollection;
CMSWorkset.SetCurrentUser(tmpColl.Item(currentUser));
// set project as current in workset
tmpColl = CMSWorkset.GetAllProjects() as ICMSDOwnCollection;
CMSProject = tmpColl.Item(currentProject) as ICMSDProject;
CMSWorkset.SetCurrentProject(CMSProject);
if (!CMSWorkset.IsInitialized())
throw new ArgumentException(
new StringBuilder().AppendFormat("")
.AppendFormat("initialization failed for connection string '{0}', user '{1}' and project name '{2}'",
connectionString, currentUser, currentProject)
.ToString()
);
}
public void Terminate()
{
// do the deallocations; mandatory
CMSWorkset.Terminate();
CMSWorkset = null;
}
On test debugging when i press step forward and reach this line : 在测试调试中,当我按前进并到达此行时:
CMSWorkset = new CPLTWorkset() as ICMSDWorkset;
the debugger stop working, the test continue running but remains stuck on that line .No errors or exceptions are throw . 调试器停止工作,测试继续运行,但仍停留在该行上。不会引发任何错误或异常。
CPLTWorkset
is a wrapper for a COM object. CPLTWorkset
是COM对象的包装器。
I checked 'Native code debugging' as other mentions on questions related to COM objects but without success. 我检查了“本机代码调试”以及其他与COM对象有关的问题,但没有成功。
Did anyone have any idea why debugger can't handle that line ? 有谁知道为什么调试器不能处理那条线? or why that piece of cod work on main but not on tests ?
还是为什么那条鳕鱼主要用于测试而不起作用?
Thanks! 谢谢!
My first guess is that the code invoked by new CPLTWorkset()
only works on a thread that is marked as a [STAThread]
. 我的第一个猜测是,
new CPLTWorkset()
调用的代码仅适用于标记为[STAThread]
的线程。 Your Main method has this attribute, making the main thread a single threaded apartment thread, while the threads used by your unit testing framework are not. 您的Main方法具有此属性,使主线程成为单线程单元线程,而单元测试框架使用的线程则没有。
Note that just calling Thread.SetAparatmentState may not fix this; 注意,仅调用Thread.SetAparatmentState可能无法解决此问题。 when a thread is an STA thread, this also implies that an event loop is being pumped on it (eg in WinForms, Application.Run would be called).
当一个线程是STA线程时,这也意味着一个事件循环正在其上被泵送(例如,在WinForms中,将调用Application.Run )。 That is going to be difficult to achieve in a unit test unless you really know what you are doing.
除非您真的知道自己在做什么,否则在单元测试中很难做到这一点。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.