umutesen
umutesen

Reputation: 2640

C# unit tests: Is it OK to use conditional access instead of Debug.Assert?

So I have the following unit test for a controller action (MVC 5). The purpose of the test is to ensure that the Edit action of the controller returns a model of type Contact:

    [TestMethod]
    public void Edit_ValidContactIdPassed_ShouldReturnEditViewWithContact()
    {
        var result = _controller.Edit(1) as ViewResult;
        result?.ViewData.Model.Should().BeOfType<Contact>();
    }

As you can see I am using the conditional access(?) on the result object instead of:

        Debug.Assert(result != null, "result != null");

I believe the conditional access is more readable but is there any issues with this within the context of a unit test?

Upvotes: 0

Views: 247

Answers (1)

Sid
Sid

Reputation: 14916

Well if you are testing the var result and test must be negative if it is == null in this case you will prevent the error from being fired as with the conditional access you prevent a potential NullPointerException

Assert.IsNotNull(result);

It's not that bad after all :)!

Upvotes: 1

Related Questions