在单元测试SerializationExceptions [英] SerializationExceptions in UnitTests

查看:206
本文介绍了在单元测试SerializationExceptions的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我的Visual Studio(2010)单元测试的麻烦。

I am having trouble with my Visual Studio (2010) Unit Tests.

每当发生任何问题,一个类型的异常被抛出,UnitTestAdapter抛出SerializationExceptions,告诉我他无法反序列化抛出的异常。

Whenever anything goes wrong and an exception of one type is thrown, the UnitTestAdapter throws SerializationExceptions, telling me he can't deserialize the Exceptions thrown.

在我创建单元测试,这些大多为 System.Data.SqlServerCe.SqlCeException NHibernate.MappingException

In the Unit Tests I created, these are mostly either System.Data.SqlServerCe.SqlCeException or NHibernate.MappingException.

我想不通为什么会这样。研究告诉我,最常见的原因是,这些类型驻留大会不提供给UnitTestAdapter,但是当我直视TestResults文件夹中,我看到每一个需要的组件,包括 System.Data这。.SqlServerCe.dll Nhibernate.dll

I cannot figure out why this is happening. Research told me, that the most common reason is that the Assemblies where these types reside is not available to the UnitTestAdapter, but when I look into the TestResults-folder, I see every assembly needed, including System.Data.SqlServerCe.dll and Nhibernate.dll.

所以 - 我该怎么办调试这个?我需要看到异常的测试结果让他们以任何方式有用的。

So - what can I do to debug this? I need to see the exceptions in the test results for them to be useful in any way.

推荐答案

好了,经过一个良好的金额浪费工时,我偶然发现了解决方案。或多或少...

Well, after a good amount of man-hours wasted, I stumbled upon the solution. More or less...

这个帖子指着我的方向,更具体报价

This post pointed me in the direction, more specifically the quote

要回顾一下,在MyCustomException测试执行的非常早期的阶段被抛出。在包含它尚未加载,这样,单元测试适配器确实DLL不能达到它。

To recap, MyCustomException was thrown at very early stage of test execution. The dll that contained it was not loaded yet so Unit Test Adapter indeed could not reach it.

我用一个自定义的基类初始化我的单元测试需要的数据库连接。这个基类有一个正常constructur ,做的不可以 ClassInitialize ,因为 [ClassInitialize]当它在子类中不被UnitTestAdapter名为

I use a custom base class to initialize my UnitTests that need database connections. This base class has a normal constructur that does not have ClassInitialize, because [ClassInitialize] is not called by the UnitTestAdapter when it's in subclasses:

public DatabaseAware()
{
    XmlConfigurator.Configure();

    _logger.Info("Configuring NHibernate");
    _configuration = new Configuration().Configure();
    _sessionFactory = _configuration.BuildSessionFactory();
}



因此,在调用构造函数,如果有什么不顺心,一切的执行显然暂停。

So, upon invoking the constructor, if anything goes wrong, the execution of everything is apparently halted.

奇怪的是,这的包括组件装载的。因此,由当时我构造函数抛出异常,大会为 NHibernate的尚未加载,导致类型未解决的成员NHibernate的。 HibernateException的消息。

Curiously enough, this includes loading assemblies. So, by the time my constructor threw an Exception, the Assembly for NHibernate was not yet loaded, resulting in a Type is not resolved for member NHibernate.HibernateException message.

什么一程。基本上,所有我现在所做的是在构造函数中引入一个异常处理失败,如果有什么错了:

What a ride. Basically, all I did now is to introduce an exception handling in the constructor to fail if anything goes wrong:

public DatabaseAware()
{
    XmlConfigurator.Configure();

    try
    {
        _logger.Info("Configuring NHibernate");
        _configuration = new Configuration().Configure();
        _sessionFactory = _configuration.BuildSessionFactory();
    }
    catch (Exception ex)
    {
        _logger.Fatal(ex);
        Assert.Fail(ex.Message);
    }
}

现在,我可以像以前一样运行测试,没有任何问题。 (或者,至少,当在构造函数中出现问题,我会通知它)。

Now, I can run the tests as before without any problems. (Or, at least, when a problem occurs in the constructor, I will be notified of it).

这篇关于在单元测试SerializationExceptions的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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