How would you unit test the PerformUpdate method in the sample below? This is a (very) simplified version of some code that performs updates against a database where the schema is dynamic and represented by the fields collection.
EDIT: If it is not clear what I am asking, I would like to know how different developers would approach testingmocking this code from a TDD perspective. For example, what tests would you write, what would you mock, how would you verify method calls are made with the right arguments?
public interface IUnitOfWork
{
IEfRepository<Field> Fields { get; }
}
public class UpdateService
{
private IUnitOfWork unitOfWork;
private IDataUpdater dataService;
public Activity PerformUpdate(App app, IDictionary<string, object> data)
{
var fields = this.unitOfWork.Fields.FindWhere(o => o.App.Identifier == app.Identifier);
var activity = this.dataService.PerformUpdate(fields, data);
return activity;
}
}
2
There are a few different parts to be tested here. Ultimately my answer is the same regardless of the specific portion of the code. The validity of each entities method should be tested separately as it’s own concern (I’m sure your doing this anyway).
In terms of testing say the PerformUpdate
method, your best bet here is to mock / stub both the IUnitOfWork
and IDataUpdater
. In each case the mock / stubs would make an assumption (good data and bad data). Simply query these after they are called to confirm the validity of the call.
17
Here’s an unhelpful answer for you 🙂
I’d probably not unit test this, and would create a more behavioural component-level test with a few complex Fakes.
Wondering how you test a particular implementation seems like a smell to me – you should be asking ‘how would I develop an implementation to fulfil this requirement?’
1