BDD / TDD bullying data in a complicated way

So, my colleague and I have a very heated debate. We are starting a new project and we are trying to use BDD. We are both pioneers and do not quite understand what methods should be used. We have written some specifications, and now we are implementing the code. Everything becomes quite complicated, as there is a lot of interaction with databases. We focus on how to mock our data. The method we were going to would require us to mock our methods instead of our data. This is easiest if I show you the 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 () in order to return us our data in order to verify this. It seems to make sense to do it like this:

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

Then we could just make fun of our objects.

Clearly, the second method simplifies testing. However, the argument is that we do not want to get a new lead and distributor object on our front-end code, because it would be easier to just pass the identifiers of our objects. Reduction of the actual code in our interface.

I hope I explained it quite well.

What do you guys think? Which way makes sense?

+6
unit-testing tdd mocking specifications bdd
source share
3 answers

What we do in our BDD specifications (executable stories) is not to not mock the database at all, but to use the built-in database instead (SQLite in our case).

In addition, we initialize the container before running any script. This is because we would like our BDD specifications to simulate the real world as much as possible, while maintaining the speed of conventional unit tests.

Thus, defining our BDD specifications, we found the need for unit tests and integration tests, and also increased productivity and comprehensibility (although they are very subjective, since you cannot really measure these indicators).

+3
source share

I think the biggest problem you are facing is that you use public static functions (which is usually bad in OO languages).

I would suggest moving this function to a Lead object, something like

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

Easier to test and more OO-like code =)

+8
source share

I like the second method better because you stated: you can easily evaluate the parameters for testing. Do you use a dependency lookup system? If not, I would recommend that you program your methods using the principle of Injection Dependency, in any case, for more unit and simple code testing.

And I agree with Samuel that you need to avoid using static methods whenever possible, or it will be very difficult for you to test them.

+2
source share

All Articles