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 7559275
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T12:35:41+00:00 2026-05-30T12:35:41+00:00

I am trying to write some unit tests for my MVC3 project (the first

  • 0

I am trying to write some unit tests for my MVC3 project (the first tests for this project) and I am getting stumped. Basically, I have a MemberQueries class that is used by my MemberController to handle all the logic.

I want to start writing tests on this class and want to start with a simple example. I have a method in this class called IsEditModeAvailable which determines if the user is a member of the “Site Administrator” role or that the user is able to edit their own data, but no one elses. I determine the last requirement by comparing the passed in Id value to the HttpContext User property.

The problem that I’m running into is I don’t know how to mock or inject the proper parameters into my unit tests when creating the MemberQueries object. I am using, NUnit, Moq and Ninject, but I’m just not sure how to write the code. If I’m just not structuring this properly, please let me know as I’m a complete noob to unit testing.

Here’s a sample of the code from my MemberQueries class:

public class MemberQueries : IMemberQueries
{
  private readonly IRepository<Member> _memberRepository;
  private readonly IMemberServices _memberServices;
  private readonly IPrincipal _currentUser;

  public MemberQueries(IUnitOfWork unitOfWork, IMemberServices memberServices, IPrincipal currentUser)
  {
    _memberRepository = unitOfWork.RepositoryFor<Member>();
    _memberServices = memberServices;
    _currentUser = currentUser;
  }

  public bool IsEditModeAvailable(int memberIdToEdit)
  {
    if (_currentUser.IsInRole("Site Administrator")) return true;
    if (MemberIsLoggedInUser(memberIdToEdit)) return true;
    return false;
  }

  public bool MemberIsLoggedInUser(int memberIdToEdit)
  {
    var loggedInUser = _memberServices.FindByEmail(_currentUser.Identity.Name);
    if (loggedInUser != null && loggedInUser.Id == memberIdToEdit) return true;
    return false;
  }

}

Here’s a sample from my MemberServices class (which is in my domain project, referenced by MemberQueries):

public class MemberServices : IMemberServices
{
  private readonly IRepository<Member> _memberRepository;

  public MemberServices(IUnitOfWork unitOfWork)
  {
    _memberRepository = unitOfWork.RepositoryFor<Member>();
  }

  public Member Find(int id)
  {
    return _memberRepository.FindById(id);
  }

  public Member FindByEmail(string email)
  {
    return _memberRepository.Find(m => m.Email == email).SingleOrDefault();
  }
}

Finally, here’s the stub of the unit test I am trying to write:

[Test]
public void LoggedInUserCanEditTheirOwnInformation()
{
  var unitOfWork = new UnitOfWork();

  var currentUser = new Mock<IPrincipal>();
  // I need to somehow tell Moq that the logged in user has a HttpContext.User.Name of "jdoe@acme.com"

  var memberServices = new Mock<MemberServices>();
  // I then need to tell Moq that it's FindByEmail("jdoe@acme.com") method should return a member with a UserId of 1

  var memberQueries = new MemberQueries(unitOfWork, memberServices.Object, currentUser.Object);

  // If the logged in user is "jdoe@acme.com" who has an Id of 1, then IsEditModeAvailable(1) should return true
  Assert.IsTrue(memberQueries.IsEditModeAvailable(1));
}
  • 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-30T12:35:42+00:00Added an answer on May 30, 2026 at 12:35 pm

    It looks like you are trying to test the MemberQueries.IsEditModeAvailable method. You have 2 cases to cover here. The Site Administrators case and the case where there’s a currently logged user whose id matches the one passed as argument. And since the MemberQueries class relies purely on interfaces you could mock everything:

    [TestMethod]
    public void EditMode_Must_Be_Available_For_Site_Administrators()
    {
        // arrange
        var unitOfWork = new Mock<IUnitOfWork>();
        var currentUser = new Mock<IPrincipal>();
        currentUser.Setup(x => x.IsInRole("Site Administrator")).Returns(true);
        var memberServices = new Mock<IMemberServices>();
        var memberQueries = new MemberQueries(unitOfWork.Object, memberServices.Object, currentUser.Object);
    
        // act
        var actual = memberQueries.IsEditModeAvailable(1);
    
        // assert
        Assert.IsTrue(actual);
    }
    
    [TestMethod]
    public void EditMode_Must_Be_Available_For_Logged_In_Users_If_His_Id_Matches()
    {
        // arrange
        var unitOfWork = new Mock<IUnitOfWork>();
        var currentUser = new Mock<IPrincipal>();
        var identity = new Mock<IIdentity>();
        identity.Setup(x => x.Name).Returns("john.doe@gmail.com");
        currentUser.Setup(x => x.Identity).Returns(identity.Object);
        currentUser.Setup(x => x.IsInRole("Site Administrator")).Returns(false);
        var memberServices = new Mock<IMemberServices>();
        var member = new Member
        {
            Id = 1
        };
        memberServices.Setup(x => x.FindByEmail("john.doe@gmail.com")).Returns(member);
        var memberQueries = new MemberQueries(unitOfWork.Object, memberServices.Object, currentUser.Object);
    
        // act
        var actual = memberQueries.IsEditModeAvailable(1);
    
        // assert
        Assert.IsTrue(actual);
    }
    

    Actually there’s a third case you need to cover: you have a currently logged in user, who is not a Site Administrator and whose id doesn’t match the one passed as argument:

    [TestMethod]
    public void EditMode_Should_Not_Be_Available_For_Logged_In_Users_If_His_Id_Doesnt_Match()
    {
        // arrange
        var unitOfWork = new Mock<IUnitOfWork>();
        var currentUser = new Mock<IPrincipal>();
        var identity = new Mock<IIdentity>();
        identity.Setup(x => x.Name).Returns("john.doe@gmail.com");
        currentUser.Setup(x => x.Identity).Returns(identity.Object);
        currentUser.Setup(x => x.IsInRole("Site Administrator")).Returns(false);
        var memberServices = new Mock<IMemberServices>();
        var member = new Member
        {
            Id = 2
        };
        memberServices.Setup(x => x.FindByEmail("john.doe@gmail.com")).Returns(member);
        var memberQueries = new MemberQueries(unitOfWork.Object, memberServices.Object, currentUser.Object);
    
        // act
        var actual = memberQueries.IsEditModeAvailable(1);
    
        // assert
        Assert.IsFalse(actual);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We are trying to write Unit Tests in our ASP.Net MVC project. Some of
I am trying to write some unit tests for my code. In out project
I am trying to write some unit tests for a small web service written
I'm attempting to write some unit tests using EasyMock and TestNG and have run
I'm trying to write some unit tests for a JPA model that I've built
I'm trying to write some unit tests for Command object validations. When my command
I'm trying to write some tests for an MVC application we're developing. We have
I'm trying to write some unit tests for a gwt-dispatch service with JUnit. I'm
I'm trying to find a way to write some unit-tests that can be used
I am trying to write some unit tests that test the endpoints for my

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.