具有依赖注入构造函数参数的 XUnit 没有匹配的装置数据
当我尝试使用 XUnit 执行简单的单元测试时,出现以下错误。
以下构造函数参数没有匹配的固定装置 数据:IMyRepository myRepo
场景:当我提供学生姓名时,我正在检查学生学校是否正确。] 代码
public class UnitTest1
{
private readonly IMyRepository _myRepo;
public UnitTest1(IMyRepository myRepo)
{
this._myRepo = myRepo;
}
[Fact]
public void TestOne()
{
var name = "Hillary";
var r = this._myRepo.FindName(name);
Assert.True(r.School == "Capital Hill School");
}
}
I am getting the following error when I am trying to execute a simple Unit Test using XUnit.
The following constructor parameters did not have matching fixture
data: IMyRepository myRepo
Scenario: When I supplied with the name of a student, I am checking if the student school is correct.]
Code
public class UnitTest1
{
private readonly IMyRepository _myRepo;
public UnitTest1(IMyRepository myRepo)
{
this._myRepo = myRepo;
}
[Fact]
public void TestOne()
{
var name = "Hillary";
var r = this._myRepo.FindName(name);
Assert.True(r.School == "Capital Hill School");
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
要在 XUnit 测试中注入依赖项,您需要定义一个固定装置。要提供的对象必须具有无参数构造函数,并且测试类必须从
IClassFixture
派生。所以类似这样的事情应该有效:
您还可以在
CollectionDefinition
中定义测试Collection
及其固定装置https://xunit.net/docs/shared-context 解释了如何操作。
To inject a dependency in an XUnit test you need to define a fixture. The object to be provided must have a paramaterless constructor, and the test class has to be derived from
IClassFixture<YourFixture>
.So something like this should work:
You can also define a test
Collection
and its fixture in theCollectionDefinition
The documentation at https://xunit.net/docs/shared-context explains how to do it.