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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T17:08:53+00:00 2026-05-31T17:08:53+00:00

I have a pretty standard .NET MVC 3 application that uses Ninject for dependency

  • 0

I have a pretty standard .NET MVC 3 application that uses Ninject for dependency injection. What I’m trying to unit test is the view that gets returned from the controller. There is a controller with a group service on it and the service has a repository. The service first checks to see if that group exists and if it does, it returns false along with an error. The controller then checks the boolean value to determine whether to move the user on to the listing of groups or stay on the current page and display a value.

Controller

[HttpPost]
public ActionResult AddGroup(Group g)
{
   string error = string.Empty;
   if (groupService.Save(g, CurrentUser, out error))
   {
       return RedirectToAction("GetGroups");
   }
   else
   {
       ViewData["Error"] = error;
       return View("AddGroup");
   }
}

Service

[Inject]
public IGroupRepository CurrentGroupRepo { get; set; }

public bool Save(Group group, string username, out string error)
{
    error = string.Empty;
    var found = CurrentGroupRepo.GetGroup(group.Descr, false);
    if (found != null)
    {
        error = "That group alread exists";
        return false;
    }

    if (group.CreatedBy == null || group.CreatedBy == string.Empty)
    {
        group.CreatedBy = username;
        group.CreatedOn = DateTime.Now;
        group.IsDeleted =false;
    }

    group.ModifiedBy = username;
    group.ModifiedOn = DateTime.Now;

    try
    {
        if (group.GroupID == 0)
        {
            CurrentGroupRepo.AddGroup(group);
        }
        else
        {
            CurrentGroupRepo.UpdateGroup(group);
        }
        return true;
    }
    catch (Exception ex)
    {
        error = ex.Message + ex.StackTrace;
        return false;
    }
}

Unit Test

[TestMethod]
public void TestAddGroup()
{
    // Arrange
    Mock<IUserService> uService = new Mock<IUserService>();
    Mock<IGroupService> gService = new Mock<IGroupService>();
    Mock<IObservationService> oService = new Mock<IObservationService>();
    Mock<IGroupRepository> mockGroupRepo = new Mock<IGroupRepository>();

        string error = string.Empty;
        List<Group> groups = new List<Group>();
        Group newGroup = new Group() { Descr = "Test Group 1234566", IsDeleted = false };
        Mock<IGroupRepository> mockGroupRepo = new Mock<IGroupRepository>();

        mockGroupRepo.Setup(cr => cr.GetGroups(It.IsAny<bool>())).Returns(
            delegate {
                return groups;
            });
        mockGroupRepo.Setup(cr => cr.GetGroup(It.IsAny<string>(), It.IsAny<bool>())).Returns(
            delegate(Group _group) {
                return groups.Where(f => f.Descr == _group.Descr).FirstOrDefault();
            });
        mockGroupRepo.Setup(cr => cr.AddGroup(newGroup)).Returns(
            delegate {
                groups.Add(newGroup);
                return newGroup;
            });
        gService.SetupGet(d => d.CurrentGroupRepo).Returns(mockGroupRepo.Object);


    AdminController controller = new AdminController(gService.Object, uService.Object, oService.Object);
    Group newGroup = new Group() { Descr = "Test Group 1234566", IsDeleted = false };
    var success = (ViewResult)controller.AddGroup(newGroup);
    Assert.IsTrue("GetGroups" == success.ViewName);

    var failure = (ViewResult)controller.AddGroup(newGroup);
    Assert.IsTrue("AddGroup" == failure.ViewName);
}

What I’m trying to test is that when I add one group, it should work and then when I add the same group, it should go to a different view. But right now, the unit test is using the real repository instead of a mocked one. How do I get the repository on the mocked service to be controlled by the unit test instead of actually using a real repository?

  • 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-31T17:08:54+00:00Added an answer on May 31, 2026 at 5:08 pm

    If you are unit testing the Controller then you don’t need to worry about the Repository because your service is mocked. As far as the controller is concerned there is no repository, all it knows about is the IGroupService contract.

    I cannot see how your testis using a real repository given the code you have supplied. If you want to change the out come of your test you will need to provide some mocked responses to your IGroupService mock.

    You need a second unit test for the GroupService that takes in a Mocked Repository.

    IMO a unit test should only test a single class, everything else
    should either be mocked or be very simple dto objects. If you are
    doing more then it is an integration test.

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

Sidebar

Related Questions

I have a pretty standard table set-up in a current application using the .NET
I have an ASP.NET MVC application which is pretty simple so far, but I
I am currently writing an financial application, and we have a pretty standard customer
I have pretty big background of .net, and I've decided that i want to
We've got an existing ASP.NET web application that already uses a home-grown role based
We have an ASP.NET project (40 or so Web forms, 50 tables, pretty standard
We have a web application written in ASP.NET for .NET 3.5, using standard web
Summary We have an ASP.NET application that allows users to query a SQL Server
I have what I'd consider a standard .NET MVC3 Repository pattern project that I've
I have a pretty standard django app, and am wondering how to set the

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.