This is a migrated thread and some comments may be shown as answers.

multiple method calls to a mocked object throws and exception

4 Answers 125 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Freddy
Top achievements
Rank 1
Freddy asked on 22 Dec 2014, 11:26 AM
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?

4 Answers, 1 is accepted

Sort by
0
Stefan
Telerik team
answered on 22 Dec 2014, 12:30 PM
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.

 
0
Freddy
Top achievements
Rank 1
answered on 22 Dec 2014, 05:39 PM
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
0
Accepted
Stefan
Telerik team
answered on 24 Dec 2014, 06:45 AM
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.

 
0
Freddy
Top achievements
Rank 1
answered on 24 Dec 2014, 09:38 AM
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.
Tags
General Discussions
Asked by
Freddy
Top achievements
Rank 1
Answers by
Stefan
Telerik team
Freddy
Top achievements
Rank 1
Share this question
or