Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3750970
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T09:00:28+00:00 2026-05-19T09:00:28+00:00

Hi I’m trying to unit test my logout action on my controller but I

  • 0

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 MVC Contrib TestHelper to make it easier but now I need a little help.

Here’s my test :

[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<ISession>();
        _configHelper = _mock.DynamicMock<IConfigHelper>();
        _authService = _mock.DynamicMock<IAuthenticationService>();
        //_mailHelper = _mock.DynamicMock<IMailHelper>();
        _crypto = _mock.DynamicMock<ICryptographer>();
        _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 => s.Clear());
        //_builder.Session.Stub(s => 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<SessionController>(c => c.Login());
    }

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?

The Exception in NUnit :

System.NotImplementedException : The
method or operation is not
implemented. at
MvcContrib.TestHelper.MockSession.Abandon()

Thank you for the help!

EDIT : Here’s the new working test

[Test]
    public void Logout_ReturnRedirectToAction()
    {
        _builder.InitializeController(_controller);
        var mockSession = _mock.Stub<HttpSessionStateBase>();

        _controller.HttpContext.BackToRecord();
        _controller.HttpContext.Stub(c => 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<SessionController>(c => c.Login());
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T09:00:28+00:00Added an answer on May 19, 2026 at 9:00 am

    It’s been a while since I used MvcContrib, so I pulled down the latest code and made a quick test project. It’s very odd. Looking at the MvcContrib code (specifically, TestControllerBuilder), it creates mocks for most of the objects (request, response, server, etc…), but not for Session. I’m not sure why this is — probably have to ask the creators.

    However, there is a way to mock it yourself. You can create your own mock session and tell the controller to use yours instead of the one from MvcContrib.TestHelpers. Here’s what I did in my test:

    var mockSession = MockRepository.GenerateStub<HttpSessionStateBase>();
    controller.HttpContext.BackToRecord();
    controller.HttpContext.Stub(c => c.Session).Return(mockSession);
    controller.HttpContext.Replay();
    

    Now I run my controller method and then use Rhino.Mocks’ AAA syntax for making sure the Abandon method was called:

    controller.Session.AssertWasCalled(s => s.Abandon());
    

    If you want to use record/replay semantics, you could set your expectations before calling controller.HttpContext.Replay().

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a French site that I want to parse, but am running into
I have thousands of HTML files to process using Groovy/Java and I need to
I am trying to loop through a bunch of documents I have to put
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.