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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T19:22:37+00:00 2026-05-12T19:22:37+00:00

Lets say I have a Customers table and an Orders table with a one-to-many

  • 0

Lets say I have a Customers table and an Orders table with a one-to-many association (one customer can have multiple orders). If I have some code that I wish to unit test that accesses a specific customer’s orders via lazy-loading (e.g. a call to customer.Orders), how do I mock / stub that call out so that it doesn’t hit the database?

Edit:

To be more clear, let’s use a more concrete example. Let’s say I want to return all the orders for a particular customer. I could write it like so using the auto-generated lazy-loading properties Linq 2 Sql provides:

Customer customer = customerRepository.GetCustomerById(customerId);

return customer.Orders;

However, unit testing this is a bit tough. I can mock out the call to GetCustomerById, but I can’t (as far as I can tell) mock out the call to Orders. The only way I can think of to unit test this would be to either a) connect to a database (which would slow down my tests and be fragile) or b) don’t use lazy-load properties.

Not using lazy-load properties, I would probably rewrite the above as this:

return orderRepository.GetOrdersByCustomerId(customerId);

This definitely works, but it feels awkward to completely ignore lazy-load properties simply for unit-testing.

  • 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-12T19:22:37+00:00Added an answer on May 12, 2026 at 7:22 pm

    As an overall answer to your question, you stub out that call just as you stub out anything else.

    I will assume that the code you want to unit test is a consumer of your data access component, as this is the most common scenario. You can only stub out you data access if you program against an interface. This means that you must hide the implementation details of L2S behind an interface such that the consuming code has no idea about which implementation it currently consumes.

    A corrolary to this is that lazy-loading is an implementation detail you you need not worry about when unit testing, because the unit test should not be using L2S at all.

    When stubbing out the data access layer, a call to customers.Orders would typically be a call to an in-memory property of the Stub.

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

Sidebar

Related Questions

Lets say I have one table called REVIEWS This table has Reviews that customers
Lets say that I have three tables, customers, orders and orderDetails. I'm doing this:
Lets say I have a Customer table and a Transaction Table, and I say
I have this GUI that shows, let's say Customer Orders. When my client nailed
Lets say I have a table CUSTOMERS with a key of CUSTOMER_ID. I would
So let's say that I have a database for managing customers. A customer has
For example, let's say that I have this 3 tables : Table Customers: CustomerID
can anyone generate a query for me. Lets say i have a table sales(saleID,
Let's say I have a Customer table which has a PrimaryContactId field and a
Lets say I have this code: <?php class hello { var $greeting = hello;

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.