Edit on GitHub

xUnit2005 Warning

Do not use identity check on value type

Cause

A violation of this rule occurs when two value type objects are compared using Assert.Same or Assert.NotSame.

Reason for rule

Assert.Same and Assert.NotSame both use Object.ReferenceEquals to compare objects. This always fails for value types since the values will be boxed before they are passed to the method, creating two different references.

How to fix violations

To fix a violation of this rule, use Assert.Equal or Assert.NotEqual instead.

Examples

Violates

[Fact]
public void ExampleMethod()
{
    DateTime result = GetDateResult();

    Assert.Same(new DateTime(2017, 01, 01), result);
}

Does not violate

[Fact]
public void ExampleMethod()
{
    DateTime result = GetDateResult();

    Assert.Equal(new DateTime(2017, 01, 01), result);
}

How to suppress violations

#pragma warning disable xUnit2005 // Do not use identity check on value type
#pragma warning restore xUnit2005 // Do not use identity check on value type
Copyright © 2017 .NET Foundation. Contributions welcomed at https://github.com/xunit/xunit.analyzers.