为什么以下使用 Ninject.Moq 的模拟不起作用?

发布于 2024-09-14 13:07:10 字数 804 浏览 3 评论 0原文

我正在尝试使用 Ninject.Moq 运行以下代码:

[TestMethod]
public void TestMethod1()
{
    var kernel = new MockingKernel();
    var engine = kernel.Get<ABC>();

   //as I don't need to actually use the interfaces, I don't want
   //to even have to bother about them.

    Assert.AreEqual<string>("abc", engine.ToString());
}

这是 ABC 类定义:

public class ABC {
    IA a;
    IB b;

    public ABC(IA a, IB b)
    {
        this.a = board;
        this.b = war;
    }

    public override string ToString()
    {
        return "abc";
    }
}

我收到以下异常:

System.ArgumentException:匹配 给定参数的构造函数 在模拟类型上找不到。 ---> 系统.MissingMethodException: 类型的构造函数 'AbcProxya759aacd0ed049f3849aaa75e2a7bade' 未找到。

I'm trying to run the following code with Ninject.Moq:

[TestMethod]
public void TestMethod1()
{
    var kernel = new MockingKernel();
    var engine = kernel.Get<ABC>();

   //as I don't need to actually use the interfaces, I don't want
   //to even have to bother about them.

    Assert.AreEqual<string>("abc", engine.ToString());
}

And here is the ABC class definition:

public class ABC {
    IA a;
    IB b;

    public ABC(IA a, IB b)
    {
        this.a = board;
        this.b = war;
    }

    public override string ToString()
    {
        return "abc";
    }
}

I'm getting the following exception:

System.ArgumentException: A matching
constructor for the given arguments
was not found on the mocked type. --->
System.MissingMethodException:
Constructor on type
'AbcProxya759aacd0ed049f3849aaa75e2a7bade'
not found.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

日暮斜阳 2024-09-21 13:07:10

好的,这将使代码工作:

[TestMethod]
public void TestMethod1()
{
    var kernel = new MockingKernel();
    kernel.Bind<Abc>().ToSelf();
    var engine = kernel.Get<ABC>();

   //as I don't need to actually use the interfaces, I don't want
   //to even have to bother about them.

    Assert.AreEqual<string>("abc", engine.ToString());
}

必须将 Abc 绑定到自身,否则它也会被模拟,而 Moq 只支持模拟无参数类,但事实并非如此。

Ok, this will make the code work:

[TestMethod]
public void TestMethod1()
{
    var kernel = new MockingKernel();
    kernel.Bind<Abc>().ToSelf();
    var engine = kernel.Get<ABC>();

   //as I don't need to actually use the interfaces, I don't want
   //to even have to bother about them.

    Assert.AreEqual<string>("abc", engine.ToString());
}

One has to bind Abc to itself, otherwise it will also get mocked and Moq only supports mocking parameterless classes, which is not the case.

独﹏钓一江月 2024-09-21 13:07:10

这有点像理解 DI在第一个
place:- 小样本并不能真正说明全部要点。

像 Ninject.Moq 这样的自动模拟容器(或者像 AutoFixture 这样的类似测试基础设施库)很难用简单的解释来真正解释例子。我建议阅读 Mark Seemann 关于 AutoFixture 的所有帖子感受需求。

因此,Ninject.Moq 将处理一组接口存根实现的链接,N 层深度,这些接口是在执行测试实际应该做的事情的过程中满足被测系统所必需的进行测试。

一般来说,您需要简短、易于阅读、易于理解的测试,并且具有最小的复杂性和底层固定装置的交互(没有大的基类层次结构,或者 6 种不同的魔术方法进行古怪的拆卸和调用基类)。通常这个目标意味着你应该让你的 DI 工具远离你的单元测试。

自动模拟容器应该像电锯一样,仅在您的投资能够获得显着的实际回报(许多更短、更容易理解的测试)时使用(另一个在其他人可以之前理解的工具)阅读你的测试,更多的调试,更多的惊喜,更多的复杂性,这将导致脆弱的、不可维护的测试)。

It's a bit like understanding DI in the first
place
:- small samples don't really get the whole point across.

An automocking container like Ninject.Moq (or similar test infrastructure libraries like AutoFixture) is hard to really explain with a simple example. I'd suggest reading all of Mark Seemann's posts on AutoFixture as a way of getting a feel fro the requirement.

So Ninject.Moq will deal with the chaining, N levels deep of a set of stub implementations of interfaces that are necessary to satisfy your System Under Test in the course of doing the thing your test is actually supposed to be testing.

In general, you want short easy to read, easy to grok tests with minimal complexity and interaction of fixtures under the cover (no big hierarchy of base classes, or 6 different magic methods doing wacky teardown and calling base classes). Normally this aim will mean you should keep your DI toolery miles away from your unit tests.

An automocking container should, like a chainsaw, only be used where you're going to get a signnificant real return (many shorter, easier to understand tests) for you investment (another tool to understand before others can read you tests, more debugging, more surprises, more complexity that'll lead to brittle, unmaintainable tests).

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