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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T21:05:22+00:00 2026-05-20T21:05:22+00:00

Here is my IProductRepository: public interface IProductsRepository { IQueryable<Product> Products { get; } }

  • 0

Here is my IProductRepository:

public interface IProductsRepository
{
    IQueryable<Product> Products { get; }
}

And here is where I’m using Moq:

public static IProductsRepository MockProductsRepository(params Product[] prods)
{
    var mockProductRepos = new Mock<IProductsRepository>();
    mockProductRepos.Setup(x => x.Products).Returns(prods.AsQueryable());
    return mockProductRepos.Object;
}

And here is how I use this MockProductsRepository method:

[Test]
public void Product_Lists_Include_Correct_Page_Numbers()
{
    //Arrange: If there are five products in the repository...
    var mockRepository = UnitTestHelpers.MockProductsRepository(
        new Product { Name = "P1" }, new Product { Name = "P2" },
        new Product { Name = "P3" }, new Product { Name = "P4" },
        new Product { Name = "P5" }
    );

    var controller = new ProductsController(mockRepository) { PageSize = 3 };

    //yada yada yada...
}

What exactly is Moq doing? I’m following the book Pro ASP.Net MVC2 and it’s teaching all sorts of new wizardry to me, and before I continue reading, I’d like to understand what’s going on – and right now I just know, “it works” nothing more. 🙂

Thank you for your time.

  • 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-20T21:05:23+00:00Added an answer on May 20, 2026 at 9:05 pm

    Moq is generating the IL in memory for a class that implements IProductsRepository, with all its properties/methods calling back to Moq to see what it should do. The Setup is configuring the Products property to always return the supplied list of products (as an IQueryable) when it is called. It’s then returning the generated object as a IProductsRepository which can then be passed to ProductsController which is none the wiser about its dynamic nature.

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

Sidebar

Related Questions

Here is my controller code and also my View: @using (Html.BeginForm(Create, Product, FormMethod.Post, new
I have a list of products: IQueryable<Product> list = _ProductRepository.GetAll(); The column Product.Clicks corresponds
Here are the simple two lines of code: public static void RemoveCoverImageForProduct(int productId) {
Here my problem: @Assert\Regex( * pattern=/^[A-Za-z0-9][A-Za-z0-9\]*$/, * groups={creation, creation_logged} * ) I'm using the
Here's the setup - I have a view that lists products. On that same
I have an an ICollection<Product> : var products = productRepository.FindAll(); Product has a property
Here's my test function (c#, visual studio 2010): [TestMethod()] public void TestGetRelevantWeeks() { List<sbyte>
For some reason, I cannot get an action to be hit. I am using
I have methods like this: public void AddOrUpdate(Product product) { try { _productRepository.AddOrUpdate(product); }
Here is my situation: I am using telerik with winform. I have a dataset

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.