ReturnsCollection does not work when deriving from a class

4 posts, 0 answers
  1. Javier
    Javier avatar
    10 posts
    Member since:
    Apr 2013

    Posted 12 Nov 2013 Link to this post

    Hi,

    I would like to Mock a Property of a class that has some protected methods , to do so I have created a Mock for exposing those methods in my tests:

    
    
    public class BatchManager
    {
     
    public IStorageInventoryService StorageInventoryService { get; set; }
      protected virtual void AddOrUpdateLoadCarrierRequest(LoadCarrierRequest loadCarrierRequest)
            {
                List<DestinationStatus> destinationStatuses = StorageInventoryService.GetDestinationStatus(new List<string> { loadCarrierRequest.DestinationAddress });
      
                AddOrUpdateDestinationDictionary(destinationStatuses, loadCarrierRequest);
                AddOrUpdateBatchDictionary(loadCarrierRequest);
                AddOrUpdateLoadCarrierRequestDictionary(loadCarrierRequest);
      
      
                ExecuteNextTransport(destinationStatuses.Select(d => d.Address).ToList());
            }
     
     
    }


    And this is the Mock:
    public class MockBatchManager : BatchManager
    {
        public new  void AddOrUpdateLoadCarrierRequest(LoadCarrierRequest loadCarrierRequest)
            {
                base.AddOrUpdateLoadCarrierRequest(loadCarrierRequest);
            }
     
     
    }
    My test:
    [TestMethod]
           public void AddOrUpdateLoadCarrierRequestAddsAnElementToEveryDictionary()
           {
               //Arrenge
               MockBatchManager mockBatchManager = new MockBatchManager
                   {
                       StorageInventoryService = Mock.Create<StorageInventoryService>()
                   };
     
               //Mock.Arrange(() => mockBatchManager.ExecuteNextTransport(Arg.IsAny<List<string>>())).DoNothing();
     
               Mock.Arrange(() => mockBatchManager.StorageInventoryService.GetDestinationStatus(Arg.IsAny<List<string>>()))
                   .ReturnsCollection(new List<DestinationStatus>());
     
               //Act
               mockBatchManager.AddOrUpdateLoadCarrierRequest(loadCarrierRequest);
     
               //Assert
     
               Assert.IsTrue(mockBatchManager.DestinationDic.Count == 1);
               Assert.IsTrue(mockBatchManager.LoadCarrierRequestsDic.Count == 1);
               Assert.IsTrue(mockBatchManager.ConcurrentDestinationStatuses.Count == 1);
               Assert.IsTrue(mockBatchManager.BatchDic.Count == 1);
                
           }
    if I take a look on the line:
    List<DestinationStatus> destinationStatuses = StorageInventoryService.GetDestinationStatus(new List<string> { loadCarrierRequest.DestinationAddress });
    I am getting a null, instead of a List initialized..
  2. Kaloyan
    Admin
    Kaloyan avatar
    872 posts

    Posted 12 Nov 2013 Link to this post

    Hi Javier,

    I tried to reproduce the issue on my side, unfortunately with no success. Executing the same test of yours resulted with correctly returned collection. I have shown this in the attached Screenshot1.png.

    To be able to continue investigating the issue on my side, I will require a sample project that can reproduce it. Please, if it is possible send me such project with your next message.

    Another thing you can try is to drop the MockBatchManager class and directly arrange expectations to the BatchManager class. Then, to execute the protected method you can use the PrivateAccessor coming with JustMock. I have prepared an example that should give you more detailed explanation:
    [TestMethod]
    public void AddOrUpdateLoadCarrierRequestAddsAnElementToEveryDictionaryWithPrivateAccessor()
    {
        //Arrange
        BatchManager mockBatchManager = Mock.Create<BatchManager>(Behavior.CallOriginal);
     
        Mock.Arrange(() => mockBatchManager.StorageInventoryService.GetDestinationStatus(Arg.IsAny<List<string>>()))
            .ReturnsCollection(new List<DestinationStatus>());
     
        //Act
        var privateacc = new PrivateAccessor(mockBatchManager);
        privateacc.CallMethod("AddOrUpdateLoadCarrierRequest", loadCarrierRequest);
     
        //Assert
        //...
    }

    Let me know if you can send me the sample project or the workaround doesn't work on your side. In both ways I will assist you further.

    Regards,
    Kaloyan
    Telerik
    Share what you think about JustTrace & JustMock with us, so we can become even better! You can use the built-in feedback tool inside JustTrace, our forums, or our JustTrace or JustMock portals.
  3. DevCraft R3 2016 release webinar banner
  4. Javier
    Javier avatar
    10 posts
    Member since:
    Apr 2013

    Posted 14 Nov 2013 Link to this post

    Hi,
    we refactored our classes and now everything is working with your reply!

    Thanks
  5. Kaloyan
    Admin
    Kaloyan avatar
    872 posts

    Posted 14 Nov 2013 Link to this post

    Hi Javier,

    I am glad the issue is solved now.

    Please, do not hesitate to contact me again. I wish you a good day and happy mocking :).

    Regards,
    Kaloyan
    Telerik
    Share what you think about JustTrace & JustMock with us, so we can become even better! You can use the built-in feedback tool inside JustTrace, our forums, or our JustTrace or JustMock portals.
Back to Top