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.