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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T19:32:03+00:00 2026-06-10T19:32:03+00:00

I want to write unit test for a class that contains linq to sql

  • 0

I want to write unit test for a class that contains linq to sql codes . I mean inside each method I have created a new DbContext and done database jobs .

I searched the web . first I came to use repository and Unit of Work patterns but I figured out that DbContext itself is a unit of work and its dbset works as repositories . another point is that I think there is no need to test Linq part because it works as it should ( tested by .net team ) . I want to test the logic I have added to the code . so I decided to create an interface with necessary methods with two implementations , one uses linqToSql while another is just a mock . something like this :

    public interface IDbManager
    {
        bool Insert(MyEntity newEntity);
    }

    public class RealDbManager:IDbManager
    {
        public bool Insert(MyEntity newEntity)
        {
            using (DbDataContext db = new DbDataContext())
            {
                db.MyEntities.InsertOnSubmit(newEntity);
                db.SubmitChanges();
            }
        }
    }

    public class MockDbManager:IDbManager
    {
        public bool Insert(MyEntity newEntity)
        {
            return true;
        }
    }

is the whole idea correct ? if so is this a correct implementation ?

is it possible to define DbDataContext as a class variable instead of creating new instance inside each method ?

  • 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-06-10T19:32:04+00:00Added an answer on June 10, 2026 at 7:32 pm

    You have the right general idea for a start. Your Mock Insert method should save the entity to some in-memory store so that subsequent queries will return the inserted information, as would be expected. But the very basic idea of having an interface, with a ‘real’ and a ‘mock’ implementation is there.

    Remember that when using your Mock in tests, you are testing your other code that uses the mock – not the mock itself.

    As for defining the DataContext as a member variable; you could use an IDisposable pattern for it, like so:

    public class RealDbManager:IDbManager, IDisposable
    {
        DbDataContext db = new DbDataContext();
        public bool Insert(MyEntity newEntity)
        {
            {
                db.MyEntities.InsertOnSubmit(newEntity);
                db.SubmitChanges();
            }
        }
        public void Dispose()
        {
            db.Dispose();
        }
    }
    

    You would just have to be sure to dispose of your DbManager, then.

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

Sidebar

Related Questions

I want to write a unit test for a Django manage.py command that does
I want to write a unit test which tests the function of a class
I have a static class representing a connection pool that I want to unit
I have some Web API methods that I want to write unit tests for.
I have a pseudo random number generator (PRNG) class that I want to unit
I want to write a unit test that basically verifies an object I add
What I want to do I'm trying to write unit test to check that
I'm trying to write a unit test that needs to confirm if a method
Assume I want to write a unit test to test a particular piece of
I have a class method that looks like this: private List<string> DataStoreContents = new

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.