Posted By: Massif | Mar 10th, 2008 @ 8:40 AM
page 1 of 1
Comments: 7 | Views: 1363
Massif
Massif
aim stupidly high, expect to fail often.
So I have an already developed set of code which I want to test.

At the moment the code's going all the way down to a database to get some details that I'd like to be able to insert by hand. Initially I thought "ah ha! I'll try mocking" (never having tried it before) but NMock won't let me, as I'm not trying to implement an interface in a dummy version. There's already a class in existence, so nmock says I can't play. (Which I should have realized earlier, but I didn't have time to read all the documentation before having a stab at getting it going.)

I know that I just need to set a few properties, and block a call to a function I don't need, is there any way to mock a single function, or replace a function at test time with a crippled version?

The alternative, deploying a test database onto every machine running tests, would be extremely tedious.
Dr Herbie
Dr Herbie
Horses for courses
And don't forget to let us know what solution you went with in the end, as well as how well it went.

Herbie
Dr Herbie
Dr Herbie
Horses for courses
Massif wrote:
So I have an already developed set of code which I want to test.

At the moment the code's going all the way down to a database to get some details that I'd like to be able to insert by hand. Initially I thought "ah ha! I'll try mocking" (never having tried it before) but NMock won't let me, as I'm not trying to implement an interface in a dummy version. There's already a class in existence, so nmock says I can't play. (Which I should have realized earlier, but I didn't have time to read all the documentation before having a stab at getting it going.)

I know that I just need to set a few properties, and block a call to a function I don't need, is there any way to mock a single function, or replace a function at test time with a crippled version?

The alternative, deploying a test database onto every machine running tests, would be extremely tedious.


Have you tried extracting the interface from the existing class (using the refactoring tool in VS), replacing references to the class with references to the interface and then mocking the interface?


All you have to do then is provide a method to overide the default (runtime) class with an instance of a mocked class that bypasses the database entirely.

E.g.

class MyClassToBeTested
{
   IDataReadingInterface myInterface;

   public MyClassToBeTested()
   {
      // Use default type if non specified (used in runtime)
       myInterface = new RuntimeDAtaReadinClas();
   }

   public MyClassToBeTested(IDataReadingInterface interfaceToUse)
   {
      // Use specified interface (used in tests)
      myInterface = interfaceToUse;
   }
}


or even

public bool MyMethod(int parameter1, int parameter2)
{
   iDataReadingInterface interface = new RuntimeDataReadingClass();

   ...
}

public bool MyMethod(int parameter1, int parameter2, IDataReadingInterface interfaceToUseInThisMethod)
{
   IDataReadingInterface interface = interfaceToUseInThisMethod;
   ...
}

By providing overrides to methods\constructors code that uses the target of testing doesn't have to be changed.


Herbie
You get the idea.

PerfectPhase
PerfectPhase
"This is not war, this is pest control!" - Dalek to Cyberman
Massif wrote:
So I have an already developed set of code which I want to test.

At the moment the code's going all the way down to a database to get some details that I'd like to be able to insert by hand. Initially I thought "ah ha! I'll try mocking" (never having tried it before) but NMock won't let me, as I'm not trying to implement an interface in a dummy version. There's already a class in existence, so nmock says I can't play. (Which I should have realized earlier, but I didn't have time to read all the documentation before having a stab at getting it going.)

I know that I just need to set a few properties, and block a call to a function I don't need, is there any way to mock a single function, or replace a function at test time with a crippled version?

The alternative, deploying a test database onto every machine running tests, would be extremely tedious.


You tried TypeMock Isolator?

How does it work?

Typemock Isolator uses an aspect-oriented programming design that creates a mock aspect. Internally, it uses the .NET Framework profiler API to monitor an application's execution. When a method is loaded by the CLR, Typemock Isolator retrieves the IL and replaces it with instrumented IL code. Typemock Isolator does not change your original IL code, it simply inserts new code that calls the Typemock Isolator framework and returns mocked values.

http://www.typemock.com/Docs/Mock%20Types.html

PerfectPhase
PerfectPhase
"This is not war, this is pest control!" - Dalek to Cyberman
Ah, in that case RhinoMocks is also worth a look over NMocks.
Hey Can anyone tell me if there is a way to mock an explicitly defined function of an interface using TypeMock? I am really stuck up with it and just cant find the solution.

Regards,
page 1 of 1
Comments: 7 | Views: 1363
Microsoft Communities