2011-11-15 63 views
3

按照我之前的問題:Rhino Mocks - Testing Repository layer returns "object reference not set to instance" errorRhino Mocks/Repository測試 - NUnit測試套件失敗,但單個模擬單元測試通過?

我在通過NUnit測試時,它與套件中的其他測試一起運行時遇到問題。

整個測試類如下:

using System; 
using System.Linq; 
using System.Linq.Expressions; 
using NUnit.Framework; 
using System.Collections.Generic; 
using Rhino.Mocks; 
using Assert = NUnit.Framework.Assert; 
Tests.DAO 
{ 
    /// <summary> 
    /// Uses the 3A method of Unit Testing; Arrange, Act, Assert. 
    /// 
    /// Tests the Billing DAO 
    /// </summary> 
    [TestFixture] 
    public class BillingTests 
    { 
     private IDataContextWrapper _dataContext; 
     private Repository _intRepository; 

     /// <summary> 
     /// Sets up the following constructs for testing. 
     /// 
     /// - DataContext 
     /// - InternalRepository 
     /// - Billing 
     /// </summary> 
     [TestFixtureSetUp] 
     public void TestFixtureSetup() 
     { 
      _dataContext = MockRepository.GenerateMock<IDataContextWrapper>(); 
     } 

     /// <summary> 
     /// 
     /// </summary> 
     [TestFixtureTearDown] 
     public void TestFixtureTearDown() 
     { 
      _dataContext.Dispose(); 
     } 

     /// <summary> 
     /// Tests adding a Billing object to the Database. 
     /// </summary> 
     [Test] 
     public void Add() 
     { 
      // Arrange 
      var billing = new Billing(); 
      _intRepository = new Repository(_dataContext); 

      // Act 
      _intRepository.AddRecord(billing); 

      // Assert 
      _dataContext.AssertWasCalled(x => x.InsertOnSubmit(billing)); 
      _dataContext.AssertWasCalled(x => x.SubmitChanges()); 
     } 

     /// <summary> 
     /// The test attempts to remove the Billing before asserting that it no-longer 
     /// exists in the database by attempting to delete it again. 
     /// </summary> 
     [Test] 
     public void Delete() 
     { 
      // Arrange 
      var billing = new Billing(); 
      _intRepository = new Repository(_dataContext); 

      // Arrange 
      _intRepository.DeleteRecord(billing); 

      // Assert 
      _dataContext.AssertWasCalled(x => x.DeleteOnSubmit(billing)); 
      _dataContext.AssertWasCalled(x => x.SubmitChanges()); 
     } 

     /// <summary> 
     /// The test retrieves the Billing from 
     /// the database and asserts that 
     /// the original Billing and 
     /// the one retrieved are the same. 
     /// </summary> 
     [Test] 
     public void GetRecordWhere() 
     { 
      // Arrange 
      var list = new List<Billing> {new Billing {BillingId = 1}}; 
      const int testId = 1; 

      _dataContext.Stub(x => x.GetTable<Billing>()).Return(list.AsQueryable()); 
      _intRepository = new Repository(_dataContext); 

      // Act 
      var result = _intRepository.GetRecordWhere<Billing>(x => x.BillingId == testId); 

      // Assert 
      Assert.IsNotNull(result); 
      Assert.AreEqual(result.BillingId, testId); 
      _dataContext.AssertWasCalled(x => x.GetTable<Billing>()); 
     } 

     /// <summary> 
     /// 
     /// </summary> 
     [Test] 
     public void GetAllRecordsWhere() 
     { 

     } 

     /// <summary> 
     /// Retrieves the total number of Billings in the database 
     /// and compares it against how many were added by the testFixture. 
     /// </summary> 
     [Test] 
     public void GetAllBillings() 
     { 
      // Arrange 
      _dataContext.Stub(x => x.GetTable<Billing>()).Return(new List<Billing> { new Billing { BillingId = 1 } }.AsQueryable()); 
      _intRepository = new Repository(_dataContext); 

      // Act 
      var result = _intRepository.GetAllRecords<Billing>(); 

      // Assert 
      Assert.AreEqual(typeof(EnumerableQuery<Billing>), result.GetType()); 
      _dataContext.AssertWasCalled(x => x.GetTable<Billing>()); 
     } 

     /// <summary> 
     /// Tests find all Billings. Expects the return type to be of IQeryable 
     /// </summary> 
     [Test] 
     public void FindAllBillings() 
     { 
      // Arrange 
      _dataContext.Stub(x => x.GetTable<Billing>()).Return(new List<Billing>().AsQueryable()); 
      _intRepository = new Repository(_dataContext); 

      // Act 
      var result = _intRepository.FindAll<Billing>(); 

      // Assert 
      Assert.IsNotNull(result); 
      Assert.AreEqual(typeof(EnumerableQuery<Billing>), result.GetType()); 
     } 
    } 
} 

,當自身的運行已得到修復(或者更確切地說,我的理解已定)通過測試。但是當測試一起運行時不行嗎?我在NUnit的SetUp/TearDown功能中丟失了什麼? dataContext或Repository是否在我不想要的地方持續存在?

感謝您的幫助!

回答

4

TestFixtureSetupTestFixtureTearDown每個燈具運行一次。也就是說,只能有一種方法標記爲TestFixtureSetup,並且在夾具中的所有測試都運行之前,它只運行一次。同樣,只有一個標記爲TestFixtureTearDown的方法,並且該方法只運行一次,並且在燈具中的所有測試都運行之後。因此,您的變量_dataContext在方法TestFixtureSetup中初始化一次,並且直到TestFixtureTearDown才被處理。真的很難說這是否是你的意圖。

如果要分別設置和拆卸每個測試之前和之後運行的方法,請使用SetupTearDown屬性。

+0

Doh!感謝你及時的答覆。這是我的一個錯字。誠實 :) – M05Pr1mty