【问题标题】:Rhino Mocks / Repository testing - NUnit test suite fails, but single mocked unit test passes?Rhino Mocks / Repository testing - NUnit 测试套件失败,但单个模拟单元测试通过?
【发布时间】:2011-11-15 15:15:59
【问题描述】:

根据我之前的问题:Rhino Mocks - Testing Repository layer returns "object reference not set to instance" error

当 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 是否被持久保存在我不希望它存在的地方?

感谢您的帮助!

【问题讨论】:

    标签: c# nunit rhino-mocks


    【解决方案1】:

    TestFixtureSetup 和 TestFixtureTearDown 每个夹具运行一次。也就是说,只能有一种方法标记为TestFixtureSetup,并且它只运行一次,并且在夹具中的所有测试运行之前。同样,只能有一种方法标记为TestFixtureTearDown,并且该方法只运行一次,并且在夹具中的所有测试都运行之后。因此,您的变量_dataContext 在方法TestFixtureSetup 中被初始化一次,直到TestFixtureTearDown 才被释放。真的很难说这是否是你想要的。

    如果您希望在每个测试之前和之后单独运行设置和拆除方法,请使用属性Setup 和TearDown。

    【讨论】:

    • 噢!感谢您及时回复。那是我的一个错字。诚实:)
    猜你喜欢
    • 2023-04-01
    • 2011-07-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多