本文介绍了AutoFixture和私有财产的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以指示也填补私人财产,具有特定属性的注解,如 Ninject.Inject ,所有类的?该人士似乎扫描唯一的公共属性:。这个问题提供了具体的解决方案 MyClass的私人二传手,而不是私人财产或全部类别:的。



我使用起订量嘲笑服务,并在结束时,我想,以填补那些嘲笑的属性。如果我揭露的MyService依赖如下设置正常工作公共



一些示例代码:



 公共类{myController的
[注入]
私人IMyService为MyService {搞定;组; }

公共无效AMethodUsingMyService(){
MyService.DoSomething();
// ...
}

// ...
}

公共类的MyService:IMyService {
公共无效DoSomething的()
{
// ...
}

// ...
}

公共类MyControllerTest {
[理论]
[AutoMoqData]
公共无效MyTest的(控制器myController的){
controller.AMethodUsingMyService();
}
}


解决方案

AutoFixture不具备非公开字段或属性赋值内置支持。这是由设计。



AutoFixture是一个单元测试工具库,和单元测试的的的。



AutoFixture最初是建立对于测试驱动开发(TDD)的工具,而TDD是所有关于反馈。在的精神,你应该听你的测试。如果测试很难写,你应该考虑你的API设计。 AutoFixture趋于放大,诸如此类的反馈,所以我的第一反应就是要挑战的动机想要做到这一点。



既然你引用NInject,它看起来好像你使用依赖注入(DI)。然而,DI与私有财产注射听起来充满异国情调。 。考虑做性能公众,甚至更好,使用构造函数注入,而不是物业注射



这将使AutoFixture像一个自动工作的。



不过,AutoFixture是一个非常扩展库,所以如果你真的必须的做到这一点,应该可以写,可以写私人性质的延伸,但它不会是有史以来最简单的AutoFixture扩展。


Can I instruct AutoFixture to fill also private properties, annotated with a specific attribute such as Ninject.Inject, of all classes? The source seems to scan for public properties only: 1. This question provides a solution for specific MyClass with private setter, but not for private property or all classes: 2.

I'm using Moq to mock the services and in the end I'd like to fill the properties with those mocks. The following setup works fine if I expose the MyService dependency as public.

Some example code:

public class MyController {
    [Inject]
    private IMyService MyService { get; set; }

    public void AMethodUsingMyService() {
        MyService.DoSomething();
        // ...
    }

    // ...
}

public class MyService : IMyService {
    public void DoSomething()
    {
        // ...
    }

    // ...
}

public class MyControllerTest {
    [Theory]
    [AutoMoqData]
    public void MyTest(MyController controller) {
        controller.AMethodUsingMyService();
    }
}
解决方案

AutoFixture doesn't have built-in support for assigning values to non-public fields or properties. This is by design.

AutoFixture is a utility library for unit testing, and unit tests shouldn't directly invoke private members of the SUT.

AutoFixture was originally build as a tool for Test-Driven Development (TDD), and TDD is all about feedback. In the spirit of GOOS, you should listen to your tests. If the tests are hard to write, you should consider your API design. AutoFixture tends to amplify that sort of feedback, so my first reaction is to challenge the motivation for wanting to do this.

Since you're referencing NInject, it looks as though you're using Dependency Injection (DI). However, DI with private Property Injection sounds exotic. Consider making the properties public, or even better, use Constructor Injection instead of Property Injection.

This will enable AutoFixture to automatically work like an Auto-Mocking Container.

However, AutoFixture is a very extensible library, so if you really must do this, it should be possible to write an extension that can write to private properties, but it's not going to be the simplest AutoFixture extension ever written.

这篇关于AutoFixture和私有财产的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-23 08:37