起订量Roles.AddUserToRole测试 [英] Moq Roles.AddUserToRole test

查看:129
本文介绍了起订量Roles.AddUserToRole测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写单元测试,使用起订量和MvcContrib TestHelper类ASP.NET MVC 1.0的项目。我遇到了一个问题。

I am writing unit tests for a project in ASP.NET MVC 1.0 using Moq and MvcContrib TestHelper classes. I have run into a problem.

当我来到Roles.AddUserToRole在我的AccountController,我得到一个System.NotSupportedException。 Roles类是静态的,起订量不能模拟静态类。

When I come to Roles.AddUserToRole in my AccountController, I get a System.NotSupportedException. The Roles class is static and Moq cannot mock a static class.

我该怎么办?

推荐答案

您可以使用像DI(依赖注入)的模式。在你的情况,我会通过一个RoleProvider到的AccountController,这将是默认的缺省RoleProvider,并在测试中模拟对象。是这样的:

You could use a pattern like DI (Dependency Injection). In your case, I would pass a RoleProvider to the AccountController, which would be the default RoleProvider by default, and a mock object in your tests. Something like:

public class AccountController
{
    private MembershipProvider _provider;
    private RoleProvider roleProvider;

    public AccountController()
      : this(null, null)
    {
    }

    public AccountController(MembershipProvider provider, RoleProvider roleProvider)
    {
      _provider = provider ?? Membership.Provider;
      this.roleProvider = roleProvider ?? System.Web.Security.Roles.Provider;
    }
}

在MVC运行时将调用默认构造函数,这反过来将初始化默认的角色提供了的AccountController。在单元测试,可以直接调用重载的构造函数,并传递一个MockRoleProvider(或使用最小起订量来为您创建它):

The MVC runtime will call the default constructor, which in turn will initialize the AccountController with the default role provider. In your unit test, you can directly call the overloaded constructor, and pass a MockRoleProvider (or use Moq to create it for you):

[Test]
public void AccountControllerTest()
{
    AccountController controller = new AccountController(new MockMembershipProvider(), new MockRoleProvider());
}

修改:这里就是我嘲笑了整个HttpContext的,包括主要用户。
要获得的HttpContext的起订量版本:

EDIT: And here's how I mocked the entire HttpContext, including the principal user. To get a Moq version of the HttpContext:

public static HttpContextBase GetHttpContext(IPrincipal principal)
{
  var httpContext = new Mock<HttpContextBase>();
  var request = new Mock<HttpRequestBase>();
  var response = new Mock<HttpResponseBase>();
  var session = new Mock<HttpSessionStateBase>();
  var server = new Mock<HttpServerUtilityBase>();
  var user = principal;


  httpContext.Setup(ctx => ctx.Request).Returns(request.Object);
  httpContext.Setup(ctx => ctx.Response).Returns(response.Object);
  httpContext.Setup(ctx => ctx.Session).Returns(session.Object);
  httpContext.Setup(ctx => ctx.Server).Returns(server.Object);
  httpContext.Setup(ctx => ctx.User).Returns(user);

  return httpContext.Object;
}

模拟实现校长:

  public class MockPrincipal : IPrincipal
  {
    private IIdentity _identity;
    private readonly string[] _roles;

    public MockPrincipal(IIdentity identity, string[] roles)
    {
      _identity = identity;
      _roles = roles;
    }

    public IIdentity Identity
    {
      get { return _identity; }
      set { this._identity = value; }
    }

    public bool IsInRole(string role)
    {
      if (_roles == null)
        return false;
      return _roles.Contains(role);
    }
  }

一个MockIdentity:

A MockIdentity:

public class MockIdentity : IIdentity
  {
    private readonly string _name;

    public MockIdentity(string userName)    {
      _name = userName;
    }

    public override string AuthenticationType
    {
      get { throw new System.NotImplementedException(); }
    }

    public override bool IsAuthenticated
    {
      get { return !String.IsNullOrEmpty(_name); }
    }

    public override string Name
    {
      get { return _name; }
    }
  }

和魔法召唤:

MockIdentity identity = new MockIdentity("JohnDoe");
var httpContext = MoqHelpers.GetHttpContext(new MockPrincipal(identity, null));

请注意,我编辑过上面的code留下了一些定制的东西,但我敢肯定这应该仍然工作。

Note that I edited the code above to leave out some custom stuff, but I'm quite sure this should still work.

这篇关于起订量Roles.AddUserToRole测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