JustMock Telerik ORM Add and Delete

14 posts, 0 answers
  1. Andrew
    Andrew avatar
    125 posts
    Member since:
    Sep 2008

    Posted 02 Jun 2015 Link to this post

    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

     

     

     

     

     

  2. Stefan
    Admin
    Stefan avatar
    198 posts

    Posted 03 Jun 2015 Link to this post

    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
  3. DevCraft R3 2016 release webinar banner
  4. Andrew
    Andrew avatar
    125 posts
    Member since:
    Sep 2008

    Posted 03 Jun 2015 in reply to Stefan Link to this post

    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.

     

     

     

     

     

  5. Andrew
    Andrew avatar
    125 posts
    Member since:
    Sep 2008

    Posted 03 Jun 2015 in reply to Andrew Link to this post

    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.
  6. Stefan
    Admin
    Stefan avatar
    198 posts

    Posted 04 Jun 2015 Link to this post

    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
  7. Andrew
    Andrew avatar
    125 posts
    Member since:
    Sep 2008

    Posted 04 Jun 2015 Link to this post

    I can send you the class but it has not changed from what was generated by Open Access
  8. Andrew
    Andrew avatar
    125 posts
    Member since:
    Sep 2008

    Posted 04 Jun 2015 in reply to Stefan Link to this post

    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.
  9. Stefan
    Admin
    Stefan avatar
    198 posts

    Posted 05 Jun 2015 Link to this post

    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
  10. Andrew
    Andrew avatar
    125 posts
    Member since:
    Sep 2008

    Posted 08 Jun 2015 in reply to Stefan Link to this post

    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.

     

     

     

     

  11. Kaloyan
    Admin
    Kaloyan avatar
    872 posts

    Posted 11 Jun 2015 Link to this post

    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
  12. Andrew
    Andrew avatar
    125 posts
    Member since:
    Sep 2008

    Posted 12 Jun 2015 in reply to Kaloyan Link to this post

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

    I think that's what my example above was doing.

    Anyways I will build a sample project and send it to you.

     

     

  13. Kaloyan
    Admin
    Kaloyan avatar
    872 posts

    Posted 17 Jun 2015 Link to this post

    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
  14. Andrew
    Andrew avatar
    125 posts
    Member since:
    Sep 2008

    Posted 19 Jun 2015 in reply to Kaloyan Link to this post

    how do I send you a zip file
  15. Kaloyan
    Admin
    Kaloyan avatar
    872 posts

    Posted 24 Jun 2015 Link to this post

    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
Back to Top
DevCraft R3 2016 release webinar banner