2

I am having a test method that asserts if the CreateClient method of the client account repository has been called. Please See the test bellow.

[TestMethod]
    public void CreateNewBasicClientAccount_NewBasicClient_CreatesNewClientBasicClient()
    {
        // Arrange

        var clientAccountToCreate = new ClientAccount
        {                
            Name = "Name",                
        };

        var clientAccountToCreateDto = AutoMapper.Mapper.Map<ClientAccount, ClientAccountDto>(clientAccountToCreate);


        var clientAccountRepository = A.Fake<IClientAccountRepository>();
        var clientAccountManager = new ClientAccountManager(clientAccountRepository);

        // Act

        clientAccountManager.CreateClient(clientAccountToCreate);

        // Assert

        A.CallTo(
            () => clientAccountRepository.CreateClient(A<ClientAccountDto>.That.IsNotNull<ClientAccountDto>()))
            .MustHaveHappened();

        A.CallTo(
            () => clientAccountRepository.CreateClient(A<ClientAccountDto>.Ignored))
            .MustHaveHappened();

        A.CallTo(
            () => clientAccountRepository.CreateClient(clientAccountToCreateDto))
            .MustHaveHappened();
    }

The Act portion of my ClientAccountManager class in the test is calling the CreateClient method of the repository

public void CreateClient(ClientAccount client)
    {
        var clientDto = AutoMapper.Mapper.Map<ClientAccount, ClientAccountDto>(client);
        _clientAccountRepository.CreateClient(clientDto);
    }

The first two asserts in the test pass, but the more specific 3rd assert fails with result message

InterfaceNameSpace.IClientAccountRepository.CreateClient(clientDto: DtoNameSpace.ClientAccountDto) Expected to find it at least once but found it #0 times among the calls:

Both the ClientAccount and ClientAccountDto classes have the exact same properties. Input in getting the failed assert to pass would be appreciated as the code is wired up for it to pass but it fails.

Ntu
  • 41
  • 4

1 Answers1

2

This is because the actual ClientAccountDto that is passed to the method is not the same instance as the one you create in your test, so they're not considered equal.

There are several options to solve this:

  • override the Equals method in ClientAccountDto (not ideal, since you normally wouldn't need this for a DTO)
  • inject an IMapper into ClientAccountManager, instead of using the static Mapper class, and configure the IMapper to return a specific instance of ClientAccountDto
  • test specific properties of the ClientAccountDto, like this:

    A.CallTo(
        () => clientAccountRepository.CreateClient(A<ClientAccountDto>.That.Matches(x => x.Name == "Name")))
        .MustHaveHappened();
    

Unrelated note: you don't need to specify the type again in A<ClientAccountDto>.That.IsNotNull<ClientAccountDto>(), you can just write A<ClientAccountDto>.That.IsNotNull().

Thomas Levesque
  • 286,951
  • 70
  • 623
  • 758