我怎么AutoFixture AutoMoq从注入的服务实例化对象返回的结果? [英] How do I get AutoFixture AutoMoq to return results from injected services in an instantiated object?

查看:213
本文介绍了我怎么AutoFixture AutoMoq从注入的服务实例化对象返回的结果?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图测试服务类消费者存储服务。我已成立了,我相信应该与我的资料库服务工作,而是返回默认的匿名结果自定义。如果你看一下下面的代码示例中,我试图让我在定制类注册回来时,我称之为svc.GetFoos方法foo的对象,而不是我得到什么:

I am trying to test a service class that consumers a repository service. I have customizations set up that I believe should work with my repository service, but instead return default Anonymous results. If you look at the code sample below, I'm trying to get the "Foo" objects that I registered in the Customization Class back when I call the svc.GetFoos method, instead I get nothing:

void Main()
{
    var fixture = new Fixture().Customize(
        new CompositeCustomization(
            new Customization(),
            new AutoMoqCustomization())); 

    var svc = fixture.CreateAnonymous<Bar>(); 

    Console.Write(svc.GetFoos().Count()); 
}

// Define other methods and classes here
public class Bar
{

    public IQueryable<Foo> GetFoos()
    {
        return _rep.Query<Foo>(); 
    }

    public Bar(IRepository rep) { _rep = rep;  }

    private IRepository _rep; 
}

public class Foo
{
    public string Name {get;set;}   
}

public class Customization : ICustomization
{
    public void Customize(IFixture fixture)
    {
        var f = fixture
                .Build<Foo>()
                .With(x => x.Name, "FromCustomize")
                .CreateMany(2)
                .AsQueryable();
        fixture.Register<IQueryable<Foo>>(() => f); 
    }
}

public interface IRepository
{
    IQueryable<T> Query<T>(); 
}

如果我下面的代码添加到Main方法夹具实例化后,它我的作品怎么想,但后来我手动设置我的嘲笑,我不知道什么AutoFixture AutoMoq是让我:

If I add the following code to the Main method after the fixture instantiation, it works how I want, but then I'm manually setting up my mocks, and I'm not sure what AutoFixture AutoMoq is getting me:

var mock = fixture.Freeze<Mock<IRepository>>(); 
mock
    .Setup(x => x.Query<Foo>())
    .Returns(fixture.CreateAnonymous<IQueryable<Foo>>); 



感谢。

Thanks.

推荐答案

AutoFixture.AutoMoq运作作为的自动嘲讽集装箱。 T>它会通过注入模拟<自动组合的对象图。实例进入所述的任何消费 T

AutoFixture.AutoMoq works as an Auto-Mocking Container. It'll automatically compose object graphs by injecting Mock<T> instances into any consumer of said T.

这不能配置模拟< T>作为您实例 - 毕竟,这怎么可能呢?只有你(测试作家)知道适当的互动应该是什么。

It can't configure the Mock<T> instances for you - after all, how could it? Only you (the test writer) knows what the appropriate interaction should be.

所以你目前的代码,包括呼叫设置返回,是正确的,虽然你可能会考虑定制类是否是矫枉过正。

So the code you present, including the calls to Setup and Returns, is correct, although you may consider whether or not the Customization class is overkill.

如果您需要自动化很多起订量重复设置的,你应该考虑

If you need to automate a lot of repetitious setup of Moq, you should consider


  • 界面设计和消费模式是否合适

  • 如果一个的不会比一个动态模拟

  • whether the interface design and the consumption pattern is appropriate
  • if a Fake wouldn't be a better option than a dynamic mock

这篇关于我怎么AutoFixture AutoMoq从注入的服务实例化对象返回的结果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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