Note that there are some explanatory texts on larger screens.

plurals
  1. POMVCContrib TestHelper problem with session.clear, session.abandon and Rhino Mock
    text
    copied!<p>Hi I'm trying to unit test my logout action on my controller but I have hard times to test or stub my Session in the HttpContext. I'm using <a href="http://mvccontrib.codeplex.com/wikipage?title=TestHelper" rel="nofollow">MVC Contrib TestHelper</a> to make it easier but now I need a little help.</p> <p>Here's my test :</p> <pre><code>[TestFixture] public class SessionControllerTest { private ISession _session; private IConfigHelper _configHelper; private IAuthenticationService _authService; //private IMailHelper _mailHelper; private ICryptographer _crypto; private SessionController _controller; private TestControllerBuilder _builder; private MockRepository _mock; [SetUp] public void Setup() { _mock = new MockRepository(); _session = _mock.DynamicMock&lt;ISession&gt;(); _configHelper = _mock.DynamicMock&lt;IConfigHelper&gt;(); _authService = _mock.DynamicMock&lt;IAuthenticationService&gt;(); //_mailHelper = _mock.DynamicMock&lt;IMailHelper&gt;(); _crypto = _mock.DynamicMock&lt;ICryptographer&gt;(); _controller = new SessionController(_authService, _session, _crypto, _configHelper); _builder = new TestControllerBuilder(); _builder.InitializeController(_controller); } [Test] public void Logout_ReturnRedirectToAction() { _builder.InitializeController(_controller); _authService.SignOut(); LastCall.Repeat.Once(); _builder.Session["memberNumber"] = string.Empty; LastCall.Repeat.Once(); _controller.Session.Clear(); LastCall.Repeat.Any(); _controller.Session.Abandon(); LastCall.Repeat.Any(); //_builder.Session.Stub(s =&gt; s.Clear()); //_builder.Session.Stub(s =&gt; s.Abandon()); //_builder.Session.Clear(); //LastCall.Repeat.Once(); //_builder.Session.Abandon(); //LastCall.Repeat.Once(); _mock.ReplayAll(); var result = _controller.Logout(); _mock.VerifyAll(); result.AssertActionRedirect().ToAction&lt;SessionController&gt;(c =&gt; c.Login()); } </code></pre> <p>You can see my differents attemps. I get an error telling me that Session.Abandon() is not implemented, witch is right when you take a look at MVCContrib's TestHelper. But how can I mock or Stub the Session that's already mocked by the TestHelper?</p> <p>The Exception in NUnit : </p> <blockquote> <p>System.NotImplementedException : The method or operation is not implemented. at MvcContrib.TestHelper.MockSession.Abandon()</p> </blockquote> <p>Thank you for the help!</p> <p>EDIT : Here's the new working test </p> <pre><code>[Test] public void Logout_ReturnRedirectToAction() { _builder.InitializeController(_controller); var mockSession = _mock.Stub&lt;HttpSessionStateBase&gt;(); _controller.HttpContext.BackToRecord(); _controller.HttpContext.Stub(c =&gt; c.Session).Return(mockSession); _controller.HttpContext.Replay(); _authService.SignOut(); LastCall.Repeat.Once(); _builder.Session["memberNumber"] = string.Empty; _controller.Session.Clear(); LastCall.Repeat.Once(); _controller.Session.Abandon(); LastCall.Repeat.Once(); _mock.ReplayAll(); var result = _controller.Logout(); _mock.VerifyAll(); result.AssertActionRedirect().ToAction&lt;SessionController&gt;(c =&gt; c.Login()); } </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