How to test a method

C

CSharper

I am trying to test a method, which in turn calls another method.
Which is something like the folloing

public string mainmethod()
{
string val = submethod();
... do more ...
}

I am using NUnit and I do not want the submethod execute to get the
'val' I want some way to fake the value that is coming from that
method and continue with the rest of mainmethod process. Is there a
way to do it in NUnit?

The submethod has some expensive process that is running that I don't
want to initialise or execute.

Thanks,
 
M

Mr. Arnold

CSharper said:
I am trying to test a method, which in turn calls another method.
Which is something like the folloing

public string mainmethod()
{
string val = submethod();
... do more ...
}

I am using NUnit and I do not want the submethod execute to get the
'val' I want some way to fake the value that is coming from that
method and continue with the rest of mainmethod process. Is there a
way to do it in NUnit?

The submethod has some expensive process that is running that I don't
want to initialise or execute.



http://madcoderspeak.blogspot.com/2009/01/nunit-rowtest-extension-running-same.html

You can use a Row attribute, mock-up the data and pass it to your submethod.
You don't run your submethod.

[Test]
[row "help", 1]
public string mainmethod(string str, long id)
{
string msg = str;
long thisid = id;
}

That's what I do with the MbUnit test framework. It looks like you can do
the same with Nunit.

I have even mocked-up a business object like Payroll.cs and sent it into a
method with a [row].








__________ Information from ESET NOD32 Antivirus, version of virus signature database 4073 (20090513) __________

The message was checked by ESET NOD32 Antivirus.

http://www.eset.com
 
M

Mr. Arnold

CSharper said:
I am trying to test a method, which in turn calls another method.
Which is something like the folloing

public string mainmethod()
{
string val = submethod();
... do more ...
}

I am using NUnit and I do not want the submethod execute to get the
'val' I want some way to fake the value that is coming from that
method and continue with the rest of mainmethod process. Is there a
way to do it in NUnit?

The submethod has some expensive process that is running that I don't
want to initialise or execute.



http://madcoderspeak.blogspot.com/2009/01/nunit-rowtest-extension-running-same.html

You can use a Row attribute, mock-up the data and pass it to your submethod.
You don't run your submethod.

[Test]
[row "help", 1]
public string mainmethod(string str, long id)
{
string msg = str;
long thisid = id;
}

That's what I do with the MbUnit test framework. It looks like you can do
the same with Nunit.

I have even mocked-up a business object like Payroll.cs and sent it into a
method with a [row].








__________ Information from ESET NOD32 Antivirus, version of virus signature database 4073 (20090513) __________

The message was checked by ESET NOD32 Antivirus.

http://www.eset.com
 
J

jehugaleahsa

I am trying to test a method, which in turn calls another method.
Which is something like the folloing

public string mainmethod()
{
     string val = submethod();
     ... do more ...

}

I am using NUnit and I do not want the submethod execute to get the
'val' I want some way to fake the value that is coming from that
method and continue with the rest of mainmethod process. Is there a
way to do it in NUnit?

The submethod has some expensive process that is running that I don't
want to initialise or execute.

Thanks,

Here is how I would go about doing this. There is probably a good
reason why submethod shouldn't get executed in test. Think about
moving this method into another class. If you are hitting a database
or whatever, separating the logic from persistence is just good
practice. Once you have your new class, create an interface for it.
"Okay", you're saying. Your simple method call just became another
class and _now_ its also got an interface.

It gets worse. Now, you have to create a stub that implements your
interface. You must pass this stub into the class your testing. This
means you may need to change your class to take the submethod
interface as a ctor argument or as an argument to mainmethod.

interface submethod_interface { string submethod(); }
class submethod_production : submethod_interface { public string
submethod() { return "Production"; } }
class submethod_test : submethod_interface { public string submethod()
{ return "Test"; }}

[TestMethod]
public void TestMainMethod()
{
submethod_test stub = new submethod_test();
MyClass myClass = new MyClass(stub);
myClass.mainmethod();
}

Now the implementation for MyClass should be obvious.

class MyClass
{
submethod_interface _smi;
MyClass(submethod_interface smi) {_smi = smi;}
void mainmethod() { string value = _smi.submethod(); }
}

