We are trying to write an Add() and Delete() test with JustMock against the Telerik ORM our gets and updates work fine but not sure what we are missing as we cannot get Adds and Deletes to work.
This is what we have:
In the above test method we want to delete an item from the collection but nothing happens and the last assert test will fail.
Do we need to Mock the Delete Method on emIRIS to make the delete work? if so how.
And for adding new objects to the collections do we need to Moch the Add method on each collection?
13 Answers, 1 is accepted
I assume that you're trying to test the emIRIS class. If so, then you should not create a mock of it, but instead use the actual class (as in "new emIRIS()") in your test. JustMock can then be used to mock the dependencies of the class under test, e.g. the underlying database connection or database context.
In general you should never mock the class under test, unless the dependencies are hard coded in it. Even then, you should use either partial mocking or a mock with Behavior.CallOriginal.
ok, I am not quite sure I understand so I lose emIRIS = Mock.Create<emIRIS>();
But keep my Mock.Arrange statements correct?
What I had above worked great for GETs and UPDATEs but just not ADDs and DELETEs with the ORM.
Most of our GET/ADD/DELETE Methods are in extended classes in the ORM.
Conceptually, JustMock allows you to replace the dependencies of a system with test doubles (mocks) so that you can test that system in isolation (isolated from the file system, database, etc.)
In your case the system under test is the emIRIS class. The dependency that needs to be mocked is the database connection. You use Mock.Create and Mock.Arrange to create instances representing the dependencies and configure their behavior. In any case, mocking is used to replace the original behavior of a component with some mock behavior suitable for testing. There's no point in replacing the piece of code you're trying to test with a mock implementation, because then you'll just be testing the mock implementation itself instead of the original code, which will lead you nowhere.
Depending on the way the emIRIS class is constructed there are different ways to mock out the database connection. I will have to look at the emIRIS class constructor to give you better advice.
I wasn't aware that emIRIS is an auto-generated class. In that case it is likely the correct type to mock.
However, I see that your DELETE test not only arranges and asserts the mock, which is correct to do, but also acts on the mock. That doesn't make sense. The act part of the test should act upon the system under test. In your case there is no system under test, only a mock. Essentially your test is testing that the mocking framework works.
the example of what I gave is not a real test, but an example of the delete not working.
I would expect in the example above for the data that I did the Mock.Arrange for I would expect my data to have one less record in the collection.
If I am doing this wrong can you give me an example how to do the add and delete correctly with the Telerik ORM. If you need to open this as a ticket I am ok with that.
What my colleague meant was that instead of faking the whole class in which the Delete and Add methods reside, you need to isolate it from its dependencies and act against its original implementation. After all, this is what you would like to test. For example, for the emIRIS class you will need to first mock the database, arranging it to return a predefined list of items for example. Then, you would need to execute the original logic of the Delete method, by calling it in the test and finally, assert that the database has changed as expected (this is one item less).
As for the sample, we might be able to provide such, but for that purpose we will require a sample project, showing how the connection to the DB is implemented. Please, feel free to share a runnable sample project on the matter and we will try to provide a more specific solution.
[quote]emIRIS class you will need to first mock the database, arranging it to return a predefined list of items for example. Then, you would need to execute the original logic of the Delete method, by calling it in the test and finally, assert that the database has changed as expected (this is one item less).[/quote]
I think that's what my example above was doing.
Anyways I will build a sample project and send it to you.
We are looking forward the sample project. Thank you for the help once again.
I apologize for not providing the necessary information about this in my previous reply.
The easiest way would be to open a JustMock support ticket and attach the sample as an archive there. Still, you should keep in mind that our system will only let you to attach the file if it is under 20mb in size.
Another approach would also be to use a 3rd party sharing service like DropBox for example.
I hope this helps.