Any way to ignore possible Null Reference exception warnings when using Assert statements?

I do not want to completely disable warnings only when they are contained in the statement.

So for example, if I have the following two lines

var someObject = GetObject(); Assert.IsNotNull(someObject, "someObject should not be null"); Assert.AreEqual(expectedValue, someObject.SomeProperty); 

I will get a possible null reference warning in the second line of someObject.SomeProperty . Is it possible to disable a warning when it is within a specific call, for example Assert.AreEqual ?

Since this is a problem with a lot of unit tests, I don’t want to hush up the tests with the ReSharper disconnect code.

Currently, the only option I can think of is to change each call to Assert.IsNotNull

 var someObject = GetObject(); if(someObject == null) { Assert.Fail("someObject is null"); return; } 

Although this view seems to be aimed at having Assert.IsNotNull in the first place. Just wondering if there is a better way.

+6
source share
6 answers

I do not know the specific library that you are using, but I would try something like

 Assert.IsTrue(someObject != null); 

or, for completeness,

 Assert.IsNotNull(someObject, "someObject must not be null"); Assert.IsNotNull(someObject.SomeProperty, "SomeProperty must not be null either"); Assert.SomethingElse(...); 
+1
source

Add this to your project:

 public static class AssertionsExtensions { [NotNull] public static TSubject ShouldNotBeNull<TSubject>([CanBeNull] this TSubject source, [CanBeNull] string because = "", [CanBeNull] [ItemCanBeNull] params object[] reasonArgs) { source.Should().NotBeNull(because, reasonArgs); // ReSharper disable once AssignNullToNotNullAttribute return source; } } 

Then use it, for example:

  // Assert succeeded.Should().Be(true, "<USB loopback cable must be connected and COM port must be correct.>"); DeviceStatus deviceStatusNotNull = testRunner.Result.ShouldNotBeNull(); deviceStatusNotNull.DeviceAddress.Should().Be(deviceAddress); deviceStatusNotNull.IsInNetwork.Should().Be(true); 
+1
source

If I'm not mistaken, your problem is that resharper gives warnings when null is not checked for the foreach object. You can modify resharper rules to not give null warnings in test classes. Here is a link about changing the ReSharper naming style for testing methods.

0
source

Use NUnit instead of MBUnit. The NUnit implementation of Assert.IsNotNull () is perceived as guaranteeing non-null, whereas MBUnit is not.

0
source

You can use external R # annotations to suppress warnings (see http://grahamrhay.wordpress.com/2011/01/09/external-annotations/ ).

What version of MbUnit / Gallio are you using?

0
source

You can use ContractAnnotations to indicate that execution stops if the parameter is null. See jetbrains contract annotations . Sample Class:

  public static class FluentExtensions { //see: https://www.jetbrains.com/help/resharper/2017.3/Contract_Annotations.html [ContractAnnotation("null => stop")] public static void ShouldNotBeNull(this object objToTest) { objToTest.Should().NotBeNull(); } } 

Using:

 doc.ShouldNotBeNull(); doc.Content.ShouldNotBeNull(); 
0
source

Source: https://habr.com/ru/post/911510/


All Articles