无法在测试 C# 中 return 元组
Not able to return Tuple in Test C#
我需要 return 一个元组响应。这是我的代码:
public Task<(List<AzureADUser> users, Dictionary<string, int> nonUserGraphObjects, string nextPageUrl, IGroupTransitiveMembersCollectionWithReferencesPage usersFromGroup)> GetNextUsersPageAsync(string nextPageUrl, IGroupTransitiveMembersCollectionWithReferencesPage usersFromGroup)
{
var users = new List<AzureADUser>();
var nonUserGraphObjects = new Dictionary<string, int>();
return (users, nonUserGraphObjects, "", null);
}
在测试这个时,我得到一个错误:
Tuple with 4 elements cannot be converted to type 'Task<(List<AzureADUser> users, Dictionary<string, int> nonUserGraphObjects, string nextPageUrl, IGroupTransitiveMembersCollectionWithReferencesPage usersFromGroup)>'
我错过了什么?
问题不在于元组——而是任务。您的方法声明它 return 是一项任务。您正在尝试直接 return 该值。如果您的方法是异步的,那会起作用,但事实并非如此。
也许你的意思是:
return Task.FromResult((users, nonUserGraphObjects, "", (IGroupTransitiveMembersCollectionWithReferencesPage) null));
顺便说一句,如果可以的话,我会强烈考虑为此 return 类型引入抽象 - 当方法的 return 类型声明(即使没有 Task<>
部分) 是 159 个字符,这确实不利于可读性。
我需要 return 一个元组响应。这是我的代码:
public Task<(List<AzureADUser> users, Dictionary<string, int> nonUserGraphObjects, string nextPageUrl, IGroupTransitiveMembersCollectionWithReferencesPage usersFromGroup)> GetNextUsersPageAsync(string nextPageUrl, IGroupTransitiveMembersCollectionWithReferencesPage usersFromGroup)
{
var users = new List<AzureADUser>();
var nonUserGraphObjects = new Dictionary<string, int>();
return (users, nonUserGraphObjects, "", null);
}
在测试这个时,我得到一个错误:
Tuple with 4 elements cannot be converted to type 'Task<(List<AzureADUser> users, Dictionary<string, int> nonUserGraphObjects, string nextPageUrl, IGroupTransitiveMembersCollectionWithReferencesPage usersFromGroup)>'
我错过了什么?
问题不在于元组——而是任务。您的方法声明它 return 是一项任务。您正在尝试直接 return 该值。如果您的方法是异步的,那会起作用,但事实并非如此。
也许你的意思是:
return Task.FromResult((users, nonUserGraphObjects, "", (IGroupTransitiveMembersCollectionWithReferencesPage) null));
顺便说一句,如果可以的话,我会强烈考虑为此 return 类型引入抽象 - 当方法的 return 类型声明(即使没有 Task<>
部分) 是 159 个字符,这确实不利于可读性。