ayla
ayla

Reputation: 3546

Why am i getting a null reference exception on a mocked test object?

I have the following

IdleClass idleSend = MockRepository.GenerateMock<IdleClass >();
SpeedClass speedSend = MockRepository.GenerateMock<SpeeClass >();
idleSend.Expect(x => x.IsUnitInSystem(networkID)).Return(true).Repeat.Any();
speedSend.Expect(x => x.IsUnitInSystem(networkID)).Return(true).Repeat.Any();

and The original IsUnitInSystem(networkID) function (in a subclass for IdleClass and SpeedClass) is below:

public bool IsUnitInSystem(string networkID)
        {
            DateTime outVal = default(DateTime);
            return m_list1.DoesListContainUnit(networkID) ||
                   m_list2.TryGetValue(networkID, out outVal) ||
                   m_list3.TryGetValue(networkID, out outVal) ||
                   m_list4.TryGetValue(networkID, out outVal) ||
                   m_list5.TryGetValue(networkID, out outVal);
        }

I am reviving a System.NullReferenceException on the return line in the IsUnitInSystem function.

Stack Trace:

IdleClass.IsUnitInSystem(String networkID)
PendingTest.<ResendClassTest>b__1b(IdleClass x)
RhinoMocksExtension.Expect[T,R](T mock, Function'2 action)
PendingTest.ResendClassTest()

What i don't understand is why and how the program is even getting into the function when i have specified a .Expect functionality for the function?

Upvotes: 3

Views: 2627

Answers (1)

Tallek
Tallek

Reputation: 1575

The method you are trying to mock must be able to be implemented by the mock object, meaning it must be marked as virtual if you are mocking a concrete class.

Take a look at the following post for some more info: Rhino Mocks stubs and mocks are only good for interfaces?

Upvotes: 6

Related Questions