单元测试xunit以下构造函数参数没有匹配的装置数据 [英] Unit Testing xunit The following constructor parameters did not have matching fixture data
本文介绍了单元测试xunit以下构造函数参数没有匹配的装置数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
以下构造函数参数没有匹配的装置数据,无法使用Moq和xunit进行单元测试。
已经使用依赖注入和模拟来测试类。
//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));
}
}
这篇关于单元测试xunit以下构造函数参数没有匹配的装置数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文