This whole process is called dependency injection. It makes large-
scale testing possible. It also has a wonderful habit of being a good
heuristic for design. By forcing your code to pass dependencies, you
get good cohesion and low coupling automatically.

In a small application, dependency injection can cause more work than
its worth. In this case, I usually create stubs for the ADO .NET
classes: IDbConnection, IDbCommand, IDataReader, etc. Then you can
call your database methods all day, the stubs just ignore them. But
this means your code has to support the general-purpose System.Data
classes and not the SQL Server-, Oracle-, etc.-specific classes (which
may be a good thing).

If you don't want to implement a bunch of stubs, a library like NMock
can come in handy. It will create some stubs automatically and record
the number of times and the order that stubbed methods were called.

Everything I learned was from the book: xUnit Test Patterns. It is a
long and redundant read, but well worth the effort if you're going to
write large-scale programs that are actually tested. :)
 
J

jehugaleahsa

I am trying to test a method, which in turn calls another method.
Which is something like the folloing

public string mainmethod()
{
     string val = submethod();
     ... do more ...

}

I am using NUnit and I do not want the submethod execute to get the
'val' I want some way to fake the value that is coming from that
method and continue with the rest of mainmethod process. Is there a
way to do it in NUnit?

The submethod has some expensive process that is running that I don't
want to initialise or execute.

Thanks,

Here is how I would go about doing this. There is probably a good
reason why submethod shouldn't get executed in test. Think about
moving this method into another class. If you are hitting a database
or whatever, separating the logic from persistence is just good
practice. Once you have your new class, create an interface for it.
"Okay", you're saying. Your simple method call just became another
class and _now_ its also got an interface.

It gets worse. Now, you have to create a stub that implements your
interface. You must pass this stub into the class your testing. This
means you may need to change your class to take the submethod
interface as a ctor argument or as an argument to mainmethod.

interface submethod_interface { string submethod(); }
class submethod_production : submethod_interface { public string
submethod() { return "Production"; } }
class submethod_test : submethod_interface { public string submethod()
{ return "Test"; }}

[TestMethod]
public void TestMainMethod()
{
submethod_test stub = new submethod_test();
MyClass myClass = new MyClass(stub);
myClass.mainmethod();
}

Now the implementation for MyClass should be obvious.

class MyClass
{
submethod_interface _smi;
MyClass(submethod_interface smi) {_smi = smi;}
void mainmethod() { string value = _smi.submethod(); }
}

This whole process is called dependency injection. It makes large-
scale testing possible. It also has a wonderful habit of being a good
heuristic for design. By forcing your code to pass dependencies, you
get good cohesion and low coupling automatically.

In a small application, dependency injection can cause more work than
its worth. In this case, I usually create stubs for the ADO .NET
classes: IDbConnection, IDbCommand, IDataReader, etc. Then you can
call your database methods all day, the stubs just ignore them. But
this means your code has to support the general-purpose System.Data
classes and not the SQL Server-, Oracle-, etc.-specific classes (which
may be a good thing).

If you don't want to implement a bunch of stubs, a library like NMock
can come in handy. It will create some stubs automatically and record
the number of times and the order that stubbed methods were called.

Everything I learned was from the book: xUnit Test Patterns. It is a
long and redundant read, but well worth the effort if you're going to
write large-scale programs that are actually tested. :)
 
P

Pavel Minaev

I am trying to test a method, which in turn calls another method.
Which is something like the folloing

public string mainmethod()
{
     string val = submethod();
     ... do more ...

}

I am using NUnit and I do not want the submethod execute to get the
'val' I want some way to fake the value that is coming from that
method and continue with the rest of mainmethod process. Is there a
way to do it in NUnit?

The submethod has some expensive process that is running that I don't
want to initialise or execute.

The traditional TDD way is to avoid hard-coded method calls at all
costs. Interfaces all the way, and dependency injection to substitute
them for mocks as needed.

If you don't want to redesign half of your application just so you can
write neat tests, and you don't think that it is sensible to have an
interface for every single class you have, then consider looking at
Typemock Isolator (http://www.typemock.com/index.php). It uses
instrumentation to allow you to intercept and mock virtually
everything, up to and including static method calls. The downside is
that it's not free; but, in my opinion, the liberation from the less
sane parts of TDD that it brings is well worth it ;)
 
