3

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?

jordan
  • 3,436
  • 11
  • 44
  • 75
  • Just because the function comes within an `Expect` clause doesn't mean it won't run normally. When the code hits the expect line, it goes into the function, and if m_list1 is null you get your null reference. – Phillip Schmidt Nov 30 '12 at 17:31
  • make sure networkid matches on calls – AD.Net Nov 30 '12 at 17:33
  • then whats the point of having expect? i want to bypass the function being called because the lists will always be null in my tests. – jordan Nov 30 '12 at 17:34
  • can you post the stack trace? – Gabe Moothart Nov 30 '12 at 17:37
  • @GabeMoothart this is in a unit test, so it will be small, but yes i can post it – jordan Nov 30 '12 at 17:38
  • Almost all cases of NullReferenceException are the same. Please see "[What is a NullReferenceException in .NET?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-in-net)" for some hints. – John Saunders Nov 30 '12 at 18:07
  • 1
    yea i know why its a null reference, try reading the question again – jordan Nov 30 '12 at 18:15

1 Answers1

6

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?

Community
  • 1
  • 1
Tallek
  • 1,575
  • 15
  • 22