Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6807523
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T19:50:33+00:00 2026-05-26T19:50:33+00:00

As per my previous question: Rhino Mocks – Testing Repository layer returns "object reference

  • 0

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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T19:50:34+00:00Added an answer on May 26, 2026 at 7:50 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm implementing a table per subclass design I discussed in a previous question .
So as per a previous question of mine I've decided to start a website
Per an answer to a previous question (answer here: SQL/Database Views in Grails ),
Per my previous question , I wish that a boost::shared_ptr<A> was actually a subclass
As per my previous question , I need to redirect an HTTP POST request
Guys ! I understood that my previous question did not meant to you guys
This is a follow up on my previous question . I set up the
As per a previous question I've run into a bit of a bug laying
As per a previous question I'm setting up the HMVC extension for codeigniter. https://bitbucket.org/wiredesignz/codeigniter-modular-extensions-hmvc/wiki/Home
This is similar to my previous question, but that solution did not solve this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.