P

Pavel Minaev

I am trying to test a method, which in turn calls another method.
Which is something like the folloing

public string mainmethod()
{
     string val = submethod();
     ... do more ...

}

I am using NUnit and I do not want the submethod execute to get the
'val' I want some way to fake the value that is coming from that
method and continue with the rest of mainmethod process. Is there a
way to do it in NUnit?

The submethod has some expensive process that is running that I don't
want to initialise or execute.

The traditional TDD way is to avoid hard-coded method calls at all
costs. Interfaces all the way, and dependency injection to substitute
them for mocks as needed.

If you don't want to redesign half of your application just so you can
write neat tests, and you don't think that it is sensible to have an
interface for every single class you have, then consider looking at
Typemock Isolator (http://www.typemock.com/index.php). It uses
instrumentation to allow you to intercept and mock virtually
everything, up to and including static method calls. The downside is
that it's not free; but, in my opinion, the liberation from the less
sane parts of TDD that it brings is well worth it ;)
 
M

Mr. Arnold

I am trying to test a method, which in turn calls another method.
Which is something like the folloing

public string mainmethod()
{
string val = submethod();
... do more ...

}

I am using NUnit and I do not want the submethod execute to get the
'val' I want some way to fake the value that is coming from that
method and continue with the rest of mainmethod process. Is there a
way to do it in NUnit?

The submethod has some expensive process that is running that I don't
want to initialise or execute.

If you don't want to redesign half of your application just so you can
write neat tests, and you don't think that it is sensible to have an
interface for every single class you have, then consider looking at
Typemock Isolator (http://www.typemock.com/index.php). It uses
instrumentation to allow you to intercept and mock virtually
everything, up to and including static method calls. The downside is
that it's not free; but, in my opinion, the liberation from the less
sane parts of TDD that it brings is well worth it ;)

------------------------------------------------------------

There is also Rhino Mock. It's open source, it's very good and it's free.

http://www.lostechies.com/blogs/jimmy_bogard/archive/2008/10/05/three-simple-rhino-mocks-rules.aspx

Have you heard of Gallio another open source solution?

http://www.infoq.com/news/2009/04/Gallio-Jeff-Brown

There is also Resharper. It's also a nice UT tool along with other things
Resharper can do. Resharper is not free, but it is worth the cost.

MBunit, Rhino Mock, Gallio and Resharper is a very good combination for UT.



__________ Information from ESET NOD32 Antivirus, version of virus signature database 4074 (20090514) __________

The message was checked by ESET NOD32 Antivirus.

http://www.eset.com
 
M

Mr. Arnold

I am trying to test a method, which in turn calls another method.
Which is something like the folloing

public string mainmethod()
{
string val = submethod();
... do more ...

}

I am using NUnit and I do not want the submethod execute to get the
'val' I want some way to fake the value that is coming from that
method and continue with the rest of mainmethod process. Is there a
way to do it in NUnit?

The submethod has some expensive process that is running that I don't
want to initialise or execute.

If you don't want to redesign half of your application just so you can
write neat tests, and you don't think that it is sensible to have an
interface for every single class you have, then consider looking at
Typemock Isolator (http://www.typemock.com/index.php). It uses
instrumentation to allow you to intercept and mock virtually
everything, up to and including static method calls. The downside is
that it's not free; but, in my opinion, the liberation from the less
sane parts of TDD that it brings is well worth it ;)

------------------------------------------------------------

There is also Rhino Mock. It's open source, it's very good and it's free.

http://www.lostechies.com/blogs/jimmy_bogard/archive/2008/10/05/three-simple-rhino-mocks-rules.aspx

Have you heard of Gallio another open source solution?

http://www.infoq.com/news/2009/04/Gallio-Jeff-Brown

There is also Resharper. It's also a nice UT tool along with other things
Resharper can do. Resharper is not free, but it is worth the cost.

MBunit, Rhino Mock, Gallio and Resharper is a very good combination for UT.



__________ Information from ESET NOD32 Antivirus, version of virus signature database 4074 (20090514) __________

The message was checked by ESET NOD32 Antivirus.

http://www.eset.com
 
