how to architect this to make it unit testable

Posted by SOfanatic on Programmers See other posts from Programmers or by SOfanatic
Published on 2013-10-23T16:00:05Z Indexed on 2013/10/23 16:07 UTC
Read the original article Hit count: 243

Filed under:
|
|

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?

© Programmers or respective owner

Related posts about c#

Related posts about architecture