0

I'm currently working on a project where I'm receiving an object via web service (WSDL).

The overall process is the following:

Receive object -> add/delete/update parts (or all) of it -> and return the object with the changes made.

The thing is that sometimes these changes are complicated and there is some logic involved, other databases, other web services, etc. so to facilitate this I'm creating a custom object that mimics the original one but has some enhanced functionality to make some things easier.

So I'm trying to have this process:

Receive original object -> convert/copy it to custom object -> add/delete/update -> convert/copy it back to original object -> return original object.

Example:

public class Row
{
    public List<Field> Fields { get; set; }
    public string RowId { get; set; }
    public Row()
    {
        this.Fields = new List<Field>();
    }
}

public class Field
{
    public string Number { get; set; }
    public string Value { get; set; }
}

So for example, one of the "actions" to perform on this would be to find all Fields in a Row that match a Value equal to something, and update them with some other value.

I have a CustomRow class that represents the Row class, how can I make this class unit testable? Do I have to create an interface ICustomRow to mock it in the unit test? If one of the actions is to sum all of the Values in the Fields that have a Number equal to 10, like this function, how can design the custom class to facilitate unit tests.

Sample function:

public int Sum(FieldNumber number)
{
    return row.Fields.Where(x => x.FieldNumber.Equals(number)).Sum(x => x.FieldValue);
}

Am I approaching this the wrong way?

3
  • I suggest spending some time with Roy's classes here artofunittesting.com .
    – Maru
    Commented Oct 23, 2013 at 16:04
  • Why aren't you using extension methods with this? It has little to do with the testablity of the code but it seems like it would remove a fair bit of code and prevent risk in the copying to/from.
    – Sign
    Commented Oct 23, 2013 at 18:31
  • @Maru awesome link, along with the answer it'll get there.
    – SOfanatic
    Commented Oct 24, 2013 at 1:17

1 Answer 1

3

Yes, I would say you're approaching it the wrong way. Why mock anything?

Let's look at your units:

receive object - testing the web service call isn't your goal, use an integration test.

convert to custom object - you need the original object and the result object to make sure this operation works. Just use them.

stuff - modifying your custom object uses just the custom object. No dependencies to mock here. Make a custom object, run the conversion, make sure you end up in the new state.

(and the reverse conversions use the same traits)

Since there's no unnecessary dependencies, there's nothing to mock.

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.