Question

I am receiving an exception: "object reference not set to an instance of an object".

I am trying to evaluate if Location and Manufacturing classes method ResetAllProperties() are executed.

What em I doing wrong?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.VisualStudio.TestTools.UnitTesting;

using Rhino.Mocks;

namespace Test
{
    public class Engine
    {
        public Engine() { }

        public EngineStatus Status { get; internal set; }
        public virtual EngineLocation Location { get; set; }
        public virtual EngineManufacturing Manufacturing { get; set; }
    }

    public abstract class EngineStatus
    {
        protected readonly Engine engine = null;

        public EngineStatus(Engine engine)
        {
            this.engine = engine;
        }

        public abstract void ResetAllProperties();
    }

    public class FirstEngineStatus : EngineStatus
    {
        public FirstEngineStatus(Engine engine) : base(engine) { }

        public override void ResetAllProperties()
        {
            this.engine.Location.ResetAllProperties();
            this.engine.Manufacturing.ResetAllProperties();
        }
    }

    public class EngineLocation
    {
        public string CustomerName { get; set; }

        public virtual EngineManufacturing Manufacturing { get; set; }
        public virtual Engine Engine { get; set; }

        public void ResetAllProperties()
        {
            this.CustomerName = null;
        }
    }

    public class EngineManufacturing
    {
        public Nullable<DateTime> EntryDate { get; set; }

        public virtual EngineLocation Location { get; set; }
        public virtual Engine Engine { get; set; }

        public void ResetAllProperties()
        {
            this.EntryDate = null;
        }
    }

    [TestClass]
    public class Test
    {
        [TestMethod]
        public void ResetAllProperties_AssertWasCalled()
        {
            // Arrange
            var engine = MockRepository.GenerateMock<Engine>();
            var status = MockRepository.GeneratePartialMock<FirstEngineStatus>(engine);

            engine.Stub(action => action.Location.ResetAllProperties());
            engine.Stub(action => action.Manufacturing.ResetAllProperties());

            // Act
            status.ResetAllProperties();

            // Assert
            engine.AssertWasCalled(action => action.Location.ResetAllProperties());
            engine.AssertWasCalled(action => action.Manufacturing.ResetAllProperties());
        }
    }
}
Was it helpful?

Solution

You are asserting the behaviour of Location and Manufacturing, so these are the objects which should be mocked. Also, when checking that something happens use Expects not Stub. Everything else should be concrete. If you make ResetAllProperties methods virtual then the following works:

[TestMethod]
public void ResetAllProperties_AssertWasCalled()
{
    var location = MockRepository.GeneratePartialMock<EngineLocation>();
    var manufacturing = MockRepository.GeneratePartialMock<EngineManufacturing>();

    // Arrange
    var engine = new Engine
    {
        Location = location,
        Manufacturing = manufacturing
    };
    var status = new FirstEngineStatus(engine);
    location.Expect(action => action.ResetAllProperties());
    manufacturing.Expect(action => action.ResetAllProperties());

    // Act
    status.ResetAllProperties();

    // Assert
    location.VerifyAllExpectations();
    manufacturing.VerifyAllExpectations();
}

However, this seems like you are testing the implementation rather than the functionality. What do you actually want to test? It looks to me like you want to test that CustomerName and EntryDate are set to null. You can test this without using any mocking at all as follows:

[TestMethod]
public void ResetAllProperties_AssertWasCalled()
{
    // Arrange
    var engine = new Engine
    {
        Location = new EngineLocation { CustomerName = "Dzenan" },
        Manufacturing = new EngineManufacturing { EntryDate = DateTime.Today }
    };
    var status = new FirstEngineStatus(engine);

    // Act
    status.ResetAllProperties();

    // Assert
    Assert.IsNull(engine.Location.CustomerName);
    Assert.IsNull(engine.Manufacturing.EntryDate);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top