具有依赖注入构造函数参数的 XUnit 没有匹配的装置数据

发布于 2025-01-13 03:22:06 字数 573 浏览 1 评论 0原文

当我尝试使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我不在是我 2025-01-20 03:22:06

要在 XUnit 测试中注入依赖项,您需要定义一个固定装置。要提供的对象必须具有无参数构造函数,并且测试类必须从 IClassFixture 派生。

所以类似这样的事情应该有效:

public class MyRepository : IMyRepository
{
    public MyRepository() // No parameters
    {
        // ... Initialize your repository
    }
    // ... whatever else the class needs 
}

public class UnitTest1 : IClassFixture<MyRepository>
{
        private readonly IMyRepository _myRepo;

        public UnitTest1(MyRepository myRepo)
        {
            this._myRepo = myRepo;
        }

        [Fact]
        public void TestOne()
        {
            var name = "Hillary";

            var r = this._myRepo.FindName(name);
            Assert.True(r.School == "Capital Hill School");
        }
}

您还可以在 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:

public class MyRepository : IMyRepository
{
    public MyRepository() // No parameters
    {
        // ... Initialize your repository
    }
    // ... whatever else the class needs 
}

public class UnitTest1 : IClassFixture<MyRepository>
{
        private readonly IMyRepository _myRepo;

        public UnitTest1(MyRepository myRepo)
        {
            this._myRepo = myRepo;
        }

        [Fact]
        public void TestOne()
        {
            var name = "Hillary";

            var r = this._myRepo.FindName(name);
            Assert.True(r.School == "Capital Hill School");
        }
}

You can also define a test Collection and its fixture in the CollectionDefinition

The documentation at https://xunit.net/docs/shared-context explains how to do it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文