1

我已经为一个类编写了一个构造函数,并且我正在测试每个参数是否为空。请参见下面的示例:

public MyClass(IObjectA objA, IObjectB objB) : IMyClass
{
    if (objA == null)
    {
        throw new ArgumentNullException("objA");
    }

    if (objB == null)
    {
        throw new ArgumentNullException("objB");
    }

    ...
}

IObjectA通常我通过模拟并将它们传入来对它进行单元测试(使用 Moq)IObjectB。上面的示例将创建 2 个单元测试来测试每个场景。

我遇到的问题是当第三个参数传递给构造函数时。它要求我改变我以前的测试,因为我突然得到一个“MyClass 的没有构造函数有 2 个参数”类型异常。

我也使用 AutoMockContainer。本质上,我希望能够通过在容器中注册一个空对象来测试构造函数。例如:

[TestMethod]
[ExpectedException(typeof(ArgumentNullException))]
public void ConstructionThrowsExceptionForNullObjA()
{
    // Arrange.
    var container = new AutoMockContainer(new MockRepository(MockBehavior.Default));

    container.Register<IObjectA>(null);

    // Act.
    var sut = container.Create<MyClass>();
}

那么在构造函数中添加多少新参数并不重要。我不必更新我的单元测试。

可悲的是,上述单元测试通过了。但是出于错误的原因。该Register<T>()方法抛出ArgumentNullException不是在“Act”部分中执行的代码。

有没有人建议能够测试构造函数参数而不必在以后添加新参数时重新访问单元测试?

4

1 回答 1

3

您可以通过使用工厂模式或构建器模式来创建对象来帮助减轻这些负担。

构建器模式的一个简化示例是:

public class Foo
{
    public string Prop1 { get; private set; }

    public Foo(string prop1)
    {
        this.Prop1 = prop1;
    }
}

[TestClass]
public class FooTests
{
    [TestMethod]
    public void SomeTestThatRequiresAFoo()
    {
        Foo f = new Foo("a");
        // testy stuff
    }

    [TestMethod]
    public void SomeTestThatRequiresAFooUtilizingBuilderPattern()
    {
        Foo f = new FooBuilder().Build();
    }

    [TestMethod]
    public void SomeTestThatRequiresAFooUtilizingBuilderPatternOverrideDefaultValue()
    {
        Foo f = new FooBuilder()
           .WithProp1("different than default")
           .Build();
    }
}

internal class FooBuilder
{

    public string Prop1 { get; private set; }

    // default constructor, provide default values to Foo object
    public FooBuilder()
    {
        this.Prop1 = "test";
    }

    // Sets the "Prop1" value and returns this, done this way to create a "Fluent API"
    public FooBuilder WithProp1(string prop1)
    {
        this.Prop1 = prop1;
        return this;
    }

    // Builds the Foo object by utilizing the properties created as BuilderConstruction and/or the "With[PropName]" methods.
    public Foo Build()
    {
        return new Foo(
            this.Prop1
        );
    }
}

这样,如果/当您的 Foo 对象更改时,更新单元测试以将更改考虑在内会更容易一些。

考虑:

public class Foo
{
    public string Prop1 { get; private set; }
    public string Prop2 { get; private set; }    

    public Foo(string prop1, string prop2)
    {
        this.Prop1 = prop1;
        this.Prop2 = prop2
    }
}

使用此实现,您的单元测试将中断,但更新您的构建器比更新每个单元测试更容易依赖于 Foo 的正确构造

internal class FooBuilder
{

    public string Prop1 { get; private set; }
    public string Prop2 { get; private set; }

    // default constructor, provide default values to Foo object
    public FooBuilder()
    {
        this.Prop1 = "test";
        this.Prop2 = "another value";
    }

    // Sets the "Prop1" value and returns this, done this way to create a "Fluent API"
    public FooBuilder WithProp1(string prop1)
    {
        this.Prop1 = prop1;
        return this;
    }

    // Similar to the "WithProp1"
    public FooBuilder WithProp2(string prop2)
    {
        this.Prop2 = prop2;
        return this;
    }

    // Builds the Foo object by utilizing the properties created as BuilderConstruction and/or the "With[PropName]" methods.
    public Foo Build()
    {
        return new Foo(
            this.Prop1,
            this.Prop2
        );
    }
}

使用 Foo 和 FooBuilder 的这种新实现,唯一会破坏的单元测试是手动创建 Foo 的单元测试,使用单元测试的 FooBuilder 仍然可以正常工作。

这是一个简化的示例,但想象一下,如果您有 20-30 个单元测试依赖于 Foo 对象的构造。无需更新那些 20-30 个单元测试,您只需更新您的构建器以正确构造 Foo 对象。

在您在构造函数中对 null 进行单元测试的示例中,您可以使用构建器模式编写单元测试,如下所示:

[TestMethod]
public void TestWithNullInFirstParam()
{
    Foo f = new FooBuilder()
        .WithProp1(null)
        .Build()

    // in this case "f" will have Prop1 = null, prop2 = "another value"
}  
于 2015-01-26T15:09:55.913 回答