My MongoDbRepository
has an interface called IRepository
. So I am mocking the interface to set up the method which is UpdateOneAsync
. However my MerchantConfigurationRepository
can only take a MongoDbRepository
object which is why I need to cast it. For some reason when I do this
(MongoDBRepository<MerchantConfigurationEntity>)dataAccess.Object
I get the error
Unable to cast object of type 'Castle.Proxies.IRepository`1Proxy' to type 'Newgistics.Common.MongoDb.MongoDBRepository`1
How am I supposed to set up the Mock and then pass in the object, I tried setting a variable to the dataAccess.Object
and passing in that variable, but if I do that the setup goes in as null
.
Below you will find the unit test:
[Fact]
public async void UpdateMerchantSuccessPushesMerchantEntityToDataStore()
{
//Arrange
var originalMerchantConfig = ModelFactory.GetMerchant();
var merchants = new List<MerchantConfigurationEntity>();
var dataAccess = new Mock<IRepository<MerchantConfigurationEntity>>();
dataAccess.Setup(m => m.UpdateOneAsync(It.IsAny<MerchantConfigurationEntity>()))
.Callback((MerchantConfigurationEntity m) => merchants.Add(m))
.Returns(Task.FromResult(1));
var merchantRepo = new MerchantConfigurationRepository((MongoDBRepository<MerchantConfigurationEntity>)dataAccess.Object);
//Act
var result = await merchantRepo.UpdateMerchant(originalMerchantConfig);
//Assert
result.ShouldNotBeNull();
result.Sucess.ShouldBeTrue();
merchants.Count.ShouldBe(1);
merchants[0].merchantId.ShouldBe(originalMerchantConfig.merchantId);
}