Skip to main content
Copy edited (e.g. ref. <en.wiktionary.org/wiki/useful#Adjective> and <en.wiktionary.org/wiki/example#Noun>). It is "response" in the code. [(its = possessive, it's = "it is" or "it has". See for example <www.youtube.com/watch?v=8Gv0H-vPoDc&t=1m20s> and <www.wikihow.com/Use-Its-and-It%27s>.)]
Source Link

Why assert for null on a object before asserting on some of it'sits internals?

Let's consider the following test.

[Fact]
public void MyTest()
{
     // Arrange Code
     var sut = new SystemWeTest();

     // Act Code
     var response = sut.Request();

     // Assert
     response.Should().NotBeNull();
     response.ResponseCode.Should().Be("1");
     response.Errors.Should().BeEmpty();
}

I have argued with a few colleagues that it's pointless to assert that 'response' is not null if you are going to then assert on some of it'sits internals. Of course, if the only thing that you are interested is checking that the object is not null, nothing more, then it's fine.

My thinking is that each following assert statements are in fact implicit assertions that 'response' is not null. If it is null then it would throw a null reference exception and making the test fail as expected.

The only benefit I see from doing this is a somewhat clearer message on why the test failed. You'd get your test framework specific exception that's thrown by the 'NotBeNull''NotBeBull' assertion instead of the generic 'NullReferenceException''nullReferenceException'. I don't feel this is useful as you are getting the same information anyway.

Am I missing something here  ? Does checking for null have any other benefits in the example provided  ?

Why assert for null on a object before asserting on some of it's internals?

Let's consider the following test.

[Fact]
public void MyTest()
{
     // Arrange Code
     var sut = new SystemWeTest();

     // Act Code
     var response = sut.Request();

     // Assert
     response.Should().NotBeNull();
     response.ResponseCode.Should().Be("1");
     response.Errors.Should().BeEmpty();
}

I have argued with a few colleagues that it's pointless to assert that 'response' is not null if you are going to then assert on some of it's internals. Of course if the only thing that you are interested is checking that the object is not null, nothing more, then it's fine.

My thinking is that each following assert statements are in fact implicit assertions that 'response' is not null. If it is null then it would throw a null reference exception and making the test fail as expected.

The only benefit I see from doing this is a somewhat clearer message on why the test failed. You'd get your test framework specific exception that's thrown by the 'NotBeNull' assertion instead of the generic 'NullReferenceException'. I don't feel this is useful as you are getting the same information anyway.

Am I missing something here  ? Does checking for null have any other benefits in the example provided  ?

Why assert for null on a object before asserting on some of its internals?

Let's consider the following test.

[Fact]
public void MyTest()
{
     // Arrange Code
     var sut = new SystemWeTest();

     // Act Code
     var response = sut.Request();

     // Assert
     response.Should().NotBeNull();
     response.ResponseCode.Should().Be("1");
     response.Errors.Should().BeEmpty();
}

I have argued with a few colleagues that it's pointless to assert that 'response' is not null if you are going to then assert on some of its internals. Of course, if the only thing that you are interested is checking that the object is not null, nothing more, then it's fine.

My thinking is that each following assert statements are in fact implicit assertions that 'response' is not null. If it is null then it would throw a null reference exception and making the test fail as expected.

The only benefit I see from doing this is a somewhat clearer message on why the test failed. You'd get your test framework specific exception that's thrown by the 'NotBeBull' assertion instead of the generic 'nullReferenceException'. I don't feel this is useful as you are getting the same information anyway.

Am I missing something here? Does checking for null have any benefits in the example provided?

Question Protected by gnat
Became Hot Network Question
edited body
Source Link
Glorfindel
  • 3.2k
  • 6
  • 28
  • 34

Let's consider the following test.

[Fact]
public void MyTest()
{
     // Arrange Code
     var sut = new SystemWeTest();

     // Act Code
     var response = sut.Request();

     // Assert
     response.Should().NotBeNull();
     response.ResponseCode.Should().Be("1");
     response.Errors.Should().BeEmpty();
}

I have argued with a few colleagues that it's pointless to assert that 'response' is not null if you are going to then assert on some of it's internals. Of course if the only thing that you are interested is checking that the object is not null, nothing more, then it's fine.

My thinking is that each following assert statements are in fact implicit assertions that 'response' is not null. If it is null then it would throw a null reference exception and making the test fail as expected.

The only benefit I see from doing this is a somewhat clearer message on why the test failed. You'd get your test framework specific exception that's thrown by the 'NotBeBull''NotBeNull' assertion instead of the generic 'nullReferenceException''NullReferenceException'. I don't feel this is usefulluseful as you are getting the same information anyway.

Am I missing something here ? Does checking for null hashave any other benefits in the exempleexample provided ?

Let's consider the following test.

[Fact]
public void MyTest()
{
     // Arrange Code
     var sut = new SystemWeTest();

     // Act Code
     var response = sut.Request();

     // Assert
     response.Should().NotBeNull();
     response.ResponseCode.Should().Be("1");
     response.Errors.Should().BeEmpty();
}

I have argued with a few colleagues that it's pointless to assert that 'response' is not null if you are going to then assert on some of it's internals. Of course if the only thing that you are interested is checking that the object is not null, nothing more, then it's fine.

My thinking is that each following assert statements are in fact implicit assertions that 'response' is not null. If it is null then it would throw a null reference exception and making the test fail as expected.

The only benefit I see from doing this is a somewhat clearer message on why the test failed. You'd get your test framework specific exception that's thrown by the 'NotBeBull' assertion instead of the generic 'nullReferenceException'. I don't feel this is usefull as you are getting the same information anyway.

Am I missing something here ? Does checking for null has any other benefits in the exemple provided ?

Let's consider the following test.

[Fact]
public void MyTest()
{
     // Arrange Code
     var sut = new SystemWeTest();

     // Act Code
     var response = sut.Request();

     // Assert
     response.Should().NotBeNull();
     response.ResponseCode.Should().Be("1");
     response.Errors.Should().BeEmpty();
}

I have argued with a few colleagues that it's pointless to assert that 'response' is not null if you are going to then assert on some of it's internals. Of course if the only thing that you are interested is checking that the object is not null, nothing more, then it's fine.

My thinking is that each following assert statements are in fact implicit assertions that 'response' is not null. If it is null then it would throw a null reference exception and making the test fail as expected.

The only benefit I see from doing this is a somewhat clearer message on why the test failed. You'd get your test framework specific exception that's thrown by the 'NotBeNull' assertion instead of the generic 'NullReferenceException'. I don't feel this is useful as you are getting the same information anyway.

Am I missing something here ? Does checking for null have any other benefits in the example provided ?

added 1 character in body
Source Link
BAmadeusJ
  • 326
  • 2
  • 7

Let's consider the following test.

[Fact]
public void MyTest()
{
     // Arrange Code
     var sut = new SystemWeTest();

     // Act Code
     var response = sut.Request();

     // Assert
     response.Should().NotBeNull();
     response.ResponseCode.Should().Be("1");
     response.Errors.Should().BeEmpty();
}

I have argued with a few colleagues that it's pointless to assert that 'response' is not null if you are going to then assert on some of it's internals. Of course if the only thing that you are interested is checking that the object is not null, nothing more, then it's fine.

My thinking is that each following assert statements are in fact implicit assertions that 'reponse''response' is not null. If it is null then it would throw a null reference exception and making the test fail as expected.

The only benefit I see from doing this is a somewhat clearer message on why the test failed. You'd get your test framework specific exception that's thrown by the 'NotBeBull' assertion instead of the generic 'nullReferenceException'. I don't feel this is usefull as you are getting the same information anyway.

Am I missing something here ? Does checking for null has any other benefits in the exemple provided ?

Let's consider the following test.

[Fact]
public void MyTest()
{
     // Arrange Code
     var sut = new SystemWeTest();

     // Act Code
     var response = sut.Request();

     // Assert
     response.Should().NotBeNull();
     response.ResponseCode.Should().Be("1");
     response.Errors.Should().BeEmpty();
}

I have argued with a few colleagues that it's pointless to assert that 'response' is not null if you are going to then assert on some of it's internals. Of course if the only thing that you are interested is checking that the object is not null, nothing more, then it's fine.

My thinking is that each following assert statements are in fact implicit assertions that 'reponse' is not null. If it is null then it would throw a null reference exception and making the test fail as expected.

The only benefit I see from doing this is a somewhat clearer message on why the test failed. You'd get your test framework specific exception that's thrown by the 'NotBeBull' assertion instead of the generic 'nullReferenceException'. I don't feel this is usefull as you are getting the same information anyway.

Am I missing something here ? Does checking for null has any other benefits in the exemple provided ?

Let's consider the following test.

[Fact]
public void MyTest()
{
     // Arrange Code
     var sut = new SystemWeTest();

     // Act Code
     var response = sut.Request();

     // Assert
     response.Should().NotBeNull();
     response.ResponseCode.Should().Be("1");
     response.Errors.Should().BeEmpty();
}

I have argued with a few colleagues that it's pointless to assert that 'response' is not null if you are going to then assert on some of it's internals. Of course if the only thing that you are interested is checking that the object is not null, nothing more, then it's fine.

My thinking is that each following assert statements are in fact implicit assertions that 'response' is not null. If it is null then it would throw a null reference exception and making the test fail as expected.

The only benefit I see from doing this is a somewhat clearer message on why the test failed. You'd get your test framework specific exception that's thrown by the 'NotBeBull' assertion instead of the generic 'nullReferenceException'. I don't feel this is usefull as you are getting the same information anyway.

Am I missing something here ? Does checking for null has any other benefits in the exemple provided ?

added 6 characters in body
Source Link
BAmadeusJ
  • 326
  • 2
  • 7
Loading
Source Link
BAmadeusJ
  • 326
  • 2
  • 7
Loading