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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T10:24:06+00:00 2026-05-15T10:24:06+00:00

I’m currently testing the controller in my mvc app and I’m creating a fake

  • 0

I’m currently testing the controller in my mvc app and I’m creating a fake repository for testing. However I seem to be writing more code and spending more time for the fakes than I do on the actual repositories. Is this right?

The code I have is as follows:

Controller

public partial class SomeController : Controller
{
    IRepository repository;

    public SomeController(IRepository rep)
    {
        repository = rep;
    }

    public virtaul ActionResult Index()
    {
        // Some logic
        var model = repository.GetSomething();

        return View(model);
    }
}

IRepository

public interface IRepository
{
    Something GetSomething();
}

Fake Repository

public class FakeRepository : IRepository
{
    private List<Something> somethingList;

    public FakeRepository(List<Something> somethings)
    {
        somthingList = somthings;
    }

    public Something GetSomething()
    {
        return somethingList;
    }
}

Fake Data

class FakeSomethingData
{
    public static List<Something> CreateSomethingData()
    {
        var somethings = new List<Something>();

        for (int i = 0; i < 100; i++)
        {
            somethings.Add(new Something
            {
                value1 = String.Format("value{0}", i),
                value2 = String.Format("value{0}", i),
                value3 = String.Format("value{0}", i)
            });
        }

        return somethings;
    }
}

Actual Test

[TestClass]
public class SomethingControllerTest
{
    SomethingController CreateSomethingController()
    {
        var testData = FakeSomethingData.CreateSomethingData();
        var repository = new FakeSomethingRepository(testData);

        SomethingController controller = new SomethingController(repository);

        return controller;
    }

    [TestMethod]
    public void SomeTest()
    {
        // Arrange
        var controller = CreateSomethingController();

        // Act
        // Some test here

        // Arrange
    }
}

All this seems to be a lot of extra code, especially as I have more than one repository. Is there a more efficient way of doing this? Maybe using mocks?

Thanks

  • 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-15T10:24:06+00:00Added an answer on May 15, 2026 at 10:24 am

    As CD proposed, use a mocking framework. I too use Moq, and with Moq your test code could be refactored to something like this:

    // Arrange
    var repoMock = new Mock<IRepository>();
    repoMock.Setup(r => r.GetSomething()).Returns(TestData.SomeThings);
    var controller = new SomethingController(repoMock.Object);
    
    // Act
    controller.DoStuff();
    
    // Assert
    ...
    

    I usually find it convenient to put all my test data in a separate TestData class with static properties for everything – that way I know that I test with the same data in each test. This is what you need in TestData for this example:

    public static List<Something> SomeThings
    { 
        get
        {     
            var somethings = new List<Something>();
    
            for (int i = 0; i < 100; i++)
            {
                somethings.Add(new Something
                {
                    value1 = String.Format("value{0}", i),
                    value2 = String.Format("value{0}", i),
                    value3 = String.Format("value{0}", i)
                });
            }
    
            return somethings;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

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.