Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Sure this is possible. This is done in unit testing all the time. Personally, I use <a href="http://www.ayende.com/projects/rhino-mocks.aspx" rel="nofollow noreferrer">RhinoMocks</a> along with <a href="http://mvccontrib.codeplex.com/wikipage?title=TestHelper&amp;referringTitle=Documentation" rel="nofollow noreferrer">MVCContrib.TestHelper</a> to call my controller actions, both authenticated and not authenticated. Here is a sample piece of code that I use to test my controllers:</p> <pre><code> private MockRepository _mocks = new MockRepository(); public ControllerTestBase() { } public static T GetController&lt;T&gt;() { return this.GetController&lt;T&gt;(null); } public T GetController&lt;T&gt;(BPUser authenticatedUser) { TestControllerBuilder testContBuilder = new TestControllerBuilder(); if (authenticatedUser != null) { var identity = _mocks.DynamicMock&lt;IIdentity&gt;(); SetupResult.For(identity.IsAuthenticated).Return(true); SetupResult.For(identity.Name).Return(authenticatedUser.ID.ToString()); _mocks.Replay(identity); var pAttrs = new System.Collections.Hashtable(); pAttrs["UserID"] = authenticatedUser.ID.ToString(); pAttrs["UserName"] = authenticatedUser.UserName; testContBuilder.HttpContext.User = new UserPrinciple(identity, new string[] { }, pAttrs); } return testContBuilder.CreateController&lt;T&gt;(); } </code></pre> <p>There is a little custom stuff in there for my implementation of IPrinciple, but hopefully it's a start.</p> <p><strong><em>Edit - Example Usage</em></strong> Here's how you would use this:</p> <pre><code>var authenticatedUser = SomeCodeToGetMyUser(); var controller = ControllerTestBase.GetController&lt;YourController&gt;(authenticatedUser); controller.UpdateHeartbeat(); </code></pre>
 

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