'Provide equality for NUnits results

We have the Result-argument for all kinds of parameterized tests on NUnit. This works well for simple types where equality is built into .NET. However I wonder if there´s a way to verify a test-method returns a complex object:

[TestCase("MyValue", Result = new MyType(...) /* doesn't work as we can only use compile-time constants for attributes */]
public MyType Check(string value)
{
    var target = ...
    return target.DoSomething(value);
}

Assuming target.DoSomething returns an instance of MyType the test should ensure that the returned instance is equal to what I provided within my TestCase. As MyType does neither implement IEquatable nor override Equals equality is determined using ReferenceEquals which of course is wrong. Moreover as mentioned in my comment we can't create an instance of MyType within the attribute as it's no compile-time constant.

So how can we provide a complex instance to our test and check if the outcome is equal to that one?



Solution 1:[1]

We use to use Fody Equals to compare custom classes Equals, there is a nuget packet to install it. In order to use it just to put Equals as header decorator and you should able to compare it properly

Solution 2:[2]

The ExpectedResult attribute is just a bit of syntactic niceness and isn't required in any way. You can always just add an extra argument to the method, giving the expected value, and test it in any way you like within your test.

We have no plan to enhance the internal equality comparison that is used by NUnit to check your ExpectedResult.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 ChaosPattern
Solution 2 Charlie