如何自动模拟现有的模拟对象?

时间:2019-06-21 11:44:09

标签: c# autofac xunit automocking

在我现有的项目中,我正在使用xunit。当前,模拟服务中的每个瞬间非常烦人。因此,我决定使用AutoMock,但是我的项目非常庞大,并且我已经完成了很多代码并分配了很多值。那么,是否可以在不更改现有模拟对象的情况下使用AutoMock? 以下是我现有的代码示例。 我打算使用AutoFac

当前,如果我要设置任何值,我将

bordereauFormatsRepositoryMock
    .Setup(x => x.GetBordereauFormats(It.IsAny<List<string>>()))
    .Returns(botFormatsVMList);*

在AutoMock中-

mock.Mock<IBordereauFormatsRepository>()
    .Setup(x => x.GetBordereauFormats(It.IsAny<List<string>>()))
    .Returns(botFormatsVMList)*;

那么我如何将bordereauFormatsRepositoryMock分配给mock.Mock<IBordereauFormatsRepository>()

public BotMockTest()
{
    #region Object initialization
    botFormatsVMList = new List<BotFormatsVM>();
    botFormatsVM = new BotFormatsVM();
    botService = new BotService(bordereauFormatsRepositoryMock.Object, bordereauFormatColumnRepositoryMock.Object, contractRepository.Object, bordereauxRepositoryMock.Object, exceptionLogServiceMock.Object);
    #endregion
}

[FactWithAutomaticDisplayName]
public void GetBordereauFormats()
{
    //Arrange
    List<string> sheetList = new List<string>() { { "sheet" } };
    botFormatsVM.BordereauFormatId = Guid.Parse("847AE6BD-C20E-4377-8F56-36D58674C961");
    botFormatsVMList.Add(botFormatsVM);
    bordereauFormatsRepositoryMock.Setup(x => x.GetBordereauFormats(It.IsAny<List<string>>())).Returns(botFormatsVMList);

    //Act
    botService.GetBordereauFormats(sheetList);

    //Assert
    Assert.True(botFormatsVM.BordereauFormatName == "UpdatedVal");
}

0 个答案:

没有答案
相关问题