Why overriding not work when mock same method

11 posts, 0 answers
  1. ari
    ari avatar
    20 posts
    Member since:
    Jun 2010

    Posted 10 Apr 2012 Link to this post

    hi

    Test CODE:
    I have to make 3 different mock to same DeleteObject ()  method

    1M       Mock.Arrange(() => CustomerEntities.DeleteObject(Arg.IsAny<DHP_History>())).DoInstead(......
    2M.      Mock.Arrange(() => CustomerEntities.DeleteObject(Arg.IsAny<DHP_Document>())).DoInstead ......
    3M.      Mock.Arrange(() => CustomerEntities.DeleteObject(Arg.IsAny<DHP_File>())).DoInstead((......

    but  only last row comes mocked (3.)  . overriding not work in this case ... Why??  
    AND how to mock this kind of situation?


    Code to test: ( they are in same function )
    so tests fail in row 1.. because it is mocked to  3M  .. WHY ?  it should be 1M
    1.  ctx.DHP_Document.Where(d => childDocumentIDs.Contains(d.DocumentID)).ToList().ForEach(ch => ctx.DeleteObject(ch));
    2.  ctx.DHP_Document.Where(d => unAttachmentDocumentIDs.Contains(d.DocumentID)).ToList().ForEach(ch => ctx.DeleteObject(ch));
    3.  ctx.DHP_File.Where(f => unAttachmentFileIDs.Contains(f.FileID)).ToList().ForEach(ch => ctx.DeleteObject(ch));
  2. ari
    ari avatar
    20 posts
    Member since:
    Jun 2010

    Posted 12 Apr 2012 Link to this post

    hi

    Is it impossible to make Mock to same method  with different parameters ??

    I didn't find same kind of situation?
  3. DevCraft R3 2016 release webinar banner
  4. Ricky
    Admin
    Ricky avatar
    467 posts

    Posted 12 Apr 2012 Link to this post

    Hi Ari,

    Thanks for bringing up the question. The default setup behavior is override (similar to other tools) that means when mocked for same method, it will always take the last setup. In order to set different action for different setup for the same member, I would recommend you to take a look at this following post:

    http://www.telerik.com/help/justmock/basic-usage-sequential-mocking.html


     
    Kind Regards
    Ricky
    the Telerik team

    Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

  5. ari
    ari avatar
    20 posts
    Member since:
    Jun 2010

    Posted 13 Apr 2012 Link to this post


    Hi

    I have already tried InSequence  but i does not solve the problem..

    And My situation is different  
    I have to mock 3 same method with 3 different parameter type

    and all 3 mocked  have to work in same method in one call

    and there is only on assert that shows when test is passed

    There comes error:
    code:
     ctx.DHP_Document.Where(d => unAttachmentDocumentIDs.Contains(d.DocumentID)).ToList().ForEach(ch => ctx.DeleteObject(ch));

    Object of type 'Granlund.RYHTI.Model.EF.Customer.DHP_Document' cannot be converted to type 'Granlund.RYHTI.Model.EF.Customer.DHP_File'.

    So justmock does not mock as it should be.. Justmock tries mock to DHP_FILE... It should be DHP_Document as it is in error message
  6. Ricky
    Admin
    Ricky avatar
    467 posts

    Posted 17 Apr 2012 Link to this post

    Hi Ari,
    Thanks again for reporting the issue.

    At this point, I would request you to send me the sample you are working on so that we can investigate further and provide you with a solution.
     
    Kind regards,
    Ricky
    the Telerik team

    Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

  7. ari
    ari avatar
    20 posts
    Member since:
    Jun 2010

    Posted 18 Apr 2012 Link to this post

    Here is my test

    This is some kind of overloading problem?
    in justmock

    [TestMethod]
            public void InternalDelete_WithStatus()
            {
                //TODO  This test not Work..Asked from telerik


                var tempList = new List<DHPDocument>();
                var tempDocumentList = new List<DHP_Document>();
                var tempHistoryList = new List<DHP_History>();
                var tempFileList = new List<DHP_File>();


                tempHistoryList = CreateDHP_HistoryList().ToList();
                tempDocumentList = CreateDHP_DocumentList().ToList();
                tempFileList = CreateDHP_FileList().ToList();
                
                //Create test worker
                //TestDHPDocumentWorker documentWorker = CreateWorker<TestDHPDocumentWorker>();
                DHPDocumentWorker documentWorker = CreateWorker<DHPDocumentWorker>();
                // Mock needed method for worker to use from entity context               
                Mock.Arrange(() => documentWorker.GetDocumentAndAttachments(Arg.IsAny<long>())).ReturnsCollection(CreateDocumentGridItemList());
                Mock.Arrange(() => documentWorker.FileWorker.Delete(Arg.IsAny<long>())).DoNothing();
                Mock.Arrange(() => CustomerEntities.DeleteObject(Arg.IsAny<DHP_Document>())).DoInstead((DHP_Document document) => { tempDocumentList.Remove(document); }).InSequence();
                Mock.Arrange(() => CustomerEntities.DeleteObject(Arg.IsAny<DHP_History>())).DoInstead((DHP_History history) => { tempHistoryList.Remove(history); }).InSequence();           
                Mock.Arrange(() => CustomerEntities.DeleteObject(Arg.IsAny<DHP_File>())).DoInstead((DHP_File file) => { tempFileList.Remove(file); }).InSequence();


                var docu = CreateDocumentWithAllValues();
                docu.DocumentID = 1;
    // // Execute method in test
                documentWorker.InternalDelete(docu);           
            }




     ****************** Code  to test   ********************************************

      protected override OperationStatus InternalDelete(DHPDocument document)
            {


                DHP_Document documentObject = ctx.DHP_Document.Single(d => d.DocumentID == document.DocumentID);
                Boolean deleteFile = true;
                // Delete document history

    // HERE IS FIRST  1
                ctx.DHP_History.Where(h => h.DocumentID == document.DocumentID).ToList().ForEach(hd => ctx.DeleteObject(hd));
                Int64 fileID = document.FileID; // store fileID for later use if we delete file also
             
       switch (document.State)
                {
                    case DHPDocumentState.LinkedChild:
                        // Dont delete file if this is only linked document
                        deleteFile = false;
                        break;


                    case DHPDocumentState.LinkedParent:
                        // Get all linked childs for this parent
                        List<Int64> childDocumentIDs = GetLinkedChildDocumentsByParentID(document.DocumentID).Select(d => d.DocumentID).ToList();


                        // Delete all linked childs for this parent
                        ctx.DHP_Document.Where(d => childDocumentIDs.Contains(d.DocumentID)).ToList().ForEach(ch => ctx.DeleteObject(ch));


                        // And delete parent file also
                        deleteFile = true;
                        break;


                    case DHPDocumentState.FrontPageImageObject:
                    case DHPDocumentState.FrontPageImageDatabase:
               
    //  -->  this case problem
        case DHPDocumentState.Undefined:


                        // Check if we have document with attachment
                        List<Int64> unAttachmentDocumentIDs = GetDocumentAndAttachments(document.DocumentID).Select(d => d.Document.DocumentID).ToList();
                        List<Int64> unAttachmentFileIDs = GetDocumentAndAttachments(document.DocumentID).Where(d => d.Document.FileID > 0).Select(d => d.Document.FileID).ToList();


                        // Delete all linked attachments for this parent
    // HERERE IS FIRST  1 

                        ctx.DHP_Document.Where(d => unAttachmentDocumentIDs.Contains(d.DocumentID)).ToList().ForEach(ch => ctx.DeleteObject(ch));


                        // Delete all attachment files for this parent
                        ctx.DHP_File.Where(f => unAttachmentFileIDs.Contains(f.FileID)).ToList().ForEach(ch => ctx.DeleteObject(ch));


                        // Dispose object
                        documentObject = null;


                        // Already deleted above
                        deleteFile = false;
                        break;


                    case DHPDocumentState.UrlLink:
                        // No file attached, only url link stored
                        deleteFile = false;
                        break;


                    case DHPDocumentState.WithAttachmentsChild:
                        // Normal document
                        deleteFile = true;
                        break;


                    case DHPDocumentState.WithAttachmentsParent:
                        List<Int64> attachmentDocumentIDs = GetDocumentAndAttachments(document.DocumentID).Select(d => d.Document.DocumentID).ToList();
                        List<Int64> attachmentFileIDs = GetDocumentAndAttachments(document.DocumentID).Where(d => d.Document.FileID > 0).Select(d => d.Document.FileID).ToList();


                        // Delete all linked attachments for this parent

    // HERE IS SECOND 2 DHP_Document 

                        ctx.DHP_Document.Where(d => attachmentDocumentIDs.Contains(d.DocumentID)).ToList().ForEach(ch => ctx.DeleteObject(ch));


                        // Delete all attachment files for this parent

    // HERE IS Third 3
     DHP_File 


                        ctx.DHP_File.Where(f => attachmentFileIDs.Contains(f.FileID)).ToList().ForEach(ch => ctx.DeleteObject(ch));


                        // Dispose object
                        documentObject = null;


                        // Already deleted above
                        deleteFile = false;
                        break;


                    default:
                        throw new InvalidDataException("Document.State has unknown value");
                }


                if (documentObject != null)
                {
                    // Do the actual delete
                    ctx.DHP_Document.DeleteObject(documentObject);
                }


                ctx.SaveChanges();


                // Now is possible to delete file (if wanted) without constrain problems
                if (deleteFile == true && fileID > 0)
                {
                    FileWorker.Delete(fileID);
                }


                return OperationStatus.Success;
            }


  8. Ricky
    Admin
    Ricky avatar
    467 posts

    Posted 23 Apr 2012 Link to this post

    Hi Ari,

    Thanks again for sending the code snippet. However, i would also need the signature of the DeleteObject method and most importantly the class structure that will point me to line where the issue is occurring.  I wrote the following test with InSequence and so far it seems to work as expected.

    Let me know what I have missed here:



    [TestMethod]
    public void ShouldAssertOfSameMethodInThreeConsequtiveCalls()
    {
        var customEntities = Mock.Create<CustomEntities>();
     
        bool first = false;
        bool second = false;
        bool third = false;
     
        Mock.Arrange(() => customEntities.DeleteObject(Arg.IsAny<DHP_History>())).DoInstead(() => first = true).InSequence();
        Mock.Arrange(() => customEntities.DeleteObject(Arg.IsAny<DHP_History>())).DoInstead(() => second = true).InSequence();
        Mock.Arrange(() => customEntities.DeleteObject(Arg.IsAny<DHP_History>())).DoInstead(() => third = true).InSequence();
     
     
        customEntities.DeleteObject(new DHP_History());
        customEntities.DeleteObject(new DHP_History());
        customEntities.DeleteObject(new DHP_History());
     
        Assert.IsTrue(first == second == third);
     
    }
     
    public class DHP_History
    {
    }
     
    public class CustomEntities
    {
        public void DeleteObject(DHP_History history)
        {
     
        }
    }


    Kind regards,
    Ricky
    the Telerik team

    Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

  9. ari
    ari avatar
    20 posts
    Member since:
    Jun 2010

    Posted 24 Apr 2012 Link to this post

    Hi

    code breaks HERE:

    // Delete all linked attachments for this parent
                        ctx.DHP_Document.Where(d => unAttachmentDocumentIDs.Contains(d.DocumentID)).ToList().ForEach(ch => ctx.DeleteObject(ch));

    --> error
    Object of type 'Granlund.RYHTI.Model.EF.Customer.DHP_Document' cannot be converted to type 'Granlund.RYHTI.Model.EF.Customer.DHP_File'.

    Here.. only first mock succes  second fails
     Mock.Arrange(() => Entities.DeleteObject(Arg.IsAny<DHP_History>())).DoInstead((DHP_History history) => { tempHistoryList.Remove(history); }).InSequence();
                Mock.Arrange(() => Entities.DeleteObject(Arg.IsAny<DHP_Document>())).DoInstead((DHP_Document document) => { tempDocumentList.Remove(document); }).InSequence();           
                Mock.Arrange(() => Entities.DeleteObject(Arg.IsAny<DHP_File>())).DoInstead((DHP_File file) => { tempFileList.Remove(file); }).InSequence();

    if i change order .. error comes from first call to DeleteObject   
     // Delete document history
                ctx.DHP_History.Where(h => h.DocumentID == document.DocumentID).ToList().ForEach(hd => ctx.DeleteObject(hd));

    AND 
    Difference is that I have only one call to mehod ( "documentWorker.InternalDelete(docu)" ; which includes 3 different DeleteObject   calls

    so justmock overloading not work? why
  10. Ricky
    Admin
    Ricky avatar
    467 posts

    Posted 26 Apr 2012 Link to this post

    Hi Ari,

    Thanks again for your reply.

    However, if you can send me a stripped down version of your project to mehfuz.hossain at telerik dot com then I can investigate further on the issue and possibly can send you a solution. I need to debug and check if that’s an issue with method overriding.


    Kind Regards
    Mehfuz
    the Telerik team

    Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

  11. ari
    ari avatar
    20 posts
    Member since:
    Jun 2010

    Posted 27 Apr 2012 Link to this post

    Hi

    we founded the answer to this "bug".. our software architect 

    this works

     Mock.Arrange(() => Entities.DeleteObject(Arg.Matches<EntityObject>(d => d is DHP_Document))).DoInstead((DHP_Document document) => { tempDocumentList.Remove(document); }).InSequence();
                Mock.Arrange(() => Entities.DeleteObject(Arg.Matches<EntityObject>(d => d is DHP_History))).DoInstead((DHP_History history) => { tempHistoryList.Remove(history); }).InSequence();
                Mock.Arrange(() => Entities.DeleteObject(Arg.Matches<EntityObject>(d => d is DHP_File))).DoInstead((DHP_File file) => { tempFileList.Remove(file); }).InSequence();


    Thanks ..if you can explain why this do not work

    Mock.Arrange(() => Entities.DeleteObject(Arg.IsAny<DHP_History>())).DoInstead((DHP_History history) => { tempHistoryList.Remove(history); }).InSequence();
    Mock.Arrange(() => Entities.DeleteObject(Arg.IsAny<DHP_Document>())).DoInstead((DHP_Document document) => { tempDocumentList.Remove(document); }).InSequence();
    Mock.Arrange(() => Entities.DeleteObject(Arg.IsAny<DHP_File>())).DoInstead((DHP_File file) => { tempFileList.Remove(file); }).InSequence();

  12. Ricky
    Admin
    Ricky avatar
    467 posts

    Posted 27 Apr 2012 Link to this post

    Hi Ari,
    It’s great that you are able to identify the problem. However, when I try the following block, it works as expected (taken from the project that I attached previously):
    [TestMethod]
      
    public void ShouldAssertOfSameMethodInThreeConsequtiveCalls()
    {
      
        var customEntities = Mock.Create<CustomEntities>();
          
        bool first = false;
      
        bool second = false;
      
        bool third = false;
      
      
      
        Mock.Arrange(() => customEntities.DeleteObject(Arg.IsAny<DHP_History>())).DoInstead(() => first = true).InSequence();
      
        Mock.Arrange(() => customEntities.DeleteObject(Arg.IsAny<DHP_History>())).DoInstead(() => second = true).InSequence();
      
        Mock.Arrange(() => customEntities.DeleteObject(Arg.IsAny<DHP_History>())).DoInstead(() => third = true).InSequence();
      
      
      
      
      
        customEntities.DeleteObject(new DHP_History());
      
        customEntities.DeleteObject(new DHP_History());
      
        customEntities.DeleteObject(new DHP_History());
      
      
      
        Assert.IsTrue(first == second == third);
          
    }


    Can you please send me the argument that you are passing for DeleteObject method? I will do another round of test once you provide me the actual parameter that you are passing for the method.

    Kind Regards
    Mehfuz
    the Telerik team

    Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

Back to Top
DevCraft R3 2016 release webinar banner