I have a basic CRUD application written in ASP.NET Core, with an example controller action looking like so:
public IActionResult Sheet(Guid characterId)
{
var character = _repository.GetCharacter(characterId);
// omit validation for brevity
return View("Sheet", new ViewModel(character, true, true));
}
And an example unit test:
[Fact]
public void TestSheet()
{
// Arrange stuff and mock setup
// Act
var result = _controller.Sheet(characterId) as ViewResult;
// Assert
Assert.NotNull(result);
// assert ViewModel constructor called with x arguments
}
Other research seems to suggest having a mock of the object, but that would not be appropriate for this scenario.
I could simply have the constructor for ViewModel
set a couple of getter properties, but that would seem to be delving into the realm of testing ViewModel
as opposed to the controller. The controller should not care about what the ViewModel does with the values that are passed to it, and in my opinion neither should the test - the test is testing that the controller behaves properly, and should really only test that it is passing the correct values. A couple of getter properties would certainly achieve this but it just seems conceptually wrong.