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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T16:27:24+00:00 2026-05-25T16:27:24+00:00

I am using trying to get my head round unit testing with DI/Mocks (Ninject/Moq)

  • 0

I am using trying to get my head round unit testing with DI/Mocks (Ninject/Moq) to inject a Product repository into my controller which I then pass to my view model in order to allow testing of that.

It is working great and is allowing me to unit test the controller action and the view model. However, when I run the application I get “No parameterless constructor defined for this object”….now I know this is due to the controller trying to initialise the View Model which doesn’t have a parameterless constructor.

I could create the constructor and call my concrete repository from that (so unit tests still use the injected/mocked one).

Is this the correct approach? Any advice would be greatly appreciated!

Controller:

public class ProductsController : Controller
{
    private readonly IProductRepository productRepository;

    public ProductsController(IProductRepository productRepository)
    {
        this.productRepository = productRepository;
    }

    //
    // GET: /Products/
    public ActionResult Index(ViewModels.ProductsIndex vm)
    {
        return View(vm);
    }

}

View model:

public class ProductsIndex
{
    public List<CTEntities.Product> ProductList { get; set; }

    public ProductsIndex(IProductRepository prods)
    {
        ProductList = prods.List().ToList();
    }

    //Adding this constructor would fix my issue but is there a cleaner way?
    public ProductsIndex()
    {
        var prod = new CTDAL.Product();
        ProductList = prod.List().ToList();
    }
}
  • 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-25T16:27:25+00:00Added an answer on May 25, 2026 at 4:27 pm

    Your View Model should be a DTO (Data Transfer Object) … in that it should only contain properties, and it should NOT be responsible for getting data. Your controller should get the data for the View Model, like so:

    New View Model:

    public class ProductsIndex
    {
        public List<CTEntities.Product> ProductList { get; set; }
    }
    

    New Controller:

    public class ProductsController : Controller
    {
        private readonly IProductRepository productRepository;
    
        public ProductsController(IProductRepository productRepository)
        {
            this.productRepository = productRepository;
        }
    
        //
        // GET: /Products/
        public ActionResult Index()
        {
            var products = productRepository.List().ToList();
            return View(products);
        }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to get my head around using Nunit, Ninject, MVC2 and the ADO.Net
I'm still trying to get my head around using Java's generics. I have no
I'm trying to get my head around nested associations in Rails using ActiveResource. My
I'm trying to get my head around mutable vs immutable objects. Using mutable objects
Just trying to get my head round the responsibilities of the service layer and
I'm trying to get my head around using QuartzCore to render semi-complex text/gradient/image UITableViewCell
I'm diving into iPhone development and I'm trying to get my head around gestures
Trying to get my head around backbone.js. This example is using Backbone Boilerplate and
Just started using log4net and trying to get my head around the config and
I'm trying to get my head round this mind boggling stuff they call Database

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.