英文:
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(" ", 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.
答案1
得分: 1
根据我看到的内容,您正在使用FileType.ClubLogo
来设置序列,而以下调用是FileType.TeamLogo
和FileType.TeamPhoto
,请将其更改为It.IsAny<FileType>()
,或者设置单独的调用。
英文:
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.
答案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<T>
with an expression to match both:
_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");
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论