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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T09:23:44+00:00 2026-05-18T09:23:44+00:00

I’m using ASP.NET MVC 2, NUnit, Moq and MVC Contrib. I have written my

  • 0

I’m using ASP.NET MVC 2, NUnit, Moq and MVC Contrib. I have written my first unit test ever, and I have a couple of questions regarding this test. My scenario is that I have an Index view. On this view I have a grid that displays all the news items.

Here is my INewsRepository class:

public interface INewsRepository
{
   IEnumerable<News> FindAll();
}

My test class with the test method:

public class NewsControllerTest :TestControllerBuilder
{
   private Mock<INewsRepository> mockNewsRepository;
   private NewsController newsController;

   [SetUp]
   public void Init()
   {
      mockNewsRepository = new Mock<INewsRepository>();
      newsController = new NewsController(mockNewsRepository.Object);
      InitializeController(newsController);
   }

   [Test]
   public void NewsController_Index()
   {
      // Arrange
      var news = new Mock<IEnumerable<News>>();
      mockNewsRepository.Setup(r => r.FindAll()).Returns(news.Object).Verifiable();

      // Act
      ActionResult actual = newsController.Index();

      // Assert
      mockNewsRepository.Verify();
      actual
         .AssertViewRendered()
         .ForView("Index")
         .WithViewData<News[]>()
         .ShouldBe(news);
   }
}

My view:

public ActionResult Index()
{
   FakeNewsRepository fakeNewsRepository = new FakeNewsRepository();
   IEnumerable<News> news = fakeNewsRepository.FindAll();
   return View(news);
}

I need some pointers on the way that I did it. Am I in the correct direction? What should I add, what should I leave out? I want to do unit testing, am I mixing it with integration testing? Any extra advice would be appreciated.

When I run this test in the NUnit GUI console then I get an error back and I’mnot sure what it means:

MyProject.Web.Tests.Controllers.NewsControllerTest.NewsController_Index:
Moq.MockVerificationException : The following setups were not matched:
INewsRepository r => r.FindAll()
  • 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-18T09:23:44+00:00Added an answer on May 18, 2026 at 9:23 am
    public ActionResult Index()
    {
        FakeNewsRepository fakeNewsRepository = new FakeNewsRepository();
        IEnumerable<News> news = fakeNewsRepository.FindAll();
        return View(news);
    }
    

    You cannot mock the repository that is hardcoded like this in your action. You are instantiating it inside the action, you will never be able to unit test this and mock the repository. The repository needs to be injected as a dependency. You could use an interface and pass this interface to the constructor of your controller:

    public class HomeController: Controller
    {
        private readonly IRepository _repository;
        public class HomeController(IRepository repository)
        {
            _repository = repository;
        }
    
        public ActionResult Index()
        {
            IEnumerable<News> news = _repository.FindAll();
            return View(news);
        }
    }
    

    Now in your unit test you could pass the mocked instance of your repository to the controller constructor and define expectations.

    Also notice that MVCContrib.TestHelper is designed to work with Rhino Mocks. I am not quite sure whether it works fine with Moq.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
We're building an app, our first using Rails 3, and we're having to build
I have an MVC Razor view @{ ViewBag.Title = Index; var c = (char)146;
I have thousands of HTML files to process using Groovy/Java and I need to
I'm making a simple page using Google Maps API 3. My first. One marker
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
That's pretty much it. I'm using Nokogiri to scrape a web page what has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and

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.