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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T20:02:09+00:00 2026-05-21T20:02:09+00:00

I’m trying to get my head around how one would unit test an ASP.NET

  • 0

I’m trying to get my head around how one would unit test an ASP.NET MVC project that accesses data through a repository of some sort.

During the unit tests I’d obviously want to create a mock repository but how do I pass this mock repository to the Controller instance being tested? Also how would the actual repository, that’s really connected to a database, find its way to the controller?

Do I simply do this through the constructors as I’ve shown below? I think this is how I should set up my controllers, but I’d like some confirmation that this is correct:

public class SampleController : Controller
{
    private IRepository _repo;

    //Default constructor uses a real repository
    // new ConcreteRepo() could also be replaced by some static 
    // GetRepository() method somewhere so it would be easy to modify
    //which concrete IRepository is being used
    public SampleController():this(new ConcreteRepo())
    {

    }

    //Unit tests pass in mock repository here
    public SampleController(IRepository repo)
    {
        _repo = repo;
    }
}
  • 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-21T20:02:10+00:00Added an answer on May 21, 2026 at 8:02 pm

    As everyone has already said, you’ll want to use an IoC* or DI** container. But what they haven’t said is why this is the case.

    The idea is that a DI container will let you bypass ASP.NET MVC’s default controller-construction strategy of requiring a parameterless constructor. Thus, you can have your controllers explicitly state their dependencies (as interfaces preferably). How those interfaces map to concrete instances is then the business of the DI container, and is something you will configure in either Global.asax.cs (live) or your test fixture setup (for unit testing).

    This means your controller doesn’t need to know anything about concrete implementations of its dependencies, and thus we follow the Dependency Inversion Principle: “High-level modules should not depend on low-level modules. Both should depend on abstractions.”

    For example, if you were to use AutoFac, you would do this:

    // In Global.asax.cs's Application_Start
    using Autofac;
    using Autofac.Integration.Mvc;
    
    var builder = new ContainerBuilder();
    builder.RegisterControllers(Assembly.GetExecutingAssembly());
    builder.Register<IRepository>(() => new ConcreteRepo());
    var container = builder.Build();
    DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
    
    // In your unit test:
    var controllerInstance = new SampleController(new InMemoryFakeRepo());
    
    // In SampleController
    public class SampleController : Controller
    {
        private readonly IRepository _repo;
    
        public SampleController(IRepository repo)
        {
            _repo = repo;
        }
    
        // No parameterless constructor! This is good; no accidents waiting to happen!
        // No dependency on any particular concrete repo! Excellent!
    }
    

    * IoC = inversion of control
    ** DI = dependency inversion
    (the two terms are often used interchangeably, which is not really correct IMO)

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

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
Basically, what I'm trying to create is a page of div tags, each has
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I am trying to understand how to use SyndicationItem to display feed which is
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 am trying to loop through a bunch of documents I have to put
I'm making a simple page using Google Maps API 3. My first. One marker
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
Seemingly simple, but I cannot find anything relevant on the web. What is 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.