测试User.IsInRole在MVC.NET [英] Testing the User.IsInRole in MVC.NET

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

问题描述

我想在我的应用程序来测试User.IsInRole(管理员),实际上试图将用户角色分配(管理员),使我的测试将通过。我使用的斯科特Hanselman的MvcMockHelpers要做到这一点,并在那一刻我有以下的测试。

I am trying to test the User.IsInRole("Administrator") in my application, and actually trying to assign the User Role to ("Administrator") so that my test will pass. I am using Scott Hanselman's MvcMockHelpers to do this, and at the moment I have the following test.

    [Test]
    public void Create_CanInsertNewArticleView_IsNotNull()
    {
        // Arrange
        var controller = new ArticleController();

        MockRepository mockRepo = new MockRepository();
        var fakeContext = MvcMockHelpers.FakeHttpContext(mockRepo, "~/Article/Create");
        fakeContext.User.IsInRole("Administrator");

        // Act
        Article fakeArticle = FakeObjects.ReturnFakeArticle();

        var result = controller.Create(fakeArticle) as ViewResult;

        // Assert
        Assert.IsNotNull(result);
    }

然而,实际控制人的用户是空的时刻。

However the actual controller's User is null at the moment.

谁能帮助我,并告诉我正确的测试应该是什么, User.IsInRole(管理员)

Can anyone help me out and tell me what the correct test should be, to User.IsInRole("Administrator")

感谢您的帮助和时间

约翰

推荐答案

请参阅this相关答案了解详情。


下面是从对方的回答中code片断转换成犀牛嘲笑:

Here's the code snippet from the other answer converted to Rhino Mocks:

var user = new GenericPrincipal(new GenericIdentity(string.Empty), null);
var httpCtx = MockRepository.GenerateStub<HttpContextBase>();
httpCtx.User = user;

var controllerCtx = new ControllerContext();
controllerCtx.HttpContext = httpCtx;

sut.ControllerContext = controllerCtx;

这篇关于测试User.IsInRole在MVC.NET的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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