Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NUnit or Fluent Assertions test for reference equality?

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?

like image 955
Cristian Diaconescu Avatar asked Jul 19 '13 08:07

Cristian Diaconescu


People also ask

Should I use fluent Assertions?

Fluent Assertions are important in unit testing because they allow the code to be easily read and followed. This makes it easier to determine whether or not an assertion is being met. As a result, everyone can easier read and understand unit tests, making it easier to locate the failing assert.

How do I compare two objects in NUnit?

Just install ExpectedObjects from Nuget, you can easily compare two objects's property value, each object value of collection, two composed object's value and partial compare property value by anonymous type. Reference: ExpectedObjects github. Introduction of ExpectedObjects.

What is fluent Assertions C#?

Fluent Assertions is a set of . NET extension methods that allow you to more naturally specify the expected outcome of a TDD or BDD-style unit test.

What is NUnit assertion?

NUnit Assert class is used to determine whether a particular test method gives expected result or not. In a test method, we write code the check the business object behavior. That business object returns a result. In Assert method we match the actual result with our expected result.


1 Answers

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.

like image 94
Ufuk Hacıoğulları Avatar answered Oct 16 '22 20:10

Ufuk Hacıoğulları