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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T08:40:34+00:00 2026-06-09T08:40:34+00:00

since i do not get my head around the unit tests. I have a

  • 0

since i do not get my head around the unit tests.

I have a service

public interface IMyService
{
    public List<Person> GetRandomPeople();
}

And then in my MVC Project I have the implementation of this service

public MyService : IMyService
{
    public List<Person> GetRandomPeople();
    {
         ...the actual logic the get the People here. This is what i want to test ?
    }
}

Then in my controller

public HomeController : Controller
{
     IMyService _myService;
     public HomeController(IMyService myService)
     {
         _myService = myService
     }
}

Then in the Action Method I will use

public ActionResult CreateRandom()
{
    List<Person> people = _myService.GetRandomPeople();
    return View(people)
}

Note : The person repo is in my service, just typed this out quickly, so i have a repo.

My Question : How would i test my Service implementation in my Tests projects. I am really stuck now, and i think this is going to the “light goes on” moment in my TDD future.

thanks in advance.

  • 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-06-09T08:40:36+00:00Added an answer on June 9, 2026 at 8:40 am

    The point of injecting the service interface into the controller is to allow you to test the controller in isolation. To test that, you pass in a mock or fake IMyService.

    Depending on how your service is implemented, you may need to test your service with integration tests. Those should be separate from your unit tests (as you don’t want to run them continuously).

    For example, let’s say IMyService is implemented with Entity Framework. You need to actually run the LINQ to Entities against a database to test it. You could use a local database, you could have EF create and populate a database on the fly, etc. Those aren’t unit tests (they use I/O), but they’re still important.

    Other persistence frameworks may permit you to test against in-memory data sets. That’s fine; I’d still consider that an integration test (your code + the framework) and would separate it from your unit tests.

    The trick is to keep the business logic out of the service implementation. Restrict that (as much as possible) to pure data-access code. You want to test your business logic with unit tests.

    EDIT:

    To address the question in the comment (“when you need to create stubs”):

    You create stubs, fakes, test doubles, or mocks (there’s a lot of terminology) when you have a class that you want to test in isolation (the system under test, or SUT) and that class has injected dependencies.

    Those dependencies are going to be abstract in some way – interfaces, abstract classes, classes with virtual methods, delegates, etc.

    Your production code will pass concrete implementations in (classes that hit the database, for instance). Your test code will pass test implementations in.

    You could pass a simple stub implementation (just write a dummy class in your test project that implements the interface and returns fixed values for its members), or you could have a fancier implementation that will detect what calls are made and what arguments are passed (a mock object). You can write all of this by hand. It gets tedious, so many testers use mock object frameworks (also known as isolation frameworks). There are many of these (often several for any given language); I tend to use either NSubstitute or Moq in .NET.

    Learning to write tests takes time. It helped me a lot to read several books on the subject. Blog posts may not give enough background. There are some excellent suggestions in the (sadly closed) question here.

    The short answer is that you create stubs or mocks when your tests require them.

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

Sidebar

Related Questions

I have been scratching my head since long now but not getting around to
Question As stated, have you any tips to help grok / understand / get-your-head-around
I'm learning C++ and can't get my head around this problem: I have a
I have a problem to get my head around smoothing and sampling contours in
Since PEAR attempts to get ConsoleTools from components.ez.no, which is not available anymore, how
I am not sure my title is correct or not since I have no
Since JavaScript is not derived from Java, why does it have Java in the
Since I'm not an Php programmer I have to ask you, I have to
Since CCKeyDerivationPBKDF is not available until after iOS 5.0, people have suggested using the
I've just been messing around with threads in Java to get my head around

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.