mercredi 21 novembre 2018

NUnit test of constructor fails because of missing parameter

I am trying to run a Unit Test using NUnit and have therefore followed a tutorial, which states the following:

[TestFixture]
public class TestFootballplayerController
{
    [Test]
    public void FootBallPlayer_CheckingIfControllerReturnsCorrectView_MustReturnTrue()
    {
        string expected = "CreatePlayer";
        FootballplayerController controller = new FootballPlayerController();

        var result = controller.CreateIngredient() as ViewResult;

        Assert.AreEqual(expected, result.ViewName);
    }
}

The tutorial assumes I have a empty default constructor, but my constructor contains a parameter with my DBContext as follows:

    public class FootballplayerController : Controller
{
    private readonly FootballContext _context;

    public FootballplayerController(FootballContext context)
    {
        _context = context;
    }

    public IActionResult CreatePlayer()
    {
        return View();
    }
}

Visual Studio suggests that I create an empty constructor, but in that way I think I will just test something, that shouldn't be tested, instead of the correct constructor with the parameter.

If the answer is obvious, then I must say, I am new to unit testing, and can't work a way around this. Do I have to fake a parameter?

Aucun commentaire:

Enregistrer un commentaire