chevron-thin-right chevron-thin-left brand cancel-circle search youtube-icon google-plus-icon linkedin-icon facebook-icon twitter-icon toolbox download check linkedin phone twitter-old google-plus facebook profile-male chat calendar profile-male
Welcome to Typemock Community! Here you can ask and receive answers from other community members. If you liked or disliked an answer or thread: react with an up- or downvote.
0 votes

I am using the trial version of typemock and was wondering (I'm a beginner to mocking), how do I mock a database connection?

For example, I need to unit test a system which is part of my web app I am designing. But this system is a newsletter system which works heavily in conjunction with the backend database and some db objects (stored procedure, table/dataset).

How do I mock this? What I need to see is some scenario specific examples to help get me going.

asked by GSS (2.8k points)

5 Answers

0 votes
We are planning to post a video on this subject in the following month.
If you can post the code you need to test here I can be more specific
but in general what you want to do is this:
1. Mock the Connection to the database (
2. Mock the Command.Execute() (The query) and return a fake DataSet object.

You can post a code sample of the part that you need to isolate and we can give you more detailed answer.
answered by ohad (35.5k points)
0 votes

I have this code:

public string LoadData()
////Load some dummy data
////Produce a CSV file with data to load in

string path = "C:/Windows/Data.csv";

StringDictionary stringDict = new StringDictionary();

stringDict.Add("one", "");

string contents = stringDict.ToString();

System.IO.StreamWriter sw = new StreamWriter(path, true);

sw.AutoFlush = true;

return path;

public void ForgottenPassword()
//I need to check if the email fired contains the relevant data (pw).
//so use an if clause and check the string for what it contains (ie an object)


public void DeleteData(string path)


I need to write some data, like to a csv file, and I will load it into my database. But then I have to retrieve the data. Remember, this is a forgotten password test, and to get the forgotten password, I have to work with the database.

I'm completely new to mocking and have studied it a little so I was wondering what the sample code for mocking the connection and db objects would like?

answered by GSS (2.8k points)
0 votes
Sorry for not expressing myself clearly :oops:
In order to give you a better help I need to see the production code not the test code.
In your case it will be the code that handle the data base.
What is method that create the data base? (I did not understand if you are using the csv file only in the test or in the production code.)
What is method that get the data from the data base?
I'll try to guess base on what you posted here.

First you have to decide if you want to do integration test or unit test.
If you are doing integration test you want to create and read from the data base so you will not use mocking.

If you are trying to do unit tests you want to abstract the data base layer and use mocking.
So what have to do is that:
1. Mock the creation of the data base:
2. Mock the connection to the data base.
3. Mock the return value from the query.
4. Mock mail sender. (you don't want to send real mail)
5. check the mail content for the password.
Note that usually you will use the Assert class to check the expected results instead of an if clause.

In order to give you more details I'll need at least the classes names and methods signatures from the production code.

:arrow: You can take a look at this blog post which talks also about data base mocking.
answered by ohad (35.5k points)
0 votes

Thanks for that article.

I have another test mocking case, and it is to check that my URL rewriting system works.

I can distinguish between testfixture and testfixturesetup, but I'm wondering what do I mock? So would I mock every bit of functionality outside of my URL rewriting system? So for example, I need to upload a page programatically, using .Net classes. Do I mock this?

Also, such setup functionality can be in it its own class file and be mocked or be inline. I guess this consideration isn't important though.
answered by GSS (2.8k points)
0 votes
Generally you'll want to isolate every class that the code under test is
dependent on.
You should remember that to over use mocking is not a good idea ...
So use common sense - Make it clear to your self what you intend to test
in every test method that you write than usually you will see the things
you'll need to mock.

Hope it helps. If you need more detailed answer please post the code
you want to test and I'll try my best to help you. 8)
answered by ohad (35.5k points)