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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T20:34:51+00:00 2026-05-23T20:34:51+00:00

//class = Person public string Name { get; internal set; } I have an

  • 0
//class = Person
public string Name { get; internal set; }

I have an object with several different fields that are declared as shown above. I would like to use Moq so I can unit test the repository. The repository simply returns a list of names, so I would like to setup Moq to work with it like so:

var personRepositoryMock = new Mock<IPersonRepository>();

personRepositoryMock
        .Setup(p => p.GetNames())
        .Returns(new List<Person>
        {
            new Person{Name = "Hulk Hogan"}
        });

Being new to mocking and unit testing in general, I have a couple of questions:

  1. What are my options to stub out the Person class in my scenario?

  2. What is the benefit of mocking in this situation? I read and read and read, but I can’t seem to get my head around why I see examples like this, testing a repository. Mocks make sense to me when I have to unit test business logic, but not so much in the data layer. Any words of wisdom to clear this up for me?

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-23T20:34:52+00:00Added an answer on May 23, 2026 at 8:34 pm

    1) You can use the “InternalsVisibleTo” attribute on the assembly (in AssemblyInfo.cs) that contains the repository class, to give the Moq assembly access to it.

    [assembly: InternalsVisibleTo("DynamicProxyGenAssembly2")]
    [assembly: InternalsVisibleTo("YourTestClass")]
    

    2) Mocking decouples your data layer from being a dependency requirement for unit tests.

    Think of it this way: you won’t be creating a Mock to test the repository itself. You’re creating the mock for other classes to use as a fake data source, to test their functionality that requires input data from the repository in the real application.

    If you can accurately predict the data cases that your repository will provide, then you can mock those cases as fake input, and not require that your unit tests actually connect to the database to get live data to use.

    Your example won’t actually unit test anything in the repository (other than the parameterless constructor!) so … I don’t know where you’re seeing these examples, but I don’t feel that the code above is providing anything useful “as is”.

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

Sidebar

Related Questions

Lets say I have an object class Person { public string Name { get;
I have the following Model: public class Person { public string Name {get;set;} public
I have a model public class Person { public string Name { get; set;
Suppose i have class Person { public int Id {get;set;} public string Name {get;set;}
I several objects of class: class Person { public string Name { get; set;
I have the following classes: class Person { public string Name { get; set;
I have the following data structure: public class Person { public string Name{get;set;} public
Given the classes: public class Person { public string Name { get; set; }
I have a custom class: public class Person { public String Name { get;
I have the following class: public class Person { public String Name { get;

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.