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

JustMock Telerik ORM Add and Delete

13 Answers 118 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Andrew
Top achievements
Rank 1
Veteran
Iron
Andrew asked on 02 Jun 2015, 05:02 PM

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:

[TestInitialize]
[Description("Test Initialize")]
public void TestInitialize()
{
    emIRIS = Mock.Create<emIRIS>();
 
    Mock.Arrange(() => emIRIS.CourseSections).ReturnsCollection(GetCourseSectionData());
    Mock.Arrange(() => emIRIS.FacultyCourseSections).ReturnsCollection(GetFacultyCourseSectionData());
}
 
[TestCleanup]
public void TestCleanup()
{
    emIRIS = null;
}
 
[TestMethod]
[Description("test")]
public void VFaculty_Test_Test()
{
    var preResults = emIRIS.FacultyCourseSections.Where(x => x.CourseSection_pk == 20).ToList();
 
    emIRIS.FacultyCourseSections.Count().Should().Be(6);
 
    emIRIS.Delete(preResults[0]);
 
    emIRIS.SaveChanges();
 
    emIRIS.FacultyCourseSections.Count().Should().Be(5);
 
}

 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?

Any Ideas???

 

Thanks

 

 

 

 

 

13 Answers, 1 is accepted

Sort by
0
Stefan
Telerik team
answered on 03 Jun 2015, 07:05 AM
Hi Andrew,

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.

Regards,
Stefan
Telerik
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
0
Andrew
Top achievements
Rank 1
Veteran
Iron
answered on 03 Jun 2015, 12:37 PM

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.

 

 

 

 

 

0
Andrew
Top achievements
Rank 1
Veteran
Iron
answered on 03 Jun 2015, 12:45 PM
Just a quick note when I got rid of the Mock.Create around my EnitiyModal Class 'emIRIS' and did 'new emIRIS()' then I tried to delete a record it wanted to connect to the database. It was looking for a connection string.
0
Stefan
Telerik team
answered on 04 Jun 2015, 09:16 AM
Hello Andrew,

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.

Regards,
Stefan
Telerik
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
0
Andrew
Top achievements
Rank 1
Veteran
Iron
answered on 04 Jun 2015, 12:09 PM
I can send you the class but it has not changed from what was generated by Open Access
0
Andrew
Top achievements
Rank 1
Veteran
Iron
answered on 04 Jun 2015, 12:14 PM
I thought that was what we are doing above by doing the 'Mock.Arrange' and the 'ReturnsCollection' above. Like I said this works great if we are just doing GETS and UPDATEs but ADDs and DELETEs do not work.
0
Stefan
Telerik team
answered on 05 Jun 2015, 01:21 PM
Hello Andrew,

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.

Regards,
Stefan
Telerik
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
0
Andrew
Top achievements
Rank 1
Veteran
Iron
answered on 08 Jun 2015, 12:21 PM

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.

 

 

 

 

0
Kaloyan
Telerik team
answered on 11 Jun 2015, 01:15 PM
Hi Andrew,

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.


Regards,
Kaloyan
Telerik
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
0
Andrew
Top achievements
Rank 1
Veteran
Iron
answered on 12 Jun 2015, 01:26 PM

[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.

 

 

0
Kaloyan
Telerik team
answered on 17 Jun 2015, 06:58 AM
Hello Andrew,

We are looking forward the sample project. Thank you for the help once again.

Regards,
Kaloyan
Telerik
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
0
Andrew
Top achievements
Rank 1
Veteran
Iron
answered on 19 Jun 2015, 01:24 PM
how do I send you a zip file
0
Kaloyan
Telerik team
answered on 24 Jun 2015, 08:23 AM
Hello Andrew,

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.

Regards,
Kaloyan
Telerik
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
Tags
General Discussions
Asked by
Andrew
Top achievements
Rank 1
Veteran
Iron
Answers by
Stefan
Telerik team
Andrew
Top achievements
Rank 1
Veteran
Iron
Kaloyan
Telerik team
Share this question
or