单元测试 xunit 以下构造函数参数没有匹配的夹具数据

Unit Testing xunit The following constructor parameters did not have matching fixture data

以下构造函数参数没有使用 moq 和 xunit 进行单元测试的匹配夹具数据。

已经在使用依赖注入和模拟来测试 class。

//this is how i register the DI.
services.AddScoped<IWaktuSolatServiceApi, WaktuSolatServiceApi>(); 


 public interface IWaktuSolatServiceApi
 {
    Task<Solat> GetAsyncSet();
 }


// the unit test. 
public class UnitTest1 
{
    Mock<IWaktuSolatServiceApi> waktu;

    public UnitTest1(IWaktuSolatServiceApi waktu)
    {
        this.waktu = new Mock<IWaktuSolatServiceApi>();
    }

    [Fact]
    public async Task ShoudReturn()
    {
        var request = new Solat
        {
            zone = "lala"
        };

        var response = waktu.Setup(x => 
        x.GetAsyncSet()).Returns(Task.FromResult(request));
    }
}

但我收到此错误以下构造函数参数没有匹配的夹具数据。

Xunit 没有使用 DI 来解析引用。

删除构造函数参数。无论如何,至少在您的代码示例中,它是未使用的。

// the unit test. 
public class UnitTest1 
{
    Mock<IWaktuSolatServiceApi> waktu;

    /// HERE, remove the parameter
    public UnitTest1()
    {
        this.waktu = new Mock<IWaktuSolatServiceApi>();
    }

    [Fact]
    public async Task ShoudReturn()
    {
        var request = new Solat
        {
            zone = "lala"
        };

        var response = waktu.Setup(x => 
        x.GetAsyncSet()).Returns(Task.FromResult(request));
    }
}