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

  • Home
  • SEARCH
  • 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 4117156
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T22:47:58+00:00 2026-05-20T22:47:58+00:00

I think I don’t understand something in Mock, if I use a DynamicMock, it

  • 0

I think I don’t understand something in Mock, if I use a DynamicMock, it should only verify the call that I expect right? Why do I get an exception in my test below? I don’t want to verify that the Session is set, I only want to verify that the _authService.EmailIsUnique is call with the right params. My problem is that in my AdminController I set _authService.Session…

 private MockRepository _mock;
 private ISession _session;
 private IAuthenticationService _authService;
 private AdminController _controller;
 private TestControllerBuilder _builder;

 [SetUp]
 public void

 Setup()
 {
        _mock = new MockRepository();
        _session = _mock.DynamicMock<ISession>();
        _authService = _mock.DynamicMock<IAuthenticationService>();
        _controller = new AdminController(_authService, _session);
        _builder = new TestControllerBuilder();
        _builder.InitializeController(_controller);
 }

 [Test]
 public void Register_Post_AddModelErrorWhenEmailNotUnique()
 {
                var userInfo = new RegisterModel();
                userInfo.Email = "the_email@domain.com";

                //_authService.Expect(x => x.Session = _session).Repeat.Once();
                Expect.Call(_authService.EmailIsUnique(userInfo.Email)).Repeat.Once().Return(false);

                _mock.ReplayAll();
                var result = _controller.Register(userInfo);
                var viewResult = (ViewResult)result;

                _authService.VerifyAllExpectations();
                result.AssertViewRendered().ForView("").WithViewData<RegisterModel>();
                Assert.That(viewResult.ViewData.Model, Is.EqualTo(userInfo));
 }

Rhino.Mocks.Exceptions.ExpectationViolationException : IAuthenticationService.set_Session(ISessionProxy2f2f623898f34cbeacf2385bc9ec641f); Expected #1, Actual #0.

Thanks for the help!

Update

Here’s part of my controller and my AuthService… I’m using Ninject as DI, since my AuthService is in my Domain and Ninject is in my WebApp I don’t know how I could use DI in my AuthService to resolve my Session. Thanks again!

    public partial class AdminController : Controller
    {
        private IAuthenticationService _authService;
        private ISession _session;

        public AdminController(IAuthenticationService authService, ISession session)
        {
            _authService = authService;
            _authService.Session = session;
            _session = session;
        }
[HttpPost]
        [Authorize(Roles = "Admin")]
        [ValidateAntiForgeryToken]
        public virtual ActionResult Register(RegisterModel userInfo)
        {
            if (!_authService.EmailIsUnique(userInfo.Email)) ModelState.AddModelError("Email", Strings.EmailMustBeUnique);
            if (ModelState.IsValid)
            {
                return RegisterUser(userInfo);
            }

            return View(userInfo);
        }
private RedirectToRouteResult RegisterUser(RegisterModel userInfo)
        {
            _authService.RegisterAdmin(userInfo.Email, userInfo.Password);
            var authToken = _authService.ForceLogin(userInfo.Email);
            SetAuthCookie(userInfo.Email, authToken);
            return RedirectToAction(MVC.Auction.Index());
        }
}

public class AuthenticationService : IAuthenticationService
    {
        public ISession Session { get; set; }

public bool EmailIsUnique(string email)
        {
            var user = Session.Single<User>(u => u.Email == email);
            return user == null;
        }
}
  • 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-20T22:47:59+00:00Added an answer on May 20, 2026 at 10:47 pm

    My problem is that in my AdminController I set _authService.Session

    Yes this is a problem indeed as this is the responsibility of your DI framework, not your controller code. So unless ISession is directly used by the controller it should be removed from it. The controller shouldn’t do any plumbing between the service and any dependencies of this service.

    So here’s an example:

    public class AdminController : Controller
    {
        private readonly IAuthenticationService _authService;
        public AdminController(IAuthenticationService authService)
        {
            _authService = authService;
        }
    
        public ActionResult Register(RegisterModel userInfo)
        {
            if (!_authService.EmailIsUnique(userInfo.Email))
            {
                ModelState.AddModelError("Email", Strings.EmailMustBeUnique);
                return View(userInfo);
            }
            return RedirectToAction("Success");
        }
    }
    

    Notice that the admin controller shouldn’t rely on any session. It already relies on the IAuthenticationService. The way this service is implemented is not important. And in this case a proper unit test would be:

    private IAuthenticationService _authService;
    private AdminController _controller;
    private TestControllerBuilder _builder;
    
    Setup()
    {
        _authService = MockRepository.GenerateStub<IAuthenticationService>();
        _controller = new AdminController(_authService);
        _builder = new TestControllerBuilder();
        _builder.InitializeController(_controller);
    }
    
    [Test]
    public void Register_Post_AddModelErrorWhenEmailNotUnique()
    {
        // arrange
        var userInfo = new RegisterModel();
        userInfo.Email = "the_email@domain.com";
        _authService
            .Stub(x => x.EmailIsUnique(userInfo.Email))
            .Return(false);
    
        // act
        var actual = _controller.Register(userInfo);
    
        // assert
        actual
            .AssertViewRendered()
            .WithViewData<RegisterModel>()
            .ShouldEqual(userInfo, "");
        Assert.IsFalse(_controller.ModelState.IsValid);
    }
    

    UPDATE:

    Now that you have shown your code I confirm:

    Remove the ISession dependency from your controller as it is not needed and leave this job to the DI framework.

    Now I can see that your AuthenticationService has a strong dependency on ISession, so constructor injection would be more adapted instead property injection. Use property injection only for optional dependencies:

    public class AuthenticationService : IAuthenticationService
    {
        private readonly ISession _session;
        public AuthenticationService(ISession session)
        {
            _session = session;
        }
    
        public bool EmailIsUnique(string email)
        {
            var user = _session.Single<User>(u => u.Email == email);
            return user == null;
        }
    }
    

    and the last part that is left is the plumbing. This is done in the ASP.NET MVC application which has references to all other layers. And because you have mentioned Ninject you could install the Ninject.MVC3 NuGet and in the generated ~/App_Start/NinjectMVC3 simply configure the kernel:

    /// <summary>
    /// Load your modules or register your services here!
    /// </summary>
    /// <param name="kernel">The kernel.</param>
    private static void RegisterServices(IKernel kernel)
    {
        kernel.Bind<ISession>().To<SessionImpl>();
        kernel.Bind<IAuthenticationService>().To<AuthenticationService>();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is beyond both making sense and my control. That being said here is
I have a project that adds elements to an AutoCad drawing. I noticed that
I am using a 3rd-party rotator object, which is providing a smooth, random rotation
I have found this example on StackOverflow: var people = new List<Person> { new
I'm in the process of porting some code from Linux to Mac OS X.

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.