Note that there are some explanatory texts on larger screens.

plurals
  1. POMocking WebSecurity provider
    primarykey
    data
    text
    <p>I'm trying to create some simple unit tests for my controllers and I've come across an issue.</p> <p>I'm using the new membership provider in MVC 4 and getting the <code>WebSecurity.CurrentUserId</code> and storing that value in the database.</p> <p>When I run my unit tests against this it's failing and I think I have track this back to the fact that the <code>WebSecurity</code> isn't being mocked at all.</p> <p>Here's my code if it helps at all,</p> <p>The Controller</p> <pre><code> [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&lt;User&gt; {_userService.SelectById(WebSecurity.CurrentUserId)} }); RedirectToAction("Index", new {id = group.Slug}); } return View(viewModel); } </code></pre> <p>The Test</p> <pre><code> [Test] public void SuccessfulCreatePost() { CreateOrganisationViewModel createOrganisationViewModel = new CreateOrganisationViewModel { Description = "My Group, bla bla bla", Name = "My Group", SelectedCountry = "gb" }; IUserService userService = MockRepository.GenerateMock&lt;IUserService&gt;(); IGroupService groupService = MockRepository.GenerateMock&lt;IGroupService&gt;(); groupService.Stub(gS =&gt; 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); } </code></pre> <p>Thanks</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload