C# 如何模拟由多个类实现单个接口的对象

C# 如何模拟由多个类实现单个接口的对象,c#,moq,autofac,xunit,.net-4.8,C#,Moq,Autofac,Xunit,.net 4.8,我有一个接口和多个类的实现。我已经在Autofac容器中注册了它们。我的PaymentService类采用两个参数,其中一个参数可以实现多个类。如何模拟特定的类 接口 public interface IAccountDataStorage { Account GetAccount(string accountNumber); void UpdateAccount(Account account); } 实现类 public class BackupAccountDataSto

我有一个接口和多个类的实现。我已经在Autofac容器中注册了它们。我的PaymentService类采用两个参数,其中一个参数可以实现多个类。如何模拟特定的类

接口

public interface IAccountDataStorage
{
    Account GetAccount(string accountNumber);
    void UpdateAccount(Account account);
}
实现类

public class BackupAccountDataStore : IAccountDataStorage
{
     ...
}

public class AccountDataStore : IAccountDataStorage
{
     ...
}
在Autofac容器中注册

 builder.RegisterType<AccountDataStore>().Keyed("defaultAccountStorage", typeof(IAccountDataStorage));
 builder.RegisterType<BackupAccountDataStore>().Keyed("backupAccountStorage", typeof(IAccountDataStorage));
builder.RegisterType().Keyed(“defaultAccountStorage”,typeof(IAccountDataStorage));
builder.RegisterType().Keyed(“backupAccountStorage”,typeof(IAccountDataStorage));
解析特定类的接口

var a = buildContainer.ResolveKeyed<IAccountDataStorage>("defaultAccountStorage");
var b = buildContainer.ResolveKeyed<IAccountDataStorage>("backupAccountStorage");
var a=buildContainer.ResolveKeyed(“defaultAccountStorage”);
var b=buildContainer.ResolveKeyed(“backupAccountStorage”);
测试班

public class MakePaymentTest
{
    private readonly PaymentService sut;
    private readonly Mock<IAppAmbientState> mockAppAmbientState = new Mock<IAppAmbientState>();
    //private readonly Mock<IAccountDataStorage> mockAccountDataStorage = new Mock<IAccountDataStorage>();


    public MakePaymentTest()
    {
        mockAppAmbientState.Object.AppSettingsRepository = new App.SharedKernel.Values.AppConfiguration
        {
            DataStoreType = "Backup"
        };

        //Need help here
        sut = new PaymentService(mockAppAmbientState.Object, ???????); // let say I want mock AccountDataStore here...
    }

    [Fact]
    public void MakePayment_Should_ReturnTypeOf_MakePaymentResult()
    {
        //Arrange
        var fixture = new Fixture();
        var mockMakePaymentRequest = fixture.Create<MakePaymentRequest>();


        //Act
        var actualDataResult = sut.MakePayment(mockMakePaymentRequest);

        //Assert

    }
公共类MakePaymentTest
{
私人只读支付服务sut;
private readonly Mock mockAppAmbientState=new Mock();
//private readonly Mock mockAccountDataStorage=new Mock();
公共MakePaymentTest()
{
mockAppAmbientState.Object.AppSettingsRepository=新建App.SharedKernel.Values.AppConfiguration
{
DataStoreType=“备份”
};
//这里需要帮助吗
sut=newPaymentService(mockAppAmbientState.Object,???????);//假设我想要这里的模拟AccountDataStore。。。
}
[事实]
public void MakePayment\u应该\u ReturnTypeOf\u MakePaymentResult()
{
//安排
var fixture=新fixture();
var mockMakePaymentRequest=fixture.Create();
//表演
var actualDataResult=sut.MakePayment(mockMakePaymentRequest);
//断言
}

您不模拟特定的实现。您模拟的是返回值

sut = new PaymentService(mockAppAmbientState.Object, ???????); // let say I want mock AccountDataStore here...
您必须做的是,使第一个和第二个模拟对象返回适当的值,以便单元测试遵循所需的代码路径

这些类的具体实现将在它们自己的类中进行测试

下面这一行的全部思想是使用抽象并处理返回值。因此,任何到实际具体实现的链接,都不需要首先进行模拟

private readonly Mock<IAccountDataStorage> mockAccountDataStorage = new Mock<IAccountDataStorage>();
private readonly Mock mockAccountDataStorage=new Mock();

您不模拟特定的实现。您模拟的是返回值

sut = new PaymentService(mockAppAmbientState.Object, ???????); // let say I want mock AccountDataStore here...
您必须做的是,使第一个和第二个模拟对象返回适当的值,以便单元测试遵循所需的代码路径

这些类的具体实现将在它们自己的类中进行测试

下面这一行的全部思想是使用抽象并处理返回值。因此,任何到实际具体实现的链接,都不需要首先进行模拟

private readonly Mock<IAccountDataStorage> mockAccountDataStorage = new Mock<IAccountDataStorage>();
private readonly Mock mockAccountDataStorage=new Mock();

所以我的主要问题是!我想测试AccountDataStore,而不是备份实现IAccountDataStore的AccountDataStore,那么我该怎么做呢?非常感谢您提前测试了
AccountDataStore
的实例。这些测试的新文件模拟了
AccountDataStore
所需的外部依赖性,所以我的mai问题是!我想测试AccountDataStore,而不是备份实现IAccountDataStore的AccountDataStore,那么我该怎么做呢?非常感谢您提前测试了
AccountDataStore
的实例。对于那些模拟
AccountDataStore
所需的外部依赖关系的测试,请显示构造或
PaymentService
的定义。请显示
PaymentService
的构造函数定义。