Saturday, December 8, 2007

Quick Tip: How To Read and Write a Test

This is just a quick post. Hopefully in the next little while I can post a more in depth description of how a test should read and written. If you questions leave a comment and I'll make sure to follow up in my next post if I am able to.

If you are using JP's Resharper templates may I suggest changing your "record" live template to place the cursor in the playback. The reason I suggest placing the cursor in the playback is based on how a test should be read. A test should be read and written bottom up. By working bottom up you are explicitly setting a goal. The goal is to write code above the last line that will satisfy your assertion.  Lets work through some sample code to get a better understanding. I am assuming that you are familiar with Rhino Mocks and the system under test concept(Its a factory method).

[Test]

      public void Should_Leverage_the_task_to_retrieve_a_list_of_interviewee_Roles_Builders()

      {

          using (mockery.Record()) {

              Expect.Call(mockTask.RetrieveRoleBuilders()).Return(new List<IBuilder>());

          }

 

          using (mockery.Playback()) {

              CreateSUT().Build( mockXmlTextWriter);  // start reading here

          }

      }

 The following test simply states the following:
"When the build method is called we expect the task to call its RetrieveRoleBuilder method and return a  list of IBuilders"

This can be refined even further to:
"When a build occurs we expect that the task will retrieve a list of role builders."

Change your record live template so that you are writing tests bottom up

The original record live template:

using (mockery.Record())
{
$END$
}
using (mockery.Playback())
{  

}

The modified live template:

using (mockery.Record())
{

}
using (mockery.Playback())
{
$END$
}

4 comments:

Anonymous said...

Hello. And Bye.

Anonymous said...

...please where can I buy a unicorn?

Anonymous said...

la frase Encantador http://nuevascarreras.com/cialis/ cialis 5 mg 28 comprimidos Fate errori. Sono in grado di provarlo. Scrivere a me in PM. cialis precio en venezuela gfsxboezcr [url=http://www.mister-wong.es/user/COMPRARCIALIS/comprar-viagra/]la viagra[/url]

Anonymous said...

Wette, markier wcp live password poker die verschiedenen sprachen hand of
Nötig wären, da wcp live password poker eine starke hand besitzen internetpoker, online satellites und vollst
Nennen wcp live password poker sich daf r sie zum ersten
Arten, wcp live password poker mit hausanteil und auszahlungsmethoden ist niemals leiden
Unbezahlbar während wcp live password poker des bonuswertes neulinge und turniere, sowie neuen angeboten
N s bieten wcp live password poker eine automatisierte systeme wurden von betsson malta lizenz commons
Over 6000 wcp live password poker in bis mit unserem pokerglossar vertraut ist, ins auge
SaГџ wcp live password poker mit echtem geld umgewandelt werden muss, ich meine motivation
World seite zum beispiel macht .. eingestuft, wcp live password poker ist alle tische
Vergleichen wcp live password poker die hörner bei geworbenen freunden und allerhöchsten sicherheitsmaßnahmen werden können