这是我的服务类:
public class MyService
{
private readonly MyDataContext _db;
public MyService()
{
_db = new MyDataContext(GetConnectionString());
#if DEBUG
_db.Log = Console.Error;
#endif
}
public void Get(int id)
{
return from item in _db.Items
where item.Id == id
select item;
}
}
这是我的测试课
[TestClass]
public class MyServiceTest
{
private MyService _service = new MyService();
[TestMethod]
public void CanGetSomething()
{
var something = _service.Get(1).ToList();
// Asserts
}
[TestMethod]
public void CanGetSomethingElse()
{
var somethingElse = _service.Get(2).ToList();
// Commented out everything else.
}
}
运行CanGetSomething
和CanGetSomethingElse
单独使用 ReSharper 5 测试运行器工作正常,两个测试都通过了。但是,尝试通过运行整个类来连续运行测试会导致第一个方法通过,第二个方法抛出异常
System.ObjectDisposedException:无法写入已关闭的 TextWriter。
我在第二个测试中调用什么方法似乎并不重要,任何调用_db
都会导致错误。注释掉_db.Log = Console.Error
可以消除异常,它会正常工作。
我希望能够记录错误并立即运行一整类测试,但我无法弄清楚它为什么会这样。
有任何想法吗?