通过 UnityAutoMoqContainer 解决依赖关系

发布于 2024-12-02 22:26:07 字数 1387 浏览 0 评论 0原文

我已经开始使用 UnityAutoMoqContainer 这是链接 我有以下两个问题,特别是关于 container.GetMock() 调用的问题。

  1. 我希望下面的断言能够成功,但它会抛出异常。

    私有UnityAutoMoqContainer容器;
    
    [设置]
    公共无效设置()
    {
        容器=新的UnityAutoMoqContainer();
    }
    
    [测试]
    公共无效Are_mocks_Same(){
    
        var serviceMock = new Mock();
        var getMock = container.GetMock();             
        Assert.AreSame(getMock, serviceMock);
    }
    

错误1测试 'UnityAutoMoq.Tests.UnityAutoMoqContainerFixture.Are_mocks_Same' 失败:预期:与上次相同 UnityAutoMoq.Tests.UnityAutoMoqContainerFixture.Are_mocks_Same() 中 C:\用户…….

那么为什么当“预期”和“但是”相同时它仍然抛出异常?

我看到的区别是,GetMock 使用 Unity 来解析,即 Resolve() 依赖项,而 new Mock 则不使用。但我无法向自己解释这个异常的原因。

  1. 解析抽象类型:

    我使用 Moq.Mock 来解析抽象类型,如下所示。

     var httpContextBaseMock = new Mock();
    

但是,以下对 UnityAutoMoqContainer 的调用会引发异常:

        var mock = container.GetMock<HttpContextBase>();

依赖关系解析失败,类型 = “System.Web.HttpContextBase”,名称=“(无)”。发生异常 while:解决时。例外是:InvalidOperationException - 无法构造 HttpContextBase 类型。您必须配置 提供此值的容器。

问题是为什么容器不方便返回模拟的抽象类型?

I have started using the UnityAutoMoqContainer Here is the Link
and I have below 2 questions in particularly around container.GetMock() call.

  1. I would expect the below Assert to succeed however it throws an exception.

    private UnityAutoMoqContainer container;
    
    [SetUp]
    public void SetUp()
    {
        container = new UnityAutoMoqContainer();
    }
    
    [Test]
    public void Are_mocks_Same(){
    
        var serviceMock = new Mock<IService>();
        var getMock = container.GetMock<IService>();             
        Assert.AreSame(getMock, serviceMock);
    }
    

Error 1 Test
'UnityAutoMoq.Tests.UnityAutoMoqContainerFixture.Are_mocks_Same'
failed: Expected: same as at
UnityAutoMoq.Tests.UnityAutoMoqContainerFixture.Are_mocks_Same() in
C:\Users…….

So why when "Expected" and the "But was" are same it still throws an exception?

The difference I see is that GetMock uses the Unity to resolve i,e Resolve() dependencies where the new Mock doesn’t. But I cannot explain my self the cause of this exception.

  1. Resolving abstract types:

    I use the Moq.Mock to resolve an abstract type as below.

        var httpContextBaseMock = new Mock<HttpContextBase>();
    

However the below call to the UnityAutoMoqContainer throws the exception:

        var mock = container.GetMock<HttpContextBase>();

Resolution of the dependency failed, type =
"System.Web.HttpContextBase", name = "(none)". Exception occurred
while: while resolving. Exception is: InvalidOperationException - The
type HttpContextBase cannot be constructed. You must configure the
container to supply this value.

The question is why the container does not facilitate to return a mocked abstract type?

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

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

发布评论

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

评论(1

涙—继续流 2024-12-09 22:26:07

AreSame 方法测试两个参数是否引用相同对象。当您创建两个不同的对象时

var serviceMock = new Mock<IService>();
var getMock = container.GetMock<IService>();
Assert.AreSame(getMock, serviceMock);

,它们永远不会是相同的引用。如果不使用容器,automock 容器就无法知道您创建的实例。然而,这将会成功:

var mock1 = container.GetMock<IService>();
var mock2 = container.GetMock<IService>();
Assert.AreSame(mock1, mock2);

无法从抽象类型创建模拟是一个错误,但现在应该修复。如果您更新到 v2.1.0,它应该可以按预期工作。

希望这有帮助!

-托马斯

The AreSame method tests that the same object are referenced by both arguments. When you do

var serviceMock = new Mock<IService>();
var getMock = container.GetMock<IService>();
Assert.AreSame(getMock, serviceMock);

you create two different objects and they will never be the same reference. There is no way the automock container can know about the instance you created without using the container. However, this will succeed:

var mock1 = container.GetMock<IService>();
var mock2 = container.GetMock<IService>();
Assert.AreSame(mock1, mock2);

Not being able to create mocks from abstract types is a bug, but should be fixed now. If you update to v2.1.0 it should hopefully work as expected.

Hope this helps!

-Thomas

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