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:
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:
And this is the Mock: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());
}
}
I am getting a null, instead of a List initialized..My test:public
class
MockBatchManager : BatchManager
{
public
new
void
AddOrUpdateLoadCarrierRequest(LoadCarrierRequest loadCarrierRequest)
{
base
.AddOrUpdateLoadCarrierRequest(loadCarrierRequest);
}
}
if I take a look on the line:[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);
}
List<DestinationStatus> destinationStatuses = StorageInventoryService.GetDestinationStatus(
new
List<
string
> { loadCarrierRequest.DestinationAddress });