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

发布时间:2022-09-18 / 作者:清心寡欲
本文介绍了单元测试xunit以下构造函数参数没有匹配的装置数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下构造函数参数没有匹配的装置数据,无法使用Moq和xunit进行单元测试。

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

//this is how i register the DI.
services.AddScoped(); 


 public interface IWaktuSolatServiceApi
 {
    Task GetAsyncSet();
 }


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

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

    [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 waktu;

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

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

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

这篇关于单元测试xunit以下构造函数参数没有匹配的装置数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持吉威生活!



[英文标题]Unit Testing xunit The following constructor parameters did not have matching fixture data


声明:本媒体部分图片、文章来源于网络,版权归原作者所有,如有侵权,请联系QQ:330946442删除。