Kapol
Kapol

Reputation: 6463

How to test a method that queries a database without actually performing the query?

I am trying to write a unit test that will perform a check against a string returned from a method. That string is a part of an entity, which should be generated by a call to the database. I can easily stub such entity, but I don't know how to omit the call to the database, since it is a part of the method.

The method looks like this:

private string GetDescriptionForRelationEntry(string relAttrId, string client)
        {
            // here we are querying the database
            var relationEntities = new EntityDatabaseQuery<AttributeEntryEntity>();

            // the rest of the method
        }

The part specified as the rest of the method needs to be executed, because I have to obtain the string result to perform necessary checks. So, basically, I need to "fake" only one line of code.

Right now I'm not able to say how extensive modifications on the SUT class are allowed, thus I'm not limiting this problem to any specific solution. Any help will be appreciated.

Upvotes: 1

Views: 314

Answers (4)

samy
samy

Reputation: 14962

I'd advise you to take a look at a solution that lets you wrap a database in memory. For example there is the Effort library.

Effort is a powerful tool that enables a convenient way to create automated tests for Entity Framework based applications. It is basically an ADO.NET provider that executes all the data operations on a lightweight in-process main memory database instead of a traditional external database. It provides some intuitive helper methods too that make really easy to use this provider with existing ObjectContext or DbContext classes. A simple addition to existing code might be enough to create data driven tests that can run without the presence of the external database.

I explained how I use it in this answer: it was for Entity Framework but I think the same behavior can be used in your case.

Upvotes: 1

Kris Vandermotten
Kris Vandermotten

Reputation: 10201

Most mocking frameworks require you to change the architecture of your solution, creating and implementing interfaces, and loading implementations dynamically.

However, you can also use Microsoft Fakes, which requires no changes to your application. Fakes come with Visual Studio, so you don't need to download or install anything.

For more information, see http://msdn.microsoft.com/en-us/library/hh549175.aspx

Microsoft Fakes help you isolate the code you are testing by replacing other parts of the application with stubs or shims. These are small pieces of code that are under the control of your tests.

Upvotes: 0

Akila De Alwis
Akila De Alwis

Reputation: 41

You can simply create a mock method to represent your DB result Use this link for more details : http://msdn.microsoft.com/en-us/library/ff650441.aspx

Upvotes: 2

Mat&#237;as Fidemraizer
Mat&#237;as Fidemraizer

Reputation: 64933

Usually a testable class would be an implementation of some interface or a derived class of some abstract class.

Thus, you can use an inversion of control container framework like Castle Windsor, Ninject or any that might you love more, and provide a fake implementation of the whole interface which won't hit the database, but it'll return a test query result.

Upvotes: 2

Related Questions