简体   繁体   English

嘲弄会员资格

[英]Mocking Membership

I'm writing a custom Profile provider, but I still intend to use the default AspNetSqlMembershipProvider as my Membership provider. 我正在编写自定义配置文件提供程序,但我仍打算使用默认的AspNetSqlMembershipProvider作为我的成员资格提供程序。 My GetAllProfiles() method in my Profile provider looks like this: 我的配置文件提供程序中的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   }

How do I mock the Membership.GetUser() call so that I can write tests for this method? 如何模拟Membership.GetUser()调用以便我可以为此方法编写测试? Any suggestions or examples? 有什么建议或例子吗? Thanks. 谢谢。

I'm running into this problem as well 我也遇到了这个问题

the problem lies in the fact that the method GetUser() without parameters is implemented as a static method on the class. 问题在于,没有参数的方法GetUser()被实现为类的静态方法。

Whereas the Membership.Provider (when mocked) does not contain a GetUser() method without parameters. Membership.Provider(模拟时)不包含没有参数的GetUser()方法。

By the way here is how I fixed this problem. 顺便说一句,这是我如何解决这个问题。 I encapsulated the static call in my own class which implements an interface so it can be mocked. 我在我自己的类中封装了静态调用,它实现了一个接口,因此可以进行模拟。

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

Could you inject a MembershipProvider instance into your profile provider and, if none is injected, fall back on using Membership.Provider ? 你可以将一个MembershipProvider实例注入你的配置文件提供程序,如果没有注入,可以使用Membership.Provider吗?

public MembershipProvider MembershipProvider
{
    get { return _membershipProvider ?? Membership.Provider; }
    set { _membershipProvider = value; }
}

Your profile provider would interact with the membership provider through the value returned by this property. 您的配置文件提供程序将通过此属性返回的值与成员资格提供程序进行交互。 In your test you'd inject the fake/mock MembershipProvider instance. 在您的测试中,您将注入假/模拟MembershipProvider实例。

If you instead want to just mock the static methods on Membership, you'll have to use something like TypeMock , I guess. 如果您只是想在成员资格上模拟静态方法,我猜你必须使用像TypeMock这样的东西。

In ASP.NET MVC they solved this by encapsulating (wrapping) the membership functionality in a MebershipService. 在ASP.NET MVC中,他们通过在MebershipService中封装(包装)成员资格功能来解决这个问题。 Which (for instance: through injection) you can then easily mock in your tests. 其中(例如:通过注射)您可以轻松地在测试中进行模拟。

An example of mocking services... http://www.asp.net/learn/mvc/tutorial-30-cs.aspx they don't use injection though. 模拟服务的一个例子...... http://www.asp.net/learn/mvc/tutorial-30-cs.aspx他们不使用注入。

A nice example is actually the test project generated when you create an ASP.NET application. 一个很好的例子实际上是创建ASP.NET应用程序时生成的测试项目。 In the following code you can see how they mock The FormsAuthentication and Membership objects: 在下面的代码中,您可以看到它们如何模拟FormsAuthentication和Membership对象:

    [TestMethod]
    public void ConstructorSetsProperties()
    {
        // Arrange
        IFormsAuthentication formsAuth = new MockFormsAuthenticationService();
        IMembershipService membershipService = new AccountMembershipService();

        // Act
        AccountController controller = new AccountController(formsAuth, membershipService);

        // Assert
        Assert.AreEqual(formsAuth, controller.FormsAuth, "FormsAuth property did not match.");
        Assert.AreEqual(membershipService, controller.MembershipService, "MembershipService property did not match.");
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM