根据我之前的问题:Rhino Mocks - Testing Repository layer returns "object reference not set to instance" error
当它与套件中的其他测试一起运行时,在传递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是否存在于我希望不存在的位置?
感谢您的帮助!
答案 0 :(得分:4)
TestFixtureSetup
和TestFixtureTearDown
每个灯具运行一次。也就是说,只有一个标记为TestFixtureSetup
的方法,它只运行一次,然后才运行夹具中的所有测试。类似地,只有一个标记为TestFixtureTearDown
的方法,该方法只运行一次,然后运行夹具中的所有测试。因此,您的变量_dataContext
会在方法TestFixtureSetup
中初始化一次,并且在TestFixtureTearDown
之前不会被处理。很难说这是否是你想要的。
如果您想要单独设置和拆除每个测试之前和之后运行的方法,请使用属性Setup
和TearDown
。