gpt4 book ai didi

tdd - 模拟成员(member)

转载 作者:行者123 更新时间:2023-12-04 03:47:21 26 4
gpt4 key购买 nike

我正在编写一个自定义配置文件提供程序,但我仍然打算使用默认的 AspNetSqlMembershipProvider 作为我的成员资格提供程序。我的 Profile 提供程序中的 GetAllProfiles() 方法如下所示:

1    public override ProfileInfoCollection GetAllProfiles(ProfileAuthenticationOption authenticationOption, int pageIndex, int pageSize, out int totalRecords)
2 {
3 // Get the profiles
4 IQueryable<Profile> profiles = _profileRepository.GetAllProfiles();
5
6 // Convert to a ProfileInfoCollection
7 ProfileInfoCollection profileInfoCollection = new ProfileInfoCollection();
8 foreach (Profile profile in profiles)
9 {
10 MembershipUser user = Membership.GetUser(profile.UserId);
11
12 string username = user.UserName;
13 bool isAnonymous = false;
14 DateTime lastActivity = user.LastActivityDate;
15 DateTime lastUpdated = profile.LastUpdated;
16
17 ProfileInfo profileInfo = new ProfileInfo(username, isAnonymous, lastActivity, lastUpdated, 1);
18
19 profileInfoCollection.Add(profileInfo);
20 }
21
22 // Set the total number of records.
23 totalRecords = profiles.ToList().Count;
24
25 return profileInfoCollection;
26 }

如何模拟 Membership.GetUser() 调用,以便我可以为此方法编写测试?有什么建议或例子吗?谢谢。

最佳答案

我也遇到了这个问题

问题在于没有参数的方法 GetUser() 是作为类上的静态方法实现的。

而 Membership.Provider (模拟时)不包含没有参数的 GetUser() 方法。

顺便说一句,这是我解决这个问题的方法。我将静态调用封装在自己的类中,该类实现了一个接口(interface),因此可以对其进行模拟。

public interface IStaticMembershipService
{
MembershipUser GetUser();

void UpdateUser(MembershipUser user);
}

public class StaticMembershipService : IStaticMembershipService
{
public System.Web.Security.MembershipUser GetUser()
{
return Membership.GetUser();
}

public void UpdateUser(MembershipUser user)
{
Membership.UpdateUser(user);
}
}

关于tdd - 模拟成员(member),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/966795/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com