Question

I'm using NUnit 2.6.2 + Fluent Assertions 2.0.1.

I want to assert that two references do NOT point to the same object instance. I can't find a clean way to express that.

NUnit has Assert.ReferenceEquals(ref1, ref2) - but I can't find the negated assertion.

In Fluent Assertions I can't find anything to directly support this scenario.

The only way I could do it was like this:

NUnit: Assert.False(object.ReferenceEquals(ref1, ref2));

Fluent: object.ReferenceEquals(ref1, ref2).Should().BeFalse();

Both of these seem less than ideal in terms of minimal noise. Is there a better way?

Was it helpful?

Solution

You can use NotBeSameAs() method:

ref1.Should().NotBeSameAs(ref2);

Its documentation says:

Asserts that an object reference refers to a different object than another object reference refers to.

OTHER TIPS

You can use Is.Not.SameAs() with Nunit 3.x here

var x = new object();
Assert.That(x, Is.SameAs(x)); // success
Assert.That(x, Is.Not.SameAs(x)); // fail

var y = new object();
Assert.That(x, Is.SameAs(y)); // fail
Assert.That(x, Is.Not.SameAs(y)); // success
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top