P

Pavel Minaev

If you don't want to redesign half of your application just so you can
write neat tests, and you don't think that it is sensible to have an
interface for every single class you have, then consider looking at
Typemock Isolator (http://www.typemock.com/index.php). It uses
instrumentation to allow you to intercept and mock virtually
everything, up to and including static method calls. The downside is
that it's not free; but, in my opinion, the liberation from the less
sane parts of TDD that it brings is well worth it ;)

------------------------------------------------------------

There is also Rhino Mock. It's open source, it's very good  and it's free.

http://www.lostechies.com/blogs/jimmy_bogard/archive/2008/10/05/three...

Have you heard of Gallio another open source solution?

http://www.infoq.com/news/2009/04/Gallio-Jeff-Brown

There are plenty of good mocking solutions for .NET indeed, many of
them free, but TypeMock is the only one that I know that isn't limited
to mocking virtual non-sealed methods on inheritable classes. That was
my point - it doesn't require you to (re)design your code to match the
limitations of the testing framework.
 
P

Pavel Minaev

If you don't want to redesign half of your application just so you can
write neat tests, and you don't think that it is sensible to have an
interface for every single class you have, then consider looking at
Typemock Isolator (http://www.typemock.com/index.php). It uses
instrumentation to allow you to intercept and mock virtually
everything, up to and including static method calls. The downside is
that it's not free; but, in my opinion, the liberation from the less
sane parts of TDD that it brings is well worth it ;)

------------------------------------------------------------

There is also Rhino Mock. It's open source, it's very good  and it's free.

http://www.lostechies.com/blogs/jimmy_bogard/archive/2008/10/05/three...

Have you heard of Gallio another open source solution?

http://www.infoq.com/news/2009/04/Gallio-Jeff-Brown

There are plenty of good mocking solutions for .NET indeed, many of
them free, but TypeMock is the only one that I know that isn't limited
to mocking virtual non-sealed methods on inheritable classes. That was
my point - it doesn't require you to (re)design your code to match the
limitations of the testing framework.
 
J

jehugaleahsa

There are plenty of good mocking solutions for .NET indeed, many of
them free, but TypeMock is the only one that I know that isn't limited
to mocking virtual non-sealed methods on inheritable classes. That was
my point - it doesn't require you to (re)design your code to match the
limitations of the testing framework.

--------------------------------------------

To be honest, I don't even mock anymore. I kind of find it a waste of time.
I do the UT in the UT project, but I don't mock it, and when I have tested
it, I move the code out of the UT project into the non UT project as code
ready to go.  I can test the whole BLL, DAL and then go to the Presenter of
the MVP using UT as a harness before it get's tested at the UI. It saves a
lot of time.

__________ Information from ESET NOD32 Antivirus, version of virus signature database 4077 (20090514) __________

The message was checked by ESET NOD32 Antivirus.

http://www.eset.com- Hide quoted text -

- Show quoted text -

I've also found that you can eliminate mocks and stubs in a lot of
cases. If you are working at the bottom of your application, you might
find you need a class higher up. I say just implement it partially for
now. When you get up to that level, you can finish it. I tend to
implement my software at the bottom first, then the top and finally in
the middle. That allows me to worry about the data access, the user
interface and not so much about the business logic (the code that
really needs tested). I usually wrap my lower layer stuff behind
facade interfaces so that I can quickly stub out large features of the
lower layers. I end up simply creating mocks for the facades and I'm
done. I test the code that matters the most, the most: the business
logic.
 
J

jehugaleahsa

There are plenty of good mocking solutions for .NET indeed, many of
them free, but TypeMock is the only one that I know that isn't limited
to mocking virtual non-sealed methods on inheritable classes. That was
my point - it doesn't require you to (re)design your code to match the
limitations of the testing framework.

--------------------------------------------

To be honest, I don't even mock anymore. I kind of find it a waste of time.
I do the UT in the UT project, but I don't mock it, and when I have tested
it, I move the code out of the UT project into the non UT project as code
ready to go.  I can test the whole BLL, DAL and then go to the Presenter of
the MVP using UT as a harness before it get's tested at the UI. It saves a
lot of time.

__________ Information from ESET NOD32 Antivirus, version of virus signature database 4077 (20090514) __________

The message was checked by ESET NOD32 Antivirus.

http://www.eset.com- Hide quoted text -

- Show quoted text -

I've also found that you can eliminate mocks and stubs in a lot of
cases. If you are working at the bottom of your application, you might
find you need a class higher up. I say just implement it partially for
now. When you get up to that level, you can finish it. I tend to
implement my software at the bottom first, then the top and finally in
the middle. That allows me to worry about the data access, the user
interface and not so much about the business logic (the code that
really needs tested). I usually wrap my lower layer stuff behind
facade interfaces so that I can quickly stub out large features of the
lower layers. I end up simply creating mocks for the facades and I'm
done. I test the code that matters the most, the most: the business
logic.
 
B

Ben Voigt [C++ MVP]

CSharper said:
I am trying to test a method, which in turn calls another method.
Which is something like the folloing

public string mainmethod()
{
string val = submethod();
... do more ...
}

I am using NUnit and I do not want the submethod execute to get the
'val' I want some way to fake the value that is coming from that
method and continue with the rest of mainmethod process. Is there a
way to do it in NUnit?

I see a lot of people have told you how, but I didn't see a name given.
This is the "Inversion of Control" pattern.
 
B

Ben Voigt [C++ MVP]

CSharper said:
I am trying to test a method, which in turn calls another method.
Which is something like the folloing

public string mainmethod()
{
string val = submethod();
... do more ...
}

I am using NUnit and I do not want the submethod execute to get the
'val' I want some way to fake the value that is coming from that
method and continue with the rest of mainmethod process. Is there a
way to do it in NUnit?

I see a lot of people have told you how, but I didn't see a name given.
This is the "Inversion of Control" pattern.
 
B

Ben Voigt [C++ MVP]

CSharper said:
I am trying to test a method, which in turn calls another method.
Which is something like the folloing

public string mainmethod()
{
string val = submethod();
... do more ...
}

I am using NUnit and I do not want the submethod execute to get the
'val' I want some way to fake the value that is coming from that
method and continue with the rest of mainmethod process. Is there a
way to do it in NUnit?

The submethod has some expensive process that is running that I don't
want to initialise or execute.

Thanks,

Also, one easy way to deal with this scenario without needing to introduce a
bunch of interfaces, etc, is to separate the data fetching from the data
processing. Unlike dependency injection, this method will let the caller
remain agnostic of the change:

public string mainmethod()
{
return mainMethodImpl(submethod());
}

string mainMethodImpl(string var)
{
...
}

now mainMethodImpl is easily tested, but callers can keep on using
mainmethod with the same interface as before.
 
B

Ben Voigt [C++ MVP]

CSharper said:
I am trying to test a method, which in turn calls another method.
Which is something like the folloing

public string mainmethod()
{
string val = submethod();
... do more ...
}

I am using NUnit and I do not want the submethod execute to get the
'val' I want some way to fake the value that is coming from that
method and continue with the rest of mainmethod process. Is there a
way to do it in NUnit?

The submethod has some expensive process that is running that I don't
want to initialise or execute.

Thanks,

Also, one easy way to deal with this scenario without needing to introduce a
bunch of interfaces, etc, is to separate the data fetching from the data
processing. Unlike dependency injection, this method will let the caller
remain agnostic of the change:

public string mainmethod()
{
return mainMethodImpl(submethod());
}

string mainMethodImpl(string var)
{
...
}

now mainMethodImpl is easily tested, but callers can keep on using
mainmethod with the same interface as before.
 
J

jehugaleahsa

Also, one easy way to deal with this scenario without needing to introduce a
bunch of interfaces, etc, is to separate the data fetching from the data
processing.  Unlike dependency injection, this method will let the caller
remain agnostic of the change:

public string mainmethod()
{
    return mainMethodImpl(submethod());

}

string mainMethodImpl(string var)
{
    ...

}

now mainMethodImpl is easily tested, but callers can keep on using
mainmethod with the same interface as before.- Hide quoted text -

- Show quoted text -

Could you explain this a little further for me? I don't think I quite
understand. Is this some sort of strategy pattern? How can the data
fetching (assumingly in the submethod method) be avoided during test?

Thanks,
Travis
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top