AutoFixture:PropertyData和异构的参数 [英] AutoFixture: PropertyData and heterogeneous parameters

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

问题描述

考虑下面的测试



  [理论] 
[PropertyData(GetValidInputForDb)]
酒店的公共空间GivenValidInputShouldOutputCorrectResult(
串patientId
,串patientFirstName

{
变种夹具=新灯();

VAR SUT = fixture.Create< HtmlOutputBuilder>();

sut.DoSomething();
//更多代码
}



我想封装在灯具的创作自己的阶级,一个类似于:

  [理论] 
[CustomPropertyData(GetValidInputForDb)]
公共无效GivenValidInputShouldOutputCorrectResult(
串patientId
,串patientFirstName
,HtmlOutputBuilder SUT

{
sut.DoSomething();
//更多代码
}

的问题是,我使用 PropertyData ,后者提供两个输入参数。事实上,我在想,然后作为参数引起异常自动创建我的夹具。



下面是CustomPropertyData:

 公共类CustomPropertyDataAttribute:CompositeDataAttribute 
{
公共CustomPropertyDataAttribute(字符串ValidInput的)
:基地(新数据属性[]
{
新PropertyDataAttribute(ValidInput的),
新AutoDataAttribute(新灯( )
.Customize(新HtmlOutpuBuilderTestConvention())),
})
{

}
}

什么是解决呢?


解决方案
<选项p>您需要将数据提供给 PropertyDataAttribute 如下:

 公共静态的IEnumerable<对象[]> GetValidInputForDb 
{
得到
{
收益返回新对象[]
{
123,
ABC
};
}
}



patientId 值将是 123 的,在 patientFirstName 值将是 ABC 的和SUT值将是通过AutoFixture自动提供。



CustomPropertyDataAttribute 看起来不错。


Given the following test:

[Theory]
[PropertyData("GetValidInputForDb")]
public void GivenValidInputShouldOutputCorrectResult(
    string patientId
    , string patientFirstName
)
{
    var fixture = new Fixture();          

    var sut = fixture.Create<HtmlOutputBuilder>();

    sut.DoSomething();
    // More code
}

I want to encapsulate fixture creation in its own class, something akin to:

[Theory]
[CustomPropertyData("GetValidInputForDb")]
public void GivenValidInputShouldOutputCorrectResult(
    string patientId
    , string patientFirstName
    , HtmlOutputBuilder sut
)
{
    sut.DoSomething();
    // More code
}

The problem is that I'm using PropertyData and the latter is supplying two input parameters. The fact that I'm then trying to automatically create my fixture as a parameter is causing an exception.

Here is the CustomPropertyData:

public class CustomPropertyDataAttribute : CompositeDataAttribute
{
    public CustomPropertyDataAttribute(string validInput)
        :base(new DataAttribute[]
            {
                new PropertyDataAttribute(validInput),
                new AutoDataAttribute(new Fixture()
                    .Customize(new HtmlOutpuBuilderTestConvention() )), 
            })
    {

    }
}

What are the options to resolve this?

解决方案

You need to supply data to the PropertyDataAttribute as below:

public static IEnumerable<object[]> GetValidInputForDb 
{
    get
    {
        yield return new object[]
        {
            "123", 
            "abc"
        };
    }
}

The patientId value will be 123, the patientFirstName value will be abc and the SUT value is going to be supplied automatically by AutoFixture.

The CustomPropertyDataAttribute looks good.

这篇关于AutoFixture:PropertyData和异构的参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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