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.
-
-
And don't forget to let us know what solution you went with in the end, as well as how well it went.
Herbie -
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.
-
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 -
Ooohh... Isolator looks like it would solve all my problems.
But I don't think anyone will let me buy it. I'm sure they'd much prefer I spend several days re-engineering a slightly different solution at great expense to them. Such is life, good thing my time is charged by the hour. (Not that I get paid that way... But never mind.) -
Ah, in that case RhinoMocks is also worth a look over NMocks.
-
In the end the ridiculous number of other things that needed to be set up in order to make the system testable. (Many of which appear to have nothing whatsoever to do with what I'm trying to test) means that the "slight inconvenience" of distributing a database to each tester is hardly worth avoiding.
Seriously...
This is not the neatest system to be tested I've seen. Why I need to install several web services, register a bunch of arbitrary dlls (taken from one of the development servers no less... run! run for the hills! In fareness they could be built from source, but that would involve installing VB6! AAarrgh!), install message queueing and do half a dozen other things, to test what should be a web page which makes calls to a service via SOAP, is beyond me.
I'm assuming that actually none of these things are relevant, but that one of them gets it all working. The rest I'm putting down to IT superstition.
It's astonishing that this service actually works at all... But there you go, next time you visit "a major media download site", spare some thought for the poor folks staring incredulously at the mish-mash of code which keeps it functioning.
[edit: as a side note, nmocks is already at least distributed to all the developers, although there's no evidence of it being used anywhere.] -
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,
Thread Closed
This thread is kinda stale and has been closed but if you'd like to continue the conversation, please create a new thread in our Forums,
or Contact Us and let us know.