multiple method calls to a mocked object throws and exception

5 posts, 1 answers
  1. Freddy
    Freddy avatar
    3 posts
    Member since:
    Oct 2014

    Posted 22 Dec 2014 Link to this post

    Hi,
    I am trying to mock db access using JustMock. I do have an interface defined and created a mock object from the interface. I then arranged a method to return false for any argument. After arranging the mock object on the second call with unique arguments a null reference exception is thrown. The code snippet below shows what I am trying to do:

    IDBAccess fakeDBAccess = Mock.Create<IDBAccess>();
    List<ModelType1> fakeModelType1 = GetFakeModelModelType1();
    Mock.Arrange(()=> fakeDBAccess.GetModelType1ForPrimaryKey(Arg.Is<long>(1))).Returns(fakeModelType1[0]);
    Mock.Arrange(() => fakeDBAccess.GetModelType1ForPrimaryKey(Arg.Is<long>(2))).Returns(fakeModelType1[1]);
    List<fakeModelType2> fakeModelType2 = new List<fakeModelType2>();
    Mock.Arrange(() => fakeDBAccess.GetModelType2ForPrimaryKey(Arg.IsAny<long>())).Returns(fakeModelType2);
    Mock.Arrange(() => fakeDBAccess.GetModelType1Status(Arg.IsAny<ModelType1>())).Returns(false);
    ClassUnderTest OUT = new ClassUnderTest(fakeDBAccess );

    On debugging the above code try to call fakeDBAccess.GetModelType1Status() with two different values say fakeModelType[0] and fakeModelType[1] (in the watch window) the first call succeeds and returns false but the second call fails with a null reference exception. The order of calls do not matter. It is always  the second call that throws the null reference exception. Can you please confirm whether this is a bug or not. If this is a bug can you please suggest a work around?
  2. Stefan
    Admin
    Stefan avatar
    198 posts

    Posted 22 Dec 2014 Link to this post

    Hello Freddy,

    I don't see anything wrong with your test code as it is. Could you provide us with a bit more information? I need at least the full stack trace of the null reference exception. Ideally, if you could provide us with a self-contained project reproducing the issue or a snippet that I can plug into my solution, that would be great!

    Regards,
    Stefan
    Telerik
     

    Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

     
  3. DevCraft R3 2016 release webinar banner
  4. Freddy
    Freddy avatar
    3 posts
    Member since:
    Oct 2014

    Posted 22 Dec 2014 in reply to Stefan Link to this post

    I did follow your direction and found that the null reference was caused due to an error in initializing the model in my code. I have enclosed a small project that shows how the error was caused. It seems that even when I set a method to return a value for any argument of a particular type JustMock tries to do a comparison with the argument used previously, this comparison would return a null reference if any of the fields are null or not initialized for the purpose of the test. Is this comparison really necessary as it was a very big gotcha for me(as I assumed that the types are all that have to be compared). Can you please tell me if I am missing something because if we have models with a large number of fields it would be useless to initialize each field although it is never used.
    P.S The attached file is a zip file I had to change the extension as the site was not allowing zip files
  5. Answer
    Stefan
    Admin
    Stefan avatar
    198 posts

    Posted 24 Dec 2014 Link to this post

    Hi Freddy,

    The part of JustMock that calls to Equals here is the one that counts occurrences. JustMock records what calls were made and with what arguments, so that you can later use Mock.Assert() to check occurrences.

    It is not possible to work around this behavior and your best course of action is to fix the ModelType.Equals method, maybe to use the static Object.Equals which accounts for null references correctly.

    Regards,
    Stefan
    Telerik
     

    Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

     
  6. Freddy
    Freddy avatar
    3 posts
    Member since:
    Oct 2014

    Posted 24 Dec 2014 in reply to Stefan Link to this post

    Thanks for the clarification, I missed the part where mocks do record calls with arguments that were used. I will fix the modelType.Equals as it does not make sense to work around the issue.
Back to Top
DevCraft R3 2016 release webinar banner