本文介绍了单元测试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以下构造函数参数没有匹配的装置数据
发布评论