mercredi 28 janvier 2015

Test several variations of data setup with MSpec

We are using Machine.Specification to as our test framework on my current project and this works well for most of our test for what we are testing. However, we have a number of view models where we have 'formatted' properties on them that take some raw data, apply some logic to it and return a formatted version of that data. Since there is logic involved in the formatting (e.g. null checks, special cases for zero, etc...) I want to write tests that test a number of possible data values including boundary conditions. To me, this doesn't feel like the right use case for MSpec and that we should drop down into something like NUnit where I can write a data-driven test using something like the [TestCase] attribute. Is there a clean, simple way to write this kind of test in MSpec, or am I right in my feeling that we should be using a different tool for this kind of test?


View Model Class



public class DwellingInformation
{

public DateTime? PurchaseDate { get; set; }
public string PurchaseDateFormatted
{
if(PurchaseDate == null)
return "N/A";

return PurchaseDate.Value.ToShortDateString();
}

public int? ReplacementCost { get; set; }
public string ReplacementCostFormatted
{
if(ReplacementCost == null)
return "N/A";

if(ReplacementCost == 0)
return "Not Set";

return ReplacementCost.ToString("C0");
}

// a bunch more properties that have been left our for brevity
}


Some of the corresponding MSpec tests



public class When_ReplacementCost_is_null
{
private static DwellingInformation information;

private Establish context = () =>
{
information = new DwellingInformation { ReplacementCost = null };
};

private It ReplacementCostFormatted_should_be_Not_Available = () => information.ReplacementCostFormatted.ShouldEqual("N/A");
}

public class When_ReplacementCost_is_zero
{
private static DwellingInformation information;

private Establish context = () =>
{
information = new DwellingInformation { ReplacementCost = "0" };
};

private It ReplacementCostFormatted_should_be_Not_Set = () => information.ReplacementCostFormatted.ShouldEqual("Not Set");
}

public class When_ReplacementCost_is_a_non_zero_value
{
private static DwellingInformation information;

private Establish context = () =>
{
information = new DwellingInformation { ReplacementCost = 200000 };
};

private It ReplacementCostFormatted_should_be_formatted_as_currency = () => information.ReplacementCostFormatted.ShouldEqual("$200,000");
}


The same tests in NUnit using [TestCase]



[TestCase(null, "N/A")]
[TestCase(0, "Not Set")]
[TestCase(200000, "$200,000")]
public void ReplacementCostFormatted_Correctly_Formats_Values(int? inputVal, string expectedVal)
{
var information = new DwellingInformation { ReplacementCost = inputVal };

information.ReplacementCostFormatted.ShouldEqual(expectedVal);
}


Is there a better way to write the MSpec tests that I'm missing because I'm just not familiar enough with MSpec yet, or is MSpec really just the wrong tool for the job in this case?


NOTE: Another Dev on the team feels we should write all of our tests in MSpec because he doesn't want to introduce multiple testing frameworks into the project. While I understand his point, I want to make sure we are using the right tool for the right job, so if MSpec is not the right tool, I'm looking for points I can use to argue the case for introducing another framework.


Aucun commentaire:

Enregistrer un commentaire