AutoFixture作为Automocking容器VS Automocking区别? [英] AutoFixture as an Automocking container vs Automocking differences?

查看:252
本文介绍了AutoFixture作为Automocking容器VS Automocking区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开始使用起订量,但是从我的理解,我总是嘲笑了所有可以被称为就算我真的不关心他们的方法。

有时候需要很长时间来样机的东西,你忘了你想做的事情。所以我一直在寻找汽车嘲讽,但我不知道我应该用什么人。

AutoFixture为自动嘲讽容器

Automocking

我不明白如何使用第一个在所有。我的排序得到第二个,但从来没有真正尝试过。

我不知道,如果一个是优于其他。我唯一​​知道的是我使用AutoFixtures已经什么是第一位的依赖关系。

所以,也许从长远来看,这是有道理的去与第一个,但就像我说我不能找到如何使用它的任何基本教程。

修改

我试图按照尼科斯Baxevanis的例子,但我遇到了错误。

 故障信息:System.ArgumentException:是不是在嘲笑类型找到匹配的构造函数在给定的参数。
  ----> System.MissingMethodException:构造函数型DatabaseProxyded46c36c8524889972231ef23659a72'未找到。


。VAR夹具=新灯()自定义(新AutoMoqCustomization());
        VAR fooMock = fixture.Freeze<模拟<的IFoo>>();
       // fooMock.Setup(X => x.GetAccounts(It.IsAny&其中; IUnitOfWork>()));
        VAR SUT = fixture.CreateAnonymous< AdminService的>();

        sut.Apply();
        fooMock.VerifyAll();
 

我想这是因为我petapoco的UnitOfWork财产

  PetaPoco.Database分贝{获得; }
 

不知道如果我以某种方式或者是嘲笑这件事。

解决方案

虽然我从来没有使用 MOQ-contrib请Automocking ,我大概可以提供有关使用某些信息<一个HREF =htt​​ps://github.com/autofixture/autofixture> AutoFixture 为自动嘲讽的容器。

目前存在的起订量,犀牛制品,FakeItEasy和NSubstitute支持。只要安装相应的扩展 AutoMoq ,的 AutoRhinoMocks AutoFakeItEasy 和的 AutoNSubstitute

一旦你安装了扩展自动嘲讽之一的额外的电话是:

  VAR夹具=新灯()
    .Customize(新AutoMoqCustomization());
 

(或者,如果您使用的是犀牛制品)

  VAR夹具=新灯()
     .Customize(新AutoRhinoMockCustomization());
 

(或者,如果您使用的是FakeItEasy)

  VAR夹具=新灯()
     .Customize(新AutoFakeItEasyCustomization());
 

(或者,如果您使用的是NSubstitute)

  VAR夹具=新灯()
     .Customize(新AutoNSubstituteCustomization());
 

示例1

 公共类myController的:IController
{
    公共myController的(的IFoo富)
    {
    }
}

公共接口IFoo的
{
}
 

下面是如何利用AutoFixture创建 myController的类的实例:

  VAR夹具=新灯()
    .Customize(新AutoMoqCustomization());

VAR SUT = fixture.CreateAnonymous&LT;&myController的GT;();
 

现在,如果你检查 SUT 变量,你会看到的的IFoo 是一个嘲弄实例的 (具有类似Castle.Proxies.IFooProxy类型名)的。

例2

这个例子扩展了previous之一。

您可以指示AutoFixture使用自己的,pre配置,嘲笑例如:

  VAR fooMock = fixture.Freeze&LT;模拟&LT;的IFoo&GT;&GT;();
//此时,你可以设置期望的fooMock(S)。

VAR SUT = fixture.CreateAnonymous&LT;&myController的GT;();
//这个实例现在使用已创建的fooMock。
//验证任何期望(S)。
 


这基本上它 - 但它可以走得更远。

下面是使用AutoFixture decleratively 与xUnit.net <在previous例子A HREF =htt​​p://nuget.org/packages/AutoFixture.Xunit/>扩展。

示例1

  [理论,AutoMoqData]
