jeudi 3 décembre 2015

Use the same test data for multiple runs of xUnit Theory

I'm using xUnit to unit test my application and am currently setting up a test to use the [Theory] attribute to test several different data inputs.

In order to do so I need to create test data in my previously mocked data context. This works, but when I add the data in the test itself each run ends up with the same data being added again.

My current test:

[Theory]
[InlineData(null, 2)]
[InlineData("en-AU", 1)]
public void Test1(string term, int expectedCount)
{
    Fixture.DbContext.Culture.Add(new Culture { Name = "English (Australia)", CultureCode = "en-AU", NativeName = "English (Australia)"});
    Fixture.DbContext.Culture.Add(new Culture { Name = "English (United States)", CultureCode = "en-US", NativeName = "English (United States)" });
    Fixture.DbContext.Culture.Add(new Culture { Name = "English", CultureCode = "en", NativeName = "English", NeutralCultureFlag = true });

    var result = controller.GetRegions(term);

    Assert.IsType(typeof (JsonResult), result);
    var jsonResult = (JsonResult)result;

    Assert.Equal(expectedCount, jsonResult.Data);
}

Is there a way of only setting up the test data once for each run of InlineData? I know I could put it in the constructor of the test class but I would prefer not to do this as this seems unnecessary if this is the only test in the class that uses that data.

Aucun commentaire:

Enregistrer un commentaire