budi budi - 3 months ago 22
C# Question

NUnit 3.0 TestCase Const Custom Object Arguments

I've written the class

SomeObject
and I want to statically define an instance of this object to keep/reuse for a unittest. How should I rewrite the code below to achieve this behavior?

[TestFixture]
public class SomeObjectTests {

private const SomeObject item0 = new SomeObject(0.0); // doesn't work

[TestCase(item0, ExpectedResult = 0.0)]
public double TestSomeObjectValue(SomeObject so) {
return so.Value;
}

[TestCase(item0, ExpectedResult = "0.0")]
public string TestSomeObjectValueString(SomeObject so) {
return so.ValueString;
}

}


I get the following error message:


A const field of a reference type other than string can only be initialized with null.

Answer

The C# language spec, §10.3 says (emphasis mine):

When a symbolic name for a constant value is desired, but when the type of that value is not permitted in a constant declaration, or when the value cannot be computed at compile-time by a constant-expression, a readonly field (Section 10.4.2) may be used instead.


Annoyingly, this is compounded by the fact that attributes have certain restrictions too - see the C# language spec, §17.2 (again, emphasis mine):

An expression E is an attribute-argument-expression if all of the following statements are true:

  • The type of E is an attribute parameter type (Section 17.1.3).

  • At compile-time, the value of E can be resolved to one of the following:

    • A constant value.

    • A System.Type object.

    • A one-dimensional array of attribute-argument-expressions.

Where §17.1.3: "Attribute parameter types" says1:

The types of positional and named parameters for an attribute class are limited to the attribute parameter types, which are:

  • One of the following types: bool, byte, char, double, float, int, long, short, string.
  • The type object.
  • The type System.Type.
  • An enum type, provided it has public accessibility and the types in which it is nested (if any) also have public accessibility (§17.2).
  • Single-dimensional arrays of the above types.

1: the quoted text is from an older version of the C# specification - in the C# 5.0 version, four additional types are mentioned: sbyte, uint, ulong, and ushort.


In other words, the best you can do is something like:

[TestFixture]
public class SomeObjectTests {

    private static readonly SomeObject item0 = new SomeObject(0.0);

    private static SomeObject getObject(string key) {
        if ( key == "item0" )
            return item0;

        throw new ArgumentException("Unknown key");
    }

    [TestCase("item0", ExpectedResult = 0.0)]
    public double TestSomeObjectValue(string key) {
        SomeObject so = getObject(key);
        return so.Value;
    }

    [TestCase("item0", ExpectedResult = "0.0")]
    public string TestSomeObjectValueString(string key) {
        SomeObject so = getObject(key);
        return so.ValueString;
    }
}

This way, the arguments to the attributes are compile-time constant, and the getObject method can handle getting the SomeObject instance.