Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass Guid.Empty into a parameterized unit test?

I'm attempting to pass Guid.Empty into my unit test:

[TestCase(null)]
[TestCase(Guid.Empty)]
public void When_AccountGuid_IsNullOrEmpty_AddError_Is_Invoked(Guid? accountGuid)
{

}

However the compiler is complaining:

An attribute argument must be a constant expression, typeof expression or array creation expression of an attribute parameter type

How do I pass Guid.Empty into my unit test method?

I've also tried to factor it out by declaring it as a private member in the class, with the same compiler error:

        private static Guid _emptyGuid = Guid.Empty;
        [TestCase(null)]
        [TestCase(_emptyGuid)]
        public void When_AccountGuid_IsNullOrEmpty_AddError_Is_Invoked(Guid? accountGuid)
        {

        }
like image 676
Alex Gordon Avatar asked Dec 19 '22 13:12

Alex Gordon


1 Answers

In this situation use the TestCaseSource atribute to define the list of cases.

static Guid?[] NullAndEmptyGuid = new Guid?[] { null, Guid.Empty };

[TestCaseSource("NullAndEmptyGuid")]
public void When_AccountGuid_IsNullOrEmpty_AddError_Is_Invoked(Guid? accountGuid)
{

}
like image 129
Scott Chamberlain Avatar answered Dec 24 '22 01:12

Scott Chamberlain