문제

이것은 내 서비스 클래스입니다 :

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.
  }
}
.

resharper 5 테스트 러너를 사용하여 CanGetSomethingCanGetSomethingElse를 별도로 실행합니다. 두 테스트가 모두 잘 작동합니다.그러나 전체 클래스를 실행하여 Tests를 실행하여 첫 번째 메소드가 전달되는 첫 번째 메소드와 두 번째 메소드가 예외를 Throw하는 두 번째 메소드가 를 던지게합니다.

System.ObjectDisposedException : 닫힌 텍스트 작성자에 쓸 수 없습니다.

두 번째 테스트에서 호출하는 방법을 중요하지 않은 것으로 보이는 것으로 보이지 않는 것으로 보이는 것으로 보이는 것으로 보이는 것으로 보이는 것으로 보이지 않는 것으로 보이는 것으로 보입니다.댓글을 지정하면 _db가 예외를 제거하고 잘 작동합니다.

오류를 기록하고 한 번에 전체 테스트 클래스를 실행할 수 있지만 왜 이런 이유로 작동하는지 알 수 없습니다.

아이디어가 있습니까?

도움이 되었습니까?

해결책

Dont share members of your test class! Not knowing resharper, but VS 2010 UnitTesting is really kean on cleaning up after running a test. My assumption for what happens here is when constructing your TestClass, your context is being initialized. And after running a single test. Resharper cleans up the resources, therefore also disposing your context.

Use a TestInitialize to create a new instance of your dataContext, therefore isolating your tests from one another

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top