mercredi 24 octobre 2018

Passing object in MSTest method parameters

I'm developing a battleship game in ASP.NET and I'm stucked on a problem with my unit tests with MSTest.

I want to test the creation of each type of boat and verifying that each boat's constructor make the desired boat with the good width, etc. So, I decided to write a general method with the tag [DataTestMethod]. But I don't understand how I can use an object as a parameter.

Here's an example of what I want :

[DataTestMethod]
[DataRow("Aircraft Cruiser", 5, OccupationType.Aircraft, new Aircraft())]
public void CreateAircraft(string description, int width, OccupationType occupationType, Ship ship)
{
    Assert.AreEqual(description, ship.Description);
    Assert.AreEqual(width, ship.Width);
    Assert.AreEqual(occupationType, ship.OccupationType);
}

But it obvliously doesn't work. So I did something like that :

[DataTestMethod]
[DataRow("Aircraft Cruiser", 5, OccupationType.Aircraft, "Aircraft")]
public void CreateAircraft(string description, int width, OccupationType occupationType, string shipType)
{
    Ship ship = null;
    switch (shipType)
    {
        case "Aircraft":
            ship = new Aircraft();
            break;
    }
    Assert.AreEqual(description, ship.Description);
    Assert.AreEqual(width, ship.Width);
    Assert.AreEqual(occupationType, ship.OccupationType);
}

but I'm sure that's not the most efficent way to do what I want. Have you some idea ?

Many thanks.

Aucun commentaire:

Enregistrer un commentaire