views:

63

answers:

1

I've just started with unit testing, and would like to know if I'm on the right track. Can you guys please take a look at my code and tell me what you think if it?

This is the (basic) method that I want to test:

public void Accept(long resumeId)
{
   // Get the resume that has to be accepted
   Resume originalResume = ResumeDAC.GetById(id);

   // Make a copy of the resume
   Resume resumeCopy = CopyResume(originalResume );

   // Accept the original resume and lock it
   originalResume .AcceptAndLock();

   // Accept the copy and keep it open for editing
   resumeCopy.AcceptAndKeepOpen();

   // Add the copy to the database
   ResumeDAC.Add(resumeCopy);

   // Commit everything
   ResumeDAC.Commit();
}

What I want to test is this:

  • The original resume has the status 'AcceptedAndLocked'
  • The copied resume has the status 'AcceptedAndOpen'
  • The copied resume was added to the database

I don't want to use the actual database in this test, but I have two classes that use the database: EmployeeDAC and ResumeDAC. EmployeeDac is easy, so I've mocked it with Rhino Mocks. ResumeDAC is a bit harder I think because a 'resume copy' is made and added to the database. I want to assert that the resume copy is added to the database.

Let's take a look at my testcode:

[TestMethod]
public void TestAcceptResume()
{
   // Arrange
   // These two classes are used for database actions
   EmployeeDAC mockEmployeeDAC = ArrangeEmployeeDAC(); // Simply mocked with Rhino Mocks
   TestResumeDAC mockResumeDac = new TestResumeDAC(); //I've created my own test double. See below

   // Inject the two Data Access Classes into the ResumeService
   var resumeService = new ResumeService("", mockResumeDac, mockEmployeeDAC);

   // Act
   var resume = resumeService.Accept(3);

   // Assert
   Resume origResume = mockResumeDac.Resumes.Single(it => it.Id == resumeId);
   Assert.AreEqual("AcceptedAndLocked", origResume.StatusName);
   Assert.AreEqual(4, mockResumeDac.Resumes.Count); // Expect 4 records in the database
}

The is my own testversion of the ResumeDAC (Data Access Class) used in the test

internal class TestResumeDAC : ResumeDAC
{
   public TestResumeDAC() : base()
   {
      Resumes = new List<Resume>();

      // Add resumes to the list
      AddResumes();
   }

   public List<Resume> Resumes { get; set; }

   public override void Add(Resume resume)
   {
      Resumes.Add(resume);
   }

   public override void Commit()
   {
   }

   public override Resume GetById(long id)
   {
      return Resumes.SingleOrDefault(it => it.Id == id);
   }
}

As you can see I've made kind of a in memory database. This makes is easy for me to see if objects are added or deleted etc.

So my question is: how do you like this code? Please let me know if this is not the way to go, or any erros I've made in my thinking, tips etc.

A: 

I'm guessing that this code isn't working for you. Is it? You didn't say...

Anyhow, I took a long look at unit testing at the behest of one of my managers. I ended up using "The Art of Unit Testing", by Roy Osherove. It's a very good introduction to unit testing.

It looks like you might have made ResumeDAC is a static class. In order to override it with your test class ResumeDAC, you'll have to get rid of the static method calls.

In any case, the issue at hand is probably replacing ResumeDAC with your test version of the class. This is called "dependency injection".

Rice Flour Cookies