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

ReturnsCollection does not work when deriving from a class

3 Answers 79 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Javier
Top achievements
Rank 2
Javier asked on 12 Nov 2013, 02:38 PM
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..

3 Answers, 1 is accepted

Sort by
0
Kaloyan
Telerik team
answered on 12 Nov 2013, 04:17 PM
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.
0
Javier
Top achievements
Rank 2
answered on 14 Nov 2013, 08:58 AM
Hi,
we refactored our classes and now everything is working with your reply!

Thanks
0
Kaloyan
Telerik team
answered on 14 Nov 2013, 09:27 AM
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.
Tags
General Discussions
Asked by
Javier
Top achievements
Rank 2
Answers by
Kaloyan
Telerik team
Javier
Top achievements
Rank 2
Share this question
or