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

  • Home
  • SEARCH
  • 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 3496772
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T12:17:24+00:00 2026-05-18T12:17:24+00:00

I am using .NET 4, NUnit and Rhino mocks. I want to unit test

  • 0

I am using .NET 4, NUnit and Rhino mocks. I want to unit test my news repository, but I am not sure of how to go about it. My news repository is what I will eventually be using to communicate to the database. I want to use it to test against fake/dummy data. Not sure if it is possible?? This is what I currently have:

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

public class NewsRepository : INewsRepository
{
   private readonly INewsRepository newsRepository;

   public NewsRepository(INewsRepository newsRepository)
   {
      this.newsRepository = newsRepository;
   }

   public IEnumerable<News> FindAll()
   {
      return null;
   }
}

My unit test looks like this:

public class NewsRepositoryTest
{
   private INewsRepository newsRepository;

   [SetUp]
   public void Init()
   {
      newsRepository = MockRepository.GenerateMock<NewsRepository>();
   }

   [Test]
   public void FindAll_should_return_correct_news()
   {
      // Arrange
      List<News> newsList = new List<News>();
      newsList.Add(new News { Id = 1, Title = "Test Title 1" });
      newsList.Add(new News { Id = 2, Title = "Test Title 2" });

      newsRepository.Stub(r => r.FindAll()).Return(newsList);

      // Act
      var actual = newsRepository.FindAll();

      // Assert
      Assert.AreEqual(2, actual.Count());
   }
}

In the above code I am not sure what I need to mock. The code above compiles but fails in the NUnit GUI about a contructor value. I can only assume it has to do with the INewsRepository paramter that I need to supply to NewsRepository. I don’t know how to do this in the test. Can someone please rectify my unit test so that it will pass in the NUnit GUI? Can someone also provide some feedback on if I am implementing my repositories correctly?

Being a newbie to mocking, is there anything that I need to verify? When would I need to verify? What is its purpose? I have been working through a couple of source code projects and some use verify and some don’t.

If the above test passes, what does this prove to me as developer? What does another developer have to do to my repository to make it fail in the NUnit GUI?

Sorry for all the questions, but they are newbie questions 🙂

I hope soomeone can help me out.

  • 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-18T12:17:24+00:00Added an answer on May 18, 2026 at 12:17 pm

    As Steven has said, you’re Asserting against the Mock NewsRepository in the above code.

    The idea of mocking is to isolate the Code Under Test and to create fakes to replace their dependencies.

    You use the Mock NewsRepository to test something that uses INewsRepository, in your case, you mention NewsService; NewsService will use your mock of INewsRepository.

    If you search your solution for anything that uses INewsRepository.FindAll(), you will create a Mock Repository to test that code in isolation.

    If you want to test something that calls your Service layer, you will need to mock NewsService.

    Also, as Steven as said, there is no need for the NewsRepository to have a copy of itself injected by IoC, so:

    public class NewsRepository : INewsRepository
    {
       private readonly INewsRepository newsRepository;
    
       public NewsRepository(INewsRepository newsRepository)
       {
          this.newsRepository = newsRepository;
       }
    
       public IEnumerable<News> FindAll()
       {
          return null;
       }
    }
    

    should become:

    public class NewsRepository : INewsRepository
    {
       public IEnumerable<News> FindAll()
       {
          return null;
       }
    }
    

    Once you have functionality in your FindAll() method that needs testing, you can mock the objects that they use.

    As a point of style from the great Art Of Unit Testing initialisation of mock objects is best left out of the Setup method and carried out in a helper method called at the start of the method. Since the call to Setup will be invisible and makes the initalisation of the mock unclear.

    As another point of style, from that book, a suggested unit test naming convention is: “MethodUnderTest_Scenario_ExpectedBehavior“.
    So,

    FindAll_should_return_correct_news
    could become, for example:
    FindAll_AfterAddingTwoNewsItems_ReturnsACollectionWithCountOf2

    I hope this makes the approach clearer.

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

Sidebar

Related Questions

I want to test ASP.NET application using NUnit, but it seems WebConfigurationManager.ConnectionStrings collection is
Using NUnit 2.2 on .NET 3.5, the following test fails when using DateTime.Equals. Why?
I'm using WatiN, NUnit and ReSharper to run my ASP.NET unit tests inside Visual
I'm trying to implement the GetHttpContext function from HtmlHelperTest.cs in VB.NET using Rhino Mocks,
I am doing regression testing using NUnit, WatiN and VB.net. What i am doing
I am testing the UI of my ASP.Net Web Forms app using NUnit/Watin. I
A group of us (.NET developers) are talking unit testing. Not any one framework
I'm using CruiseControl.NET with NUnit and NAnt. I'm trying to get CC.net to send
I'm using NUnit 2.5 and NAnt 0.85 to compile a .NET 3.5 library. Because
Using .Net (C#), how can you work with USB devices? How can you detect

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.