惩戒WebSecurity提供商 [英] Mocking WebSecurity provider

查看:203
本文介绍了惩戒WebSecurity提供商的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图创建我的控制器一些简单的单元测试和我遇到的问题。

I'm trying to create some simple unit tests for my controllers and I've come across an issue.

我使用MVC中4新成员提供和获取 WebSecurity.CurrentUserId ,并在数据库中存储的值。

I'm using the new membership provider in MVC 4 and getting the WebSecurity.CurrentUserId and storing that value in the database.

当我运行我的单元测试,对于这一点的失败,我想我已经跟踪这回事实,即 WebSecurity 并不是在所有的嘲笑。

When I run my unit tests against this it's failing and I think I have track this back to the fact that the WebSecurity isn't being mocked at all.

下面是我的code。如果它有助于在所有,

Here's my code if it helps at all,

控制器

    [HttpPost]
    public ActionResult Create(CreateOrganisationViewModel viewModel)
    {
        if (ModelState.IsValid)
        {
            Group group = _groupService.Create(
                new Group
                {
                    Name = viewModel.Name,
                    Slug = viewModel.Name.ToSlug(),
                    Profile = new Profile
                    {
                        Country = viewModel.SelectedCountry,
                        Description = viewModel.Description
                    },
                    CreatedById = WebSecurity.CurrentUserId,
                    WhenCreated = DateTime.UtcNow,
                    Administrators = new List<User> {_userService.SelectById(WebSecurity.CurrentUserId)}
                });
            RedirectToAction("Index", new {id = group.Slug});
        }
        return View(viewModel);
    }

测试

    [Test]
    public void SuccessfulCreatePost()
    {
        CreateOrganisationViewModel createOrganisationViewModel = new CreateOrganisationViewModel
        {
            Description = "My Group, bla bla bla",
            Name = "My Group",
            SelectedCountry = "gb"
        };

        IUserService userService = MockRepository.GenerateMock<IUserService>();
        IGroupService groupService = MockRepository.GenerateMock<IGroupService>();
        groupService.Stub(gS => gS.Create(null)).Return(new Group {Id = 1});
        GroupController controller = new GroupController(groupService, userService);
        RedirectResult result = controller.Create(createOrganisationViewModel) as RedirectResult;
        Assert.AreEqual("Index/my-group", result.Url);
    }

感谢

推荐答案

一个可能的解决方案是创建一个围绕WebSecurity一个包装类 - 比如 WebSecurityWrapper 。揭露静态WebSecurity方法,如 WebSecurity.CurrentUserId 作为包装实例方法。该包装在这种情况下,工作会简单地委托所有 WebSecurity 呼叫。

A possible solution is to create a wrapper class around WebSecurity - say WebSecurityWrapper. Expose the static WebSecurity methods such as WebSecurity.CurrentUserId as instance methods on the wrapper. The wrapper's job in this case would be simply to delegate all the calls to WebSecurity.

注入 WebSecurityWrapper GroupController 的构造。现在,您可以使用您所选择的模拟框架存根包装 - 从而测试控制器逻辑

Inject WebSecurityWrapper into the GroupController's constructor. Now, you can stub the wrapper using the mocking framework of your choice - and thus test the controller logic.

希望这有助于。

这篇关于惩戒WebSecurity提供商的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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