公共无效TestMethod的(SUT myController的)
{
    //开始直接使用SUT实例。
}
 

例2

  [理论,AutoMoqData]
公共无效TestMethod的([冰冻]模拟&LT;的IFoo&GT; fooMock,SUT myController的)
{
   //此时,你可以设置期望的fooMock(S)。
   // SUT的实例现在使用已创建的fooMock。
   //验证任何期望(S)。
}
 

  

您可能会发现更多信息这的博客张贴这包含链接到周围的一切AutoFixture,xUnit.net,以及相关汽车嘲讽。

希望有所帮助。

I started to use moq but from my understanding I always have to mock up all the methods that could be called even if I really do not care about them.

Sometimes it takes so long to mockup stuff you forget what you want to do. So I been looking at auto mocking but I am not sure what one I should use.

AutoFixture as an auto-mocking container

Automocking

I don't get how to use the first one at all. I sort of get the second one but never really tried it.

I am not sure if one is better than the other. The only thing I know is I am using AutoFixtures already what is a dependency of the first one.

So maybe in the long run it makes sense to go with the first one but like I said I can't find any basic tutorials on how to use it.

Edit

I am trying to follow "Nikos Baxevanis" examples but I am running into errors.

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


var fixture = new Fixture().Customize(new AutoMoqCustomization());
        var fooMock = fixture.Freeze<Mock<IFoo>>();
       // fooMock.Setup(x => x.GetAccounts(It.IsAny<IUnitOfWork>()));
        var sut = fixture.CreateAnonymous<AdminService>();

        sut.Apply();
        fooMock.VerifyAll();

I think it is because of my petapoco unitOfWork property

PetaPoco.Database Db { get; }

Not sure if I got to mock this up somehow or what.

解决方案

While I have never used moq-contrib Automocking, I could probably provide some information on using AutoFixture as an auto-mocking container.

Currently there is support for Moq, Rhino Mocks, FakeItEasy, and NSubstitute. Just install the appropriate extension AutoMoq, AutoRhinoMocks, AutoFakeItEasy, and AutoNSubstitute.

Once you have installed one of the extensions for Auto Mocking the extra call is:

var fixture = new Fixture()
    .Customize(new AutoMoqCustomization());

(or if you are using Rhino Mocks)

var fixture = new Fixture()
     .Customize(new AutoRhinoMockCustomization());

(or if you are using FakeItEasy)

var fixture = new Fixture()
     .Customize(new AutoFakeItEasyCustomization());

(or if you are using NSubstitute)

var fixture = new Fixture()
     .Customize(new AutoNSubstituteCustomization());

Example 1

public class MyController : IController
{
    public MyController(IFoo foo)
    {
    }
}

public interface IFoo
{
}

Here is how to use AutoFixture to create instances of MyController class:

var fixture = new Fixture()
    .Customize(new AutoMoqCustomization());

var sut = fixture.CreateAnonymous<MyController>();

Now, if you inspect the sut variable you will see the the IFoo is a mocked instance (having a type name similar to Castle.Proxies.IFooProxy).

Example 2

This examples extends the previous one.

You can instruct AutoFixture to use your own, pre-configured, mocked instance:

var fooMock = fixture.Freeze<Mock<IFoo>>();
// At this point you may setup expectation(s) on the fooMock.

var sut = fixture.CreateAnonymous<MyController>();
// This instance now uses the already created fooMock.
// Verify any expectation(s).


That's basically it - but it can go further!

Below are the previous examples using AutoFixture decleratively with the xUnit.net extension.

Example 1

[Theory, AutoMoqData]
public void TestMethod(MyController sut)
{
    // Start using the sut instance directly.
}

Example 2

[Theory, AutoMoqData]
public void TestMethod([Frozen]Mock<IFoo> fooMock, MyController sut)
{
   // At this point you may setup expectation(s) on the fooMock.
   // The sut instance now uses the already created fooMock.
   // Verify any expectation(s).
}

You may find more information on this blog post which contains links to everything related around AutoFixture, xUnit.net, and Auto Mocking.

Hope that helps.

这篇关于AutoFixture作为Automocking容器VS Automocking区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