0

I need to some how unit test this method. The problem is that FsFileGroupFile is not easily mocked, it has complex constructor requirements and doesn't directly use an interface. _blockReaderFactory on the other hand is an interface and therefore easy to mock. How can I mock such a complicated object. I'm using Rhino Mocks and the Microsoft Unit Testing Framework. Any one have any ideas?

    public void ReadGeneral(FsFileGroupFile a_file, FileItemData a_fileItemData)
    {
        try
        {
            var blockReader = _blockReaderFactory.Create(a_file.File.FullName, "CabinetData/StartData");

            var version = blockReader.ReadVersion();
            var name = blockReader.ReadString();
            var type = blockReader.ReadString();
            var defaultHeight = blockReader.ReadDouble();
            var defaultWidth = blockReader.ReadDouble();
            var defaultDepth = blockReader.ReadDouble();

            a_fileItemData.Name = name;
            a_fileItemData.DefaultWidth = defaultWidth * 100.0;
            a_fileItemData.DefaultHeight = defaultHeight * 100.0;
            a_fileItemData.DefaultDepth = defaultDepth * 100.0;
        }
        catch (Exception ex)
        {
            throw new IOException("General data could not be read from block data.", ex);
        }
    }

3 Answers 3

1

It seems that you're only using a_file to get the filename. So why not create an interface FilenameSupplier (or similar), and write a wrapper that implements it?

Java code example (added before question was tagged as C#...):

interface FilenameSupplier {
    String getName();
}

public void ReadGeneral(FilenameSupplier a_file, FileItemData a_fileItemData) {
    ...
    a_file.getName();
    ...
}

class ConcreteSupplier implements FilenameSupplier {
    private final FsFileGroupFile file;
    public ConcreteSupplier(FsFileGroupFile file) { this.file = file; }
    String getName() { return a_file.File.FullName; }
}
2
  • I'm using C# not Java. I'm sorry I didn't say that in the post. I think that's what I'll do. It seems weird to have an interface only used for testing though. But I'm at my whits end.
    – Jordan
    Jul 25, 2013 at 20:53
  • @Jordan: Well, that's what one did before reflection-based mocking frameworks were common ;) I'm not familiar enough with Rhino (or C#) to know if there's a better way to achieve this, I'm afraid... Jul 25, 2013 at 20:55
0

You should extract some interface from FsFileGroupFile and pass it into constructor argument. Then you can easily mock this interface with your preferable framework, Rhino Mocks in your case.

If it is not appropriate, you should build your FsFileGroupFile and may be use mocks when passing arguments in its complex constructor.

Seems that there is no another options, except may be you should review your design here. If classes are so hard to test it can be a sign of poor design.

0

When I have had to create complicated objects in a test I've used the Test Data Builder Pattern. As an example let's assume that you have five values to pass to the constructor:

public FsFileGroupFile(string firstProperty, string secondProperty,
    string thirdProperty, string fourthProperty, string fifthProperty)
{
    // constructor logic goes here
}

This would then be wrapped with a test builder class in the unit test project:

public class FsFileGroupFileBuilder
{
    public string FirstProperty { get; set; }
    public string SecondProperty { get; set; }
    public string ThirdProperty { get; set; }
    public string FourthProperty { get; set; }
    public string FifthProperty { get; set; }

    public FsFileGroupFile Build()
    {
        return new FsFileGroupFile(FirstProperty, SecondProperty, ThirdProperty,
            FourthProperty, FifthProperty);
    }
}

Now you can assign values to only the properties you care about and build your object in this way:

// in your test setup use this to initial to a default/valid state
var fsFileGroupBuilder = new fsFileGroupBuilder
{
    FirstProperty = "Default",
    SecondProperty = "Default",
    ThirdProperty = "Default",
    FourthProperty = "Default",
    FifthProperty = "Default"
}

Note: Rhino Mocks can probably set those default value for you, but I have not used it personally so I'm not sure.

// Override the properties in each test
fsFileGroupBuilder.ThirdProperty = "Value needed for unit test."

// create
var fileItemData = new FileItemData();
ReadGeneral(fsFileGroupBuilder.Build(), fileItemData);

There are other open source libraries that can help with generating the test data such as NBuilder which have worked well for me in the past.

The main point here is that the complex constructor can be abstracted away with a builder which will allow you to concentrate on testing your business logic instead of satisfying the constructor in every test.

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.