1

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 testing\mocking 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
  • I think we need more info. What is the purpose of the method, a bit of context/background would help. I mean I do understand what the code does, but I can't understand why
    – MikeSW
    Jan 20, 2014 at 11:18
  • Sure thing. 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. Jan 20, 2014 at 11:39

2 Answers 2

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
  • Can you give an example of how you would mock the fields repository and validate that the FindWhere method was called with the correct arguments? Jan 20, 2014 at 11:48
  • Or some alternative way of verifying the fields passed to dataservice.PerformUpdate. Jan 20, 2014 at 11:58
  • Sure, can you tell me what assemblies you have for each object? I can pull them and mock it out then Jan 20, 2014 at 11:59
  • I'm not sure what more you need here? You can assume that IDataService just has one member matching the arguments seen in the example. Jan 20, 2014 at 12:04
  • 1
    @Roysvork I think what @McDonnellDean means is that a unit test for UpdateService shouldn't test the correctness of objects it collaborates with (Fields.FindWhere()). This is precisely what mocks and stubs are for - remove your neighbor's correctness from the equation. Just test that you send them the right messages and can handle their answers. And if Fields.FindWhere() is code you wrote, then yes, you'll have to test it, but somewhere else. Jan 20, 2014 at 16:27
1

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
  • This is not the purpose of this question :) Regardless of how this is structured, at some point I will need test that I am supplying the correct fields and it is that verification which is at the end of this rabbit hole :) Jan 20, 2014 at 11:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.