Results 1 to 2 of 2

Thread: Need some help to understand mocking (nsubstitute)

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    May 2002
    Posts
    1,602

    Need some help to understand mocking (nsubstitute)

    Hi!

    I have just started digging into some code developed by a previous employee, and now I am reviewing unit tests. They ahve used something I am not familiar with, namely nsubstitute for mocking. I guess I am having trouble understanding the concept of substituting stuff, rather than the framework itself, so let me explain the question:

    They have a test method and class like like


    IJobManager jobManagerMock;
    JobStart obj;
    IStartJobCommand cmd;

    [TestMethod]
    public void NoJobRunning()
    {

    }



    The method then creates a fake object like

    obj.JobId = 1;
    obj.UserId = "12345";
    obj.StartTime = DateTime.Now;

    Then there is a definition like:

    jobManagerMock.GetRunningJob(1).Returns(0);


    The above code I guess is a "mock" for the actual implementation of the jobManager.GetRunning Job method. What it says is "If the input parameter is 1 I return 0. Plain and simple right?

    After that they execute the job object like

    cmd.Execute(obj);

    That call executes some stuff...

    After the call there is the statement that confuses me the most:

    jobManagerMock.ReceivedWithAnyArgs(1).GetRunningJob(obj.JobId);
    Assert.IsFalse(!obj.JobAccepted);


    Granted, I have not been doing uni testing for very long, but I am having a hard time understanding what is actually being tested? I understand that a mock substitute is useful to simulate the createn of test data, wihtout having to actualy write test tlasses, just directly on the fly specify, for this input, return this response. That is neat. But the above code coesn't make senes to me. I write substitute command for a method that returns 0 if the input is 1, then some lines later I verify something? What do I actually verify, and what is the point of this test?

    The code of "Assert.IsFalse" makes perfect sense from a testing perspective. If the jobaccepted is false, it means that an exception occured in the Execute method, meaning the code failed for some reason.

    Maybe I can't get my head around this abstract way of thinking when working with substitutes...?


    cheers!
    H

  2. #2
    PowerPoster Evil_Giraffe's Avatar
    Join Date
    Aug 2002
    Location
    Suffolk, UK
    Posts
    2,555

    Re: Need some help to understand mocking (nsubstitute)

    The mocks will record what calls are made against them, and then let you verify what occured. So in this case, we are verifying that something called your instance of IJobManager's GetRunningJob method, without caring whether the arguments received in the test match those specified here (that looks like a bug in the test).

    To be honest, I don't think this is necessarily a very good unit test, but having a quick look at the nSubstitute library pages, they are intentionally obscuring the difference between a mock and a stub, which is a kinda critical difference to understand to unit test effectively, IMO. They are different beasts and should be used at different times.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width