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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T00:12:31+00:00 2026-05-18T00:12:31+00:00

I am trying to write some unit tests for my code. In out project

  • 0

I am trying to write some unit tests for my code. In out project we are using the Linq-SQL objects that are created from the DBML. I am trying to figure out how i would need to test the following logic.

Say for example i need to get a count of records from a table using LINQ.

var objectCount = (from x in DB.MyRecords
                  where x.DateAdded.Date == DateTime.Now.Date
                  select x).Count(); //For example this will return 4

if(objectCount > 3)
{
   //Do some logic here
}
else
{
   //Do some logic here
}

Now my understanding is that a unit test is not really a unit test if you access the DB.
My query is also quite more involved than that as it the data structure has Forign keys that need to be maintained.
Now the next problem is that due to the fact that we are using LINQ-SQL object we are not using interfaces therefore we cannot really use a mocking framework (or can we????)
i would like to know what the process is to be able to unit test this.

  • 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-18T00:12:31+00:00Added an answer on May 18, 2026 at 12:12 am

    You need an intermediary classes between your DBML and your business logic code to be able to test. Take a look at Repository pattern for more details about this.

    Let me give you a very simple example, suppose you have a Product table and you want to test whether total number of product in your database is less than 10 products. You need a repository class that supply you the number of product in the database in a form of integer, you don’t care about how the method gets the number as long as you get the number (think of abstraction). Take a look at the following code snippet:

    public interface IRepository
    {
        int CountProduct();
    }
    
    // your implementation of repository against real database
    public class DBLinqToSQLRepository : IRepository
    {
        // some constructor here
    
        public int CountProduct()
        {
            // your code here to return the actual number of product from db.
        }
    }
    
    // your implementation of fake repository that will provide fake data for testing
    public class FakeRepository : IRepository
    {
        private List<Product> products = new List<Product>();
    
        // some initialization here
    
        public int CountProduct()
        {
            return products.Count;
        }
    }
    

    Then in your test environment you can use this FakeRepository instead of the actual database repository to test your business logic.

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

Sidebar

Related Questions

I'm trying to write some LINQ To SQL code that would generate SQL like
I'm trying to write some C# code that calls a method from an unmanaged
I was trying to write some code that would check if an item has
I'm trying to write some SQL that will delete files of type '.7z' that
So, im trying to write some code that utilizes Nvidia's CUDA architecture. I noticed
I have a mobile platform that I am trying to write some communications code
I'm trying to write some C# code to get to a specific folder in
I'm trying to write some code to find a specific XmlNode object based on
I'm trying to write some code to work with an htdigest password file. The
I'm trying to write some PHP to upload a file to a folder on

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.