0

I'm writing an application that allows users to "log on" by registering with the same email. (Don't worry, it's not an application that will run on the internet, and also doesn't hold critical data. It's a temporary registration and will only run in local networks.) The MVC method is supposed to return the API key and some other data upon successfully logging in. I created a unit test to check this behavior:

[Fact]
public void Register_ExistingEmail()
{
    //Arrange
    DbContext context = new DbContext(InMemoryDbContextOptionsExtensions.UseInMemoryDatabase(new DbContextOptionsBuilder<DbContext>(), "VoteInMemDb").Options);
    context.Users.Add(new User { ApiToken = "TestToken123", Email = "commonname@commondomain.com", JsonData = "{}" });
    context.SaveChanges();
    //logger, hub and settings are being mocked using Moq and are set up in the constructor
    RegistrationController regCon = new RegistrationController(logger, context, hub, settings);

    //Act
    IActionResult result = regCon.Register("commonname@commondomain.com", "{}", false);

    //Assert
    try
    {
        //attempt to cast to expected result type
        JsonResult jsonResult = (JsonResult)result;

        Assert.Equal(new
        {
            apiToken = "TestToken123",
            currentScreen = ClientScreen.welcome.ToString(),
            cookieDuration = settings.Value.ApiTokenDuration
        }, jsonResult.Value);
    }
    catch (EqualException e)
    {
        throw e;
    }
    catch
    {
        Assert.True(false);
    }
}

Now, even though it successfully returns the object as expected, it still throws an EqualException:

Testname:   project.unit_tests.UnitTest1.Register_ExistingEmail
FullName für Test:  project.unit_tests.UnitTest1.Register_ExistingEmail
Testquelle: D:\Repositories\project.unit-tests\UnitTest1.cs : Zeile 77
Testergebnis:   Fehler
Testdauer:  0:00:01.009

Ergebnis StackTrace:    at project.unit_tests.UnitTest1.Register_ExistingEmail() in D:\Repositories\project.unit-tests\UnitTest1.cs:line 104
Ergebnis Meldung:   
Assert.Equal() Failure
Expected: { apiToken = TestToken123, currentScreen = welcome, cookieDuration = 1440 } (<>f__AnonymousType0`3[[System.String, System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e],[System.String, System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e],[System.Int32, System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]])
Actual:   { apiToken = TestToken123, currentScreen = welcome, cookieDuration = 1440 } (<>f__AnonymousType1`3[[System.String, System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e],[System.String, System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e],[System.Int32, System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]])

I see that it is 99% the same object, besides the <>f__AnonymousTypeX`3 part. Is this because I'm using anonymous Types for generating JsonResults? Do I have to create models for every anonymous Type I'm returning to unit test the method?

Thanks in advance

lenny
  • 734
  • 2
  • 15
  • 43
  • 2
    `Assert.Equal` checks reference equality if the types are reference types. It doesn't matter if the properties all have the same value. If you don't want to make a class for which you can override `Equals` then you will have to determine equality by comparing each property. – Crowcoder Apr 09 '19 at 10:39
  • 1
    Possible duplicate of [XUnit Assertion for checking equality of objects](https://stackoverflow.com/questions/11135337/xunit-assertion-for-checking-equality-of-objects) – Kami Apr 09 '19 at 10:40
  • @Crowcoder Thanks, I'll check that out! – lenny Apr 09 '19 at 10:41

0 Answers0