Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

BDD/TDD mocking data the tricky way

So a coworker and I are in a fairly heated debate. We are beginning a new project and we are attempting to use BDD. We are both first-timers and don't fully understand what practices should be used. We've written some specs and we are now implementing the code. Things are getting pretty tricky since there is a lot of database interaction. We are stuck on how we should mock our data. The method we were going about would require us to mock our methods instead of our data. It's easiest if I show you in code...

public static void AssignLeadToDistributor(int leadId, int distributorId)
{
    Lead lead = GetById(leadId);
    lead.DistributorId = distributorId;
    Save(lead);
}

Basically, we would have to override GetById() and Save() to return mock data for us to test this. It seems to make more sense to do it like this:

public static void AssignLeadToDistributor(Lead lead, Distributor distributor)
{
   lead.DistributorId = distirbutor.Id;
}

Then we could just mock our objects.

Clearly the second method makes it easier to test. However, the argument is that we don't want to have to fetch a new lead and distributor object on our front end code, because it would be easier to just pass the ids of our objects. Cutting down on the actual code in our front end.

Hopefully I explained that well enough.

What do you guys think? Which way makes more sense?

like image 981
Kevin Wiskia Avatar asked Jan 27 '10 00:01

Kevin Wiskia


1 Answers

I think the biggest problem you're having is because you're using public static functions (which is usually a bad thing in OO languages).

I'd suggest moving this function to the Lead object, something like

public AssignDistributor(int distributorId) {
   this.DistributorId = distributorId;
   this.Save();
}

Easier to test, and more OO-like code =)

like image 101
Samuel Carrijo Avatar answered Nov 15 '22 11:11

Samuel Carrijo