Search code examples
c#unit-testingassertxunit.net

C# XUnit Assert.Equal failing when comparing var and int


I have this snippet of test code:

public void Test () {
    var expected = 10;
    int actual = 10;
    Assert.Equal(expected, actual);    //Failing
}

I understand that C#s var is implicitly typed, so it is recognized as an int during compile time; they should both be Int32. Would it not be comparing the values?

I also read that the Equal method compares objects; would this affect the way it compares the two primitive types?


Solution

  • Yes, it should be comparing the values, and they should both be ints.

    The impl of the various Equal overloads should be in here.

    If you clone the Asserts Lib (or the source repo for which I provided a submodule link) locally, you can step through the impl (or turn off Just My Code in Tools|Options - pretty sure xUnit's symbols are published somewhere).