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

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

c# Moq SetupSequence 2nd call returning null

问题

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

类中的调用如下:

  1. if (request.ClubLogo != null)
  2. {
  3. 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);
  4. _logger.LogInformation(clubLogoUrl);
  5. teamInfo.ClubTeam.Club.SetClubLogoUri(new Uri(clubLogoUrl, UriKind.Absolute));
  6. }
  7. if (request.TeamLogo != null)
  8. {
  9. 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),
  10. FileType.TeamLogo, request.EventReference.ToLower(), cancellationToken);
  11. _logger.LogInformation(teamLogoUri);
  12. teamInfo.ClubTeam.SetTeamLogoUri(new Uri(teamLogoUri, UriKind.Absolute));
  13. }
  14. 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),
  15. FileType.TeamPhoto, request.EventReference.ToLower(), cancellationToken);
  16. _logger.LogInformation(teamPhotoUri);
  17. teamInfo.ClubTeam.SetTeamPhotoUri(new Uri(teamPhotoUri, UriKind.Absolute));

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

在测试中,我设置如下:

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

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

  1. public interface IFileStorage
  2. {
  3. Task<string> StoreFileAsync(IFormFile file, string country, string club, string team, FileType fileType, string containerName, CancellationToken cancellationToken);
  4. Task<string> StoreFileAsync(IFormFile file, string country, string club, FileType fileType, string containerName, CancellationToken cancellationToken);
  5. }

我无法看出您在这里做错了什么,因为我过去多次设置过序列。

英文:

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

  1. if (request.ClubLogo != null)
  2. {
  3. 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);
  4. _logger.LogInformation(clubLogoUrl);
  5. teamInfo.ClubTeam.Club.SetClubLogoUri(new Uri(clubLogoUrl, UriKind.Absolute));
  6. }
  7. if (request.TeamLogo != null)
  8. {
  9. 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),
  10. FileType.TeamLogo, request.EventReference.ToLower(), cancellationToken);
  11. _logger.LogInformation(teamLogoUri);
  12. teamInfo.ClubTeam.SetTeamLogoUri(new Uri(teamLogoUri, UriKind.Absolute));
  13. }
  14. 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),
  15. FileType.TeamPhoto, request.EventReference.ToLower(), cancellationToken);
  16. _logger.LogInformation(teamPhotoUri);
  17. 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

  1. _fileStorage.Setup(x =&gt; x.StoreFileAsync(It.IsAny&lt;IFormFile&gt;(), It.IsAny&lt;string&gt;(),
  2. It.IsAny&lt;string&gt;(), FileType.ClubLogo, It.IsAny&lt;string&gt;(), CancellationToken.None))
  3. .ReturnsAsync(&quot;http://www.someurl.com/1&quot;);
  4. _fileStorage.SetupSequence(x =&gt; x.StoreFileAsync(It.IsAny&lt;IFormFile&gt;(), It.IsAny&lt;string&gt;(),
  5. It.IsAny&lt;string&gt;(), It.IsAny&lt;string&gt;(), FileType.ClubLogo, It.IsAny&lt;string&gt;(), CancellationToken.None))
  6. .ReturnsAsync(&quot;http://www.someurl.com/1&quot;)
  7. .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

  1. public interface IFileStorage
  2. {
  3. Task&lt;string&gt; StoreFileAsync(IFormFile file, string country, string club, string team, FileType fileType, string containerName, CancellationToken cancellationToken);
  4. Task&lt;string&gt; StoreFileAsync(IFormFile file, string country, string club, FileType fileType, string containerName, CancellationToken cancellationToken);
  5. }

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> 并使用表达式匹配两者:

  1. _fileStorage.SetupSequence(x =>
  2. x.StoreFileAsync(
  3. It.IsAny<IFormFile>(),
  4. It.IsAny<string>(),
  5. It.IsAny<string>(),
  6. It.IsAny<string>(),
  7. It.Is<FileType>(f => f == FileType.ClubLogo || f == FileType.TeamLogo),
  8. It.IsAny<string>(),
  9. CancellationToken.None))
  10. .ReturnsAsync("http://www.someurl.com/1")
  11. .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:

  1. _fileStorage.SetupSequence(x =&gt;
  2. x.StoreFileAsync(
  3. It.IsAny&lt;IFormFile&gt;(),
  4. It.IsAny&lt;string&gt;(),
  5. It.IsAny&lt;string&gt;(),
  6. It.IsAny&lt;string&gt;(),
  7. It.Is&lt;FileType&gt;(f =&gt; f == FileType.ClubLogo || f == FileType.TeamLogo),
  8. It.IsAny&lt;string&gt;(),
  9. CancellationToken.None))
  10. .ReturnsAsync(&quot;http://www.someurl.com/1&quot;)
  11. .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:

确定