简单的方法来指定一个构造函数参数的值? [英] Easy way to specify the value of a single constructor parameter?

查看:142
本文介绍了简单的方法来指定一个构造函数参数的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有使用它的构造在Autofixture一些简单的方法来新建立一个对​​象,但硬code /指定要使用的值在构造一个参数?

Is there some easy way in Autofixture to new-up an object using it's constructor, but hard-code/specify the value to use for a single parameter in the constructor?

我看这可以用属性来实现,使用类似:

I see that this can be done with properties, using something like:

fixture.Build< MyObj中方式>()和(X = x.Val,硬codeD值)的Create()

推荐答案

从讨论有关在评论这个问题:

From the discussion about this question in the comments:

我经常要新建立一个对​​象,但我只关心一个参数。也许参数值驱动计算或某种变换。我想本身的价值是随机的,但测试需要知道什么被选为随机值。

Often I want to new-up an object, but I'm only concerned about a single parameter. Maybe the parameter value drives a calculation or transform of some sort. I want the value itself to be random, but the test needs to know what random value was chosen.

假设这是潜在的问题,我会尝试解决这一问题。

Assuming that this is the underlying problem, I'll attempt to address this issue.

询问对象

最简单的方法是简单地询问对象的值是什么,AutoFixture创建后:

The easiest solution is to simply ask the object what the value is, after AutoFixture created it:

var fixture = new Fixture();
var mc = fixture.Create<MyClass>();
var specialValue = mc.SpecialValue;
// Do something with specialValue...

如果你只需要知道的特殊价值是什么,但你并不需要它有一个特定的值,可以使用这种技术。

If you just need to know what the special value is, but you don't need it to have a particular value, you can use this technique.

这显然需要您从构造函数的参数为​​(只读)属性,但是这的一个好主意,做反正

This obviously requires you to expose the value from the constructor parameter as a (read-only) property, but that's a good idea to do anyway.

创建后分配值

如果您需要的参数有一个特定的值,你可以创建后分配给它:

If you need the parameter to have a specific value, you can assign it after creation:

var fixture = new Fixture();
var mc = fixture.Create<MyClass>();
mc.SpecialValue = "Some really special value";

这需要你做出可作为可写属性的值。虽然我个人没有这方面的一个大风扇,因为这使对象可变的,很多人都已经设计自己的对象的方式,如果是这样的话,为什么不好好利用这个优势?

This requires you to make the value available as a writeable property. While I'm personally not a big fan of that, because that makes the object mutable, many people already design their objects that way, and if that's the case, why not take advantage of it?

使用创建后复制和更新

如果你希望你的对象是不变的,仍然可以使用上述技术的一个变种。在F#中,可以使用所谓的副本和更新前pressions 来达到同样的目的。在F#中,它会是这样的:

If you want your objects to be immutable, you can still use a variation of the above technique. In F#, you can use so-called copy and update expressions to achieve the same goal. In F#, it'd be something like:

let fixture = Fixture ()
let mc = {
    fixture.Create<MyRecord> ()
    with SpecialValue = "Some special value!" }

您可以通过给你的类在C#中模拟这种复制和更新的方法,所以你可以这样写:

You can emulate this in C# by giving your classes copy-and-update methods, so you'd be able to write something like this:

var fixture = new Fixture();
var mc = fixture
    .Create<MyClass>()
    .WithSpecialValue("Some really special value");

这是一种技术,我用所有的时间在我的C#code。 AutoFixture有<一个href=\"http://www.nudoq.org/#!/Packages/AutoFixture.Idioms/Ploeh.AutoFixture.Idioms/CopyAndUpdateAssertion\"相对=nofollow>一个地道的断言来测试这样的复制和更新的方法的。

This is a technique I use all the time in my C# code. AutoFixture has an idiomatic assertion to test such copy and update methods.

注入一个值的参数

如果你可以为特定的构造函数的参数注入一个固定值活,你可以用AutoFixture内核的基石做到这一点。此测试演示如何:

If you can live with injecting a fixed value for a particular constructor parameter, you can do this with the building blocks of the AutoFixture kernel. This test demonstrates how:

[Fact]
public void CustomizeParameter()
{
    var fixture = new Fixture();
    fixture.Customizations.Add(
        new FilteringSpecimenBuilder(
            new FixedBuilder("Ploeh"),
            new ParameterSpecification(
                typeof(string),
                "specialValue")));

    var actual = fixture.Create<MyClass>();

    Assert.Equal("Ploeh", actual.SpecialValue);
}

不过,这会导致参数的总是的是Ploeh为灯具实例。这也是重构不安全的,因为它是一个字符串指的是参数的名称基地。

However, this causes that parameter to always be "Ploeh" for that fixture instance. It's also refactoring-unsafe, since it's base on a string referring to the name of the parameter.

这篇关于简单的方法来指定一个构造函数参数的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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