I went for an interview and was given a NUnit test, which failed and asked why it failed, and I couldnt find a reason. During debug the values were all the same except the ID, which was 1 and 0 respectively.
I am not strong in testing, but want to learn my mistake, what do I look out for and why did it fail ?
Please help me understand what is wrong here ? thank you
ERROR MESSAGE
NUnit.Framework.AssertionException HResult=0x80131500 Message=Expected _context.Account {Root.CLIENTBooking.Data.Models.Account { Created = <2021-08-31 07:58:18.8245439> DateOfBirth = <2021-10-26 02:51:07.9281209> Email = "Email44cc0cbb-a6c3-43a5-974d-e1003b958854" FirstName = "FirstNameda3ad7fd-f2d4-4720-92df-46f82b6406cd" Gender = 0 Id = 1L LastName = "LastName765872d6-7fe9-475a-b217-87c45ca7e1a6" Orders = {empty} }} to contain equivalent of Root.CLIENTBooking.Data.Models.Account { Created = <2021-08-31 07:58:04.4695485> DateOfBirth = <2021-10-26 02:51:07.9281209> Email = "Email44cc0cbb-a6c3-43a5-974d-e1003b958854" FirstName = "FirstNameda3ad7fd-f2d4-4720-92df-46f82b6406cd" Gender = 0 Id = 0L LastName = "LastName765872d6-7fe9-475a-b217-87c45ca7e1a6" Orders = {empty} }. With configuration: - Use declared types and members - Compare enums by value - Exclude member root.Id - Match member by name (or throw) - Without automatic conversion. - Be strict about the order of items in byte arrays Source=FluentAssertions StackTrace: at FluentAssertions.Execution.LateBoundTestFramework.Throw(String message) at FluentAssertions.Execution.TestFrameworkProvider.Throw(String message) at FluentAssertions.Execution.CollectingAssertionStrategy.ThrowIfAny(IDictionary`2 context) at FluentAssertions.Collections.CollectionAssertions`2.ContainEquivalentOf[TExpectation](TExpectation expectation, Func`2 config, String because, Object[] becauseArgs) at Root.CLIENTBooking.Service.Tests.AccountServices.AccountServiceTests.AddAccount_AddsAccountToContextWithGeneratedId() in C:\Users\XXXXXoz\OneDrive\Documents\XXXXX TEst\Root.CLIENTBooking.Service.Tests\AccountServices\AccountServiceTests.cs:line 110
Test
[Test] public void AddAccount_AddsAccountToContextWithGeneratedId() { //arrange var request = _fixture.Create<AddAccountRequest>(); var expected = new Account { FirstName = request.FirstName, LastName = request.LastName, DateOfBirth = request.DateOfBirth, Gender = (int)request.Gender, Email = request.Email, Orders = new List<Order>(), Created = DateTime.UtcNow }; //act _AccountService.AddAccount(request); //assert _context.Account.Should().ContainEquivalentOf(expected, options => options.Excluding(Account => Account.Id)); }
ACCOUNT CLASS
public class Account { public long Id { get; set; } public string FirstName { get; set; } public string LastName { get; set; } public DateTime DateOfBirth { get; set; } public int Gender { get; set; } public string Email { get; set; } public virtual ICollection<Order> Orders { get; set; } public DateTime Created { get; set; } }
ADD ACCOUNT CLASS
public class AddAccountRequest { public string FirstName { get; set; } public string LastName { get; set; } public DateTime DateOfBirth { get; set; } public Gender Gender { get; set; } public string Email { get; set; } }
FINALLY
ACCOUNT SERVICE
> public void AddAccount(AddAccountRequest request)
{ var validationResult = _validator.ValidateRequest(request); if (!validationResult.PassedValidation) { throw new ArgumentException(validationResult.Errors.First()); } _context.Account.Add(new Account { FirstName = request.FirstName, LastName = request.LastName, DateOfBirth = request.DateOfBirth, Gender = (int)request.Gender, Email = request.Email, Orders = new List<Order>(), Created = DateTime.UtcNow }); _context.SaveChanges(); }