c# Moq SetupSequence 第二次调用返回 null

huangapple go评论63阅读模式
英文:

c# Moq SetupSequence 2nd call returning null

问题

我正在为一个类编写单元测试,我在其中传递了对同一方法的三个调用,但使用了一些不同的属性。

类中的调用如下:

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));

然而,当我调用存储团队徽标的方法时,teamLogoUri 为 null,但 clubLogoUri 具有正确的值。

在测试中,我设置如下:

_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");

之所以有两个设置是因为根据该接口存在一个重载方法:

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 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(&quot; &quot;, string.Empty), teamInfo.ClubTeam.Club.ClubName.Replace(&quot; &quot;, string.Empty), FileType.ClubLogo, &quot;clublogo&quot;, 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(&quot; &quot;, string.Empty), teamInfo.ClubTeam.Club.ClubName.Replace(&quot; &quot;, string.Empty), teamInfo.ClubTeam.TeamName.Replace(&quot; &quot;, 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(&quot; &quot;, string.Empty), teamInfo.ClubTeam.Club.ClubName.Replace(&quot; &quot;, string.Empty), teamInfo.ClubTeam.TeamName.Replace(&quot; &quot;, 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 =&gt; x.StoreFileAsync(It.IsAny&lt;IFormFile&gt;(), It.IsAny&lt;string&gt;(),
		It.IsAny&lt;string&gt;(), FileType.ClubLogo, It.IsAny&lt;string&gt;(), CancellationToken.None))
	.ReturnsAsync(&quot;http://www.someurl.com/1&quot;);

_fileStorage.SetupSequence(x =&gt; x.StoreFileAsync(It.IsAny&lt;IFormFile&gt;(), It.IsAny&lt;string&gt;(),
	It.IsAny&lt;string&gt;(), It.IsAny&lt;string&gt;(), FileType.ClubLogo, It.IsAny&lt;string&gt;(), CancellationToken.None))
	.ReturnsAsync(&quot;http://www.someurl.com/1&quot;)
	.ReturnsAsync(&quot;http://www.someurl.com/2&quot;);

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

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

    Task&lt;string&gt; 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.

答案1

得分: 1

根据我看到的内容,您正在使用FileType.ClubLogo来设置序列,而以下调用是FileType.TeamLogoFileType.TeamPhoto,请将其更改为It.IsAny&lt;FileType&gt;(),或者设置单独的调用。

英文:

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&lt;FileType&gt;(), or setup separate calls.

答案2

得分: 1

你正在将 FileType 参数与 FileType.ClubLogo 进行匹配,而在第二次调用中,你传递了 FileType.TeamLogo。请使用 It.Is<T> 并使用表达式匹配两者:

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

You are matching the FileType argument against FileType.ClubLogo and in the second call you are passing FileType.TeamLogo. Use It.Is&lt;T&gt; with an expression to match both:

_fileStorage.SetupSequence(x =&gt;
    x.StoreFileAsync(
        It.IsAny&lt;IFormFile&gt;(),
        It.IsAny&lt;string&gt;(),
        It.IsAny&lt;string&gt;(),
        It.IsAny&lt;string&gt;(),
        It.Is&lt;FileType&gt;(f =&gt; f == FileType.ClubLogo || f == FileType.TeamLogo),
        It.IsAny&lt;string&gt;(),
        CancellationToken.None))
    .ReturnsAsync(&quot;http://www.someurl.com/1&quot;)
    .ReturnsAsync(&quot;http://www.someurl.com/2&quot;);

huangapple
  • 本文由 发表于 2023年7月14日 04:53:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/76683171.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定