可以使用Autodata xUnit理论对注入的SUT的特定构造函数参数进行设置吗? [英] Can a particular constructor argument of an injected SUT using Autodata xUnit Theories?

查看:94
本文介绍了可以使用Autodata xUnit理论对注入的SUT的特定构造函数参数进行设置吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


考虑以下测试,

Consider the following test,

[Theory, MyConventions]
public void GetClientExtensionReturnsCorrectValue(BuilderStrategy sut)
{
    var expected = ""; // <--??? the value injected into BuilderStrategy
    var actual = sut.GetClientExtension();
    Assert.Equal(expected, actual);
}

以及我正在使用的自定义属性:

and the custom attribute I'm using:

public class MyConventionsAttribute : AutoDataAttribute {
    public MyConventionsAttribute()
        : base(new Fixture().Customize(new AutoMoqCustomization())) {}
}

和SUT:

class BuilderStrategy {
    private readonly string _clientID;
    private readonly IDependency _dependency;
    public void BuilderStrategy(string clientID, IDependency dependency) {
        _clientID = clientID;
        _dependency = dependency;      
    }
    public string GetClientExtension() {
        return _clientID.Substring(_clientID.LastIndexOf("-") + 1);
    }
}

我需要知道向构造函数中注入了什么值参数 clientID ,以便我可以将其与 GetClientExtension 的输出进行比较。如果仍然可以在将SUT注入到测试方法中的情况下编写这种测试样式,是否可以这样做?

I need to know what value was injected into the constructor parameter clientID so that I can use it to compare with the output of GetClientExtension. Is it possible to do this while still writing this style of test where the SUT is injected into the test method?

推荐答案

将注入的 clientID (以及依赖项)公开为只读属性,您始终可以查询其值:

If you expose the injected clientID (and dependency as well) as read-only properties, you can always query their values:

public class BuilderStrategy {
    private readonly string _clientID;
    private readonly IDependency _dependency;
    public void BuilderStrategy(string clientID, IDependency dependency) {
        _clientID = clientID;
        _dependency = dependency;      
    }
    public string GetClientExtension() {
        return _clientID.Substring(_clientID.LastIndexOf("-") + 1);
    }

    public string ClientID
    {
        get { return _clientID; }
    }

    public IDependency Dependency
    {
        get { return _dependency; }
    }
}

不会破坏封装,但被称为结构检查

This doesn't break encapsulation, but is rather known as Structural Inspection.

有了此更改,您现在可以像这样重写测试:

With this change, you could now rewrite the test like this:

[Theory, MyConventions]
public void GetClientExtensionReturnsCorrectValue(BuilderStrategy sut)
{
    var expected = sut.ClientID.Substring(sut.ClientID.LastIndexOf("-") + 1);
    var actual = sut.GetClientExtension();
    Assert.Equal(expected, actual);
}

有些人不喜欢在单元测试中复制生产代码,但是我宁愿争辩说,如果您遵循测试驱动的开发,则是生产代码复制了测试代码。

Some people don't like duplicating production code in the unit test, but I would rather argue that if you follow Test-Driven Development, it's the production code that duplicates the test code.

无论如何,这是一种称为导出值。我认为,,只要它保持1的圈复杂度,我们可以仍然相信测试。此外,只要重复的代码仅出现在两个位置,则三个规则建议我们应该保持这种状态。

In any case, this is a technique known as Derived Value. In my opinion, as long as it retains a cyclomatic complexity of 1, we can still trust the test. Additionally, as long as the duplicated code only appears in two places, the rule of three suggests that we should keep it like that.

这篇关于可以使用Autodata xUnit理论对注入的SUT的特定构造函数参数进行设置吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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