Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You'll have to mock an <code>IPrincipal</code> for your user object, set it up so that <code>.IsInRole("Administrator")</code> returns <code>true</code>, and then set your <code>fakeContext</code> up to return that <code>IPrincipal</code> for the<code>.User</code> property. It would be something like this:</p> <p><strong>EDIT:</strong> It turns out the OP is using Rhino Mocks, and the code I provided was for Moq. Here's an attempt att writing Rhino code, although I have never actually used Rhino myself. The original Moq code can be found below.</p> <p>In Rhino Mocks, you'll want to add another helper method, or change the current one, so you have the following:</p> <pre><code>public static HttpContextBase FakeHttpContext(this MockRepository mocks, string url, IPrincipal user) { // Do the same setup as Scott does... // ...and add this: SetupResult.For(context.User).Return(user); mocks.Replay(context); return context, } </code></pre> <p>Then you declare and set up your IPrincipal mock object like this before the call to <code>FakeHttpContext</code>, and send the mock object in as the third parameter.</p> <hr> <p>In Moq:</p> <pre><code>fakeContext = MvcMockHelpers.FakeHttpContext("~/Article/Create"); fakeUser = new Mock&lt;IPrincipal&gt;(); fakeUser.Expect(usr =&gt; usr.IsInRole(It.IsAny&lt;String&gt;())).Returns(true); fakeContext.Expect(context =&gt; context.User).Returns(fakeUser.Object); </code></pre> <p><em>(Disclaimer: It was a while since I wrote a unit test, and this code has not been tested even for compilation errors. Hence, there might be some wrinkles that need to be ironed out before you can use this code, but you get the general idea...)</em></p>
 

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