I do not want to disable the warnings completely, just when it's in an Assert 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'll get the possible null reference warning on the second line on someObject.SomeProperty
. Is it possible to disable the warning when it is within a certain call, like Assert.AreEqual
?
Since this is an issue with a lot of unit tests, I don't want to litter the tests with the ReSharper disable code.
Right now the only option I can think of is to change every Assert.IsNotNull
call to be
var someObject = GetObject();
if(someObject == null)
{
Assert.Fail("someObject is null");
return;
}
Although this kind of seems to defeat the purpose of having Assert.IsNotNull
in the first place. Just wondering if there is a better way.
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 like this, 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);
I don't know the specific library you use, but I'd try something like
Assert.IsTrue(someObject != null);
or, for the sake of completeness,
Assert.IsNotNull(someObject, "someObject must not be null");
Assert.IsNotNull(someObject.SomeProperty, "SomeProperty must not be null either");
Assert.SomethingElse(...);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With