1

I have seen a lot posts explaining how to use NMock to expect an exception. But that is not what i want to know. In my case, i am testing the happy path. But looks like the NMock is throwing exceptions as long as the method called on the mock is called within a try/catch. So suppose I have a method in the class i am testing:

class MyClass
{
    public MyClass(some_type obj)
    {
        m_member = obj;
    }

    public void Func()
    {
        try
        {
            m_member.some_function()
        }
        catch (System.Exception e)
        {
            // do something
        }
    }

    private some_type m_member;
}

In the unit test for this class, I have a test for this Func in MyClass to test the happy path:

[Test]
public void TestFunc()
{
    MockFactory mock_factory = new MockFactory();
    Mock<some_type> mock = mock_facoty.CreateMock<some_type>();
    MyClass uut = new MyClass(); 

    mock.Expects.One.MethodWith(_ => _.some_function());
    uut.Func();

    mock_facoty.VerifyAllExpectationsHaveBeenMet();
    mock_facoty.ClearExpectations();
}

This unit test keeps failing. If I remove the try/catch in the code and just do (line 8 - 18):

    public void Func()
    {
        //try
        //{
            m_member.some_function()
        //}
        //catch (System.Exception e)
        //{
        //    // do something
        //}
    }

This test will work fine.

Does anyone have an idea as to why is this happening? and how could I make this work? Thank you very much!

user1469452
  • 39
  • 1
  • 4
  • 1
    Please reformat your code without number of lines.. – Soner Gönül Apr 22 '14 at 20:19
  • Thanks for reminding. I have reformat it. – user1469452 Apr 22 '14 at 20:23
  • This is far from a working example so I'm going to have to guess: what details are in the exception which is thrown? – ClickRick Apr 23 '14 at 18:04
  • It would help enormously if the code sample you post would compile. For example, you refer to `some_type` but don't tell us anything about it, you declare `mock_factory` but then use `mock_facoty`, and you call `m_member.some_function()` but it doesn't compile because of a missing semi-colon. What else have you not told us that we would need in order to be able to reproduce your problem? – ClickRick Apr 23 '14 at 18:07
  • Hi ClickRick, after i saw your comment, i started to start over with my code so that i can provide accurate codes that reproduces this problem. But somehow, as I re-do this in baby steps, it just worked. So now i am confused as to why it has been failing in the first place. I will follow up with this post if I figured out what i did wrong the first time. Or it might be just some stupid mistake that i made. Anyway, thanks for all of you trying to help! – user1469452 Apr 23 '14 at 19:23

1 Answers1

1

You are not actually using your mocked object, you are creating the concrete (real) some_type object in your MyClass constructor with the new statement.

I would suggest changing your MyClass class to accept the some_type object as a constructor parameter:

public MyClass(some_type thetype)
{
    m_member = thetype;
}

Then, in your test method, pass the mocked object into the ctor:

MockFactory mock_factory = new MockFactory();
Mock<some_type> mock = mock_factory.CreateMock<some_type>();
MyClass uut = new MyClass(mock.MockObject);

That will mean that your MyClass instance will actually use the mocked object, and then you can verify against it...

If you can't change your constructor to have a required parameter all the time, you could use a poor man's dependency injection (not recommended but might be necessary):

public MyClass() : this(new some_type()) {}

public MyClass(some_type thetype)
{
    m_member = thetype;
}
Simon C
  • 9,458
  • 3
  • 36
  • 55
  • You are right about the constructor. I actually am passing in an object of some_type to initialize the member. Sorry i didn't get that part right in my post. I have corrected it. But although i am doing that, the problem described above still exists. Any idea why..? – user1469452 Apr 23 '14 at 17:14