Click here to Skip to main content
15,905,136 members
Home / Discussions / C#
   

C#

 
GeneralRe: nUnit and TDD Pin
Wes Aday21-Mar-08 4:25
professionalWes Aday21-Mar-08 4:25 
GeneralRe: nUnit and TDD Pin
led mike21-Mar-08 4:40
led mike21-Mar-08 4:40 
GeneralRe: nUnit and TDD Pin
Ennis Ray Lynch, Jr.21-Mar-08 4:51
Ennis Ray Lynch, Jr.21-Mar-08 4:51 
GeneralRe: nUnit and TDD Pin
Not Active21-Mar-08 5:10
mentorNot Active21-Mar-08 5:10 
GeneralRe: nUnit and TDD Pin
#realJSOP21-Mar-08 5:49
professional#realJSOP21-Mar-08 5:49 
GeneralRe: nUnit and TDD Pin
led mike21-Mar-08 6:45
led mike21-Mar-08 6:45 
GeneralRe: nUnit and TDD Pin
Not Active21-Mar-08 7:15
mentorNot Active21-Mar-08 7:15 
GeneralRe: nUnit and TDD Pin
Judah Gabriel Himango21-Mar-08 6:27
sponsorJudah Gabriel Himango21-Mar-08 6:27 
John Simmons / outlaw programmer wrote:
It seems to me that it's only real purpose is to test discrete functions and classes

NUnit can be used to test your whole application, but just parts of it at a time. Unit testing doesn't work for integration tests or tests where you want to see if 2 or more components work well together. Unit testing is just that: testing a unit at a time. In practical sense, this means testing a single method at time.

There are some barriers to unit testing: if you've mixed logic code in your GUI classes, unit testing them may cause your forms and controls to show up during the unit test runs, slowing things down and making testing a real pain. That's bad. Separate your logic from your GUI and you'll be good. Then you don't write unit tests for your GUI classes since there's no logic to be tested.

Another barrier to unit testing is hard-coded dependencies on the file system, the network, databases, etc. For example, if you've got a function that should open a connection to a database, and you want to test that the database is opened, you're in a predicament: your test can actually open the database, but that can slow down unit tests and it might not work if run on certain machines. Instead of opening a real database, make your code flexible enough so that there's no hard-coded dependency on your database:

// old code:
void OpenTheDatabase()
{
   someSqlConnection.Open();
}

// new code:
void OpenTheDatabase(IDbConnection connection)
{
    connection.Open();
}
Here you injec a connection object, which could be a dummy object you create in your tests:

[Test]
public void OpenTheDatabaseDoesExactlyThat()
{
     DummyConnection connection = new DummyConnection();
     myDataLayer.OpenTheDatabase(connection);
    
     Assert.IsTrue(connection.Opened);
}

class DummyConnection : IDbConnection
{
    public bool Opened = false;
    
    public void Open()
    {
         opened = true;
    }
}
Ah, now your test doesn't actually talk to a database during the unit tests.

It's a contrieved example, sure, but you get the idea: replace hard-coded dependencies with fake objects. For a better example, see Martin Fowler's Dependency Injection[^] article.

Now, once you've done a few unit tests like this, you begin to realize it's really a pain to have to create dummy classes like our DummyConnection every dang time you want to replace a dependency with a fake object.

Fortunately, there are some real great pieces of code out there, mocking frameworks, that take care of this for you. At my job we use a free one called RhinoMocks[^]. With it, replacing dependencies with fake objects becomes a breeze:

[Test]
public void OpenTheDatabaseDoesExactlyThat()
{
     using (rhinoMocks.Record())
     {
        IDbConnection fakeConnection = rhinoMocks.CreateMock<IDbConnection>();
        fakeConnection.Open();
     }

     using (rhinoMocks.Playback())
     {
        myDataLayer.OpenTheDatabase(connection);
     }
}
Notice in the above example, there is no DummyClass we had to create. Also notice we didn't have to Assert that Open() was called on the IDbConnection object -- we just told RhinoMocks we were expecting connection.Open to be called, and that was it.

John Simmons / outlaw programmer wrote:
but what about adding nUnit to existing code?

You mean unit testing already-written code? Since that code's probably gonna be hard to test, it's gonna be hard to do. Skip the elementary nUnit and go for something more powerful. TypeMock[^] allows you to unit test existing codebases that weren't built with unit testing in mind. (It's so powerful, some people look down on it[^] because it allows you to write code the "old" way: hard-coded dependencies, tight coupling, etc. and still do unit tests.)

John Simmons / outlaw programmer wrote:
TDD makes a lot more sense in theory because you have to have a good design up front in order to write applicable/viable tests

Precisely. It makes you essentially write blue prints for your code before actually writing the code. If you've got tests like ListensForUserModification(), SavesToDatabaseWhenClosing(), you've got a blueprint for how your code should work, and you've already put some thought into the design before actually writing code.


Tech, life, family, faith: Give me a visit.
I'm currently blogging about: The Torah is Obsolete and Passing Away?
The apostle Paul, modernly speaking: Epistles of Paul

Judah Himango


GeneralDateTime Setting A Specifed Week Pin
Zap-Man21-Mar-08 3:00
Zap-Man21-Mar-08 3:00 
GeneralRe: DateTime Setting A Specifed Week Pin
PIEBALDconsult21-Mar-08 4:49
mvePIEBALDconsult21-Mar-08 4:49 
GeneralVisual Studio Automation Model Pin
DanB198321-Mar-08 2:56
DanB198321-Mar-08 2:56 
GeneralRe: Visual Studio Automation Model Pin
led mike21-Mar-08 4:53
led mike21-Mar-08 4:53 
GeneralRe: Visual Studio Automation Model Pin
DanB198321-Mar-08 4:58
DanB198321-Mar-08 4:58 
GeneralRe: Visual Studio Automation Model Pin
DanB198321-Mar-08 6:34
DanB198321-Mar-08 6:34 
QuestionQuestion Regarding Combo box Pin
Krazy Programmer21-Mar-08 1:08
Krazy Programmer21-Mar-08 1:08 
GeneralRe: Question Regarding Combo box Pin
Luc Pattyn21-Mar-08 1:13
sitebuilderLuc Pattyn21-Mar-08 1:13 
GeneralRe: Question Regarding Combo box Pin
Krazy Programmer21-Mar-08 2:54
Krazy Programmer21-Mar-08 2:54 
GeneralRe: Question Regarding Combo box Pin
Luc Pattyn21-Mar-08 3:07
sitebuilderLuc Pattyn21-Mar-08 3:07 
GeneralAllocate disk spce before file copy Pin
poqeqw20-Mar-08 22:54
poqeqw20-Mar-08 22:54 
GeneralRe: Allocate disk spce before file copy Pin
pmarfleet21-Mar-08 0:30
pmarfleet21-Mar-08 0:30 
GeneralRe: Allocate disk spce before file copy Pin
Luc Pattyn21-Mar-08 1:16
sitebuilderLuc Pattyn21-Mar-08 1:16 
GeneralAnother Exception,, Pin
ptr2void20-Mar-08 22:03
ptr2void20-Mar-08 22:03 
GeneralRe: Another Exception,, Pin
MarkB77720-Mar-08 23:17
MarkB77720-Mar-08 23:17 
GeneralSet column width of datagrid Pin
Xmen Real 20-Mar-08 21:06
professional Xmen Real 20-Mar-08 21:06 
GeneralRe: Set column width of datagrid Pin
darkelv20-Mar-08 21:32
darkelv20-Mar-08 21:32 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.