Reputation: 731
As per my previous question: Rhino Mocks - Testing Repository layer returns "object reference not set to instance" error
I am having an issue when passing the NUnit test when it is run in conjunction with the other tests in the suite.
The entire test class is as follows:
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());
}
}
}
The test which has now been fixed (or rather my understanding has been fixed) passes when run on its own. But not when the tests are run together? Am i missing something in the SetUp / TearDown features of NUnit? Is the dataContext or the Repository being persisted where i want it not to be?
Thanks for any help!
Upvotes: 3
Views: 1576
Reputation: 241591
TestFixtureSetup
and TestFixtureTearDown
are run once per fixture. That is, there can be only one method marked TestFixtureSetup
and it is run exactly once and before all the tests in the fixture are run. Similarly, there can be only one method marked TestFixtureTearDown
and that method is run exactly once and after all the tests in the fixture are run. Therefore, your variable _dataContext
is initialzed once in the method TestFixtureSetup
and is not disposed until TestFixtureTearDown
. It's really hard to tell if this is what you intend or not.
If you want setup and tear down methods that are run before and after each test individually, use the attributes Setup
and TearDown
.
Upvotes: 4