Search code examples
c#.netmoq

c# Moq SetupSequence 2nd call returning null


I am writing a unit test for a class where I am passing three calls to the same method, but with some different properties.

The calls in the class look like this

if (request.ClubLogo != null)
{
    var clubLogoUrl = await _fileStorage.StoreFileAsync(request.ClubLogo, teamInfo.ClubTeam.Club.NationalGoverningBody.Country.Iso.Replace(" ", string.Empty), teamInfo.ClubTeam.Club.ClubName.Replace(" ", string.Empty), FileType.ClubLogo, "clublogo", cancellationToken);

    _logger.LogInformation(clubLogoUrl);
    teamInfo.ClubTeam.Club.SetClubLogoUri(new Uri(clubLogoUrl, UriKind.Absolute));
}

if (request.TeamLogo != null)
{
    var teamLogoUri = await _fileStorage.StoreFileAsync(request.TeamLogo, teamInfo.ClubTeam.Club.NationalGoverningBody.Country.Iso.Replace(" ", string.Empty), teamInfo.ClubTeam.Club.ClubName.Replace(" ", string.Empty), teamInfo.ClubTeam.TeamName.Replace(" ", string.Empty),
        FileType.TeamLogo, request.EventReference.ToLower(), cancellationToken);

    _logger.LogInformation(teamLogoUri);
    teamInfo.ClubTeam.SetTeamLogoUri(new Uri(teamLogoUri, UriKind.Absolute));
}

var teamPhotoUri = await _fileStorage.StoreFileAsync(request.TeamPhoto, teamInfo.ClubTeam.Club.NationalGoverningBody.Country.Iso.Replace(" ", string.Empty), teamInfo.ClubTeam.Club.ClubName.Replace(" ", string.Empty), teamInfo.ClubTeam.TeamName.Replace(" ", string.Empty),
          FileType.TeamPhoto, request.EventReference.ToLower(), cancellationToken);

_logger.LogInformation(teamPhotoUri);
teamInfo.ClubTeam.SetTeamPhotoUri(new Uri(teamPhotoUri, UriKind.Absolute));

However, when I make the call to store the team logo, teamLogoUri is null, but clubLogoUri has the correct value.

In the test I am setting this up like this

_fileStorage.Setup(x => x.StoreFileAsync(It.IsAny<IFormFile>(), It.IsAny<string>(),
        It.IsAny<string>(), FileType.ClubLogo, It.IsAny<string>(), CancellationToken.None))
    .ReturnsAsync("http://www.someurl.com/1");

_fileStorage.SetupSequence(x => x.StoreFileAsync(It.IsAny<IFormFile>(), It.IsAny<string>(),
    It.IsAny<string>(), It.IsAny<string>(), FileType.ClubLogo, It.IsAny<string>(), CancellationToken.None))
    .ReturnsAsync("http://www.someurl.com/1")
    .ReturnsAsync("http://www.someurl.com/2");

The reason there is two setups is because there is an overloaded method as per this interface

public interface IFileStorage
{
    Task<string> StoreFileAsync(IFormFile file, string country, string club, string team, FileType fileType, string containerName, CancellationToken cancellationToken);

    Task<string> StoreFileAsync(IFormFile file, string country, string club, FileType fileType, string containerName, CancellationToken cancellationToken);
}

I cant see what I have done wrong here as I have setup a sequence many times in the past.


Solution

  • As far as I can see you are setting up sequence with FileType.ClubLogo while the following calls are FileType.TeamLogo and FileType.TeamPhoto, change it to It.IsAny<FileType>(), or setup separate calls.