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

编程入门 行业动态 更新时间:2024-10-28 19:22:59
本文介绍了单元测试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以下构造函数参数没有匹配的装置数据

本文发布于:2023-11-15 18:11:30,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1596789.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:函数   装置   单元测试   参数   数据

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!