IamaC
IamaC

Reputation: 377

How to structure my unit test?

I am new to unit test and wondering how to start testing. The application I am currently working on, does not have any unit test. It a winform application and I am only interested to test the data layer of this application.

Here is an example.

public interface ICalculateSomething
{
     SomeOutout1 CalculateSomething1(SomeInput1 input1);
     SomeOutout2 CalculateOSomething2(SomeInput2 input2);
}

public class CalculateSomething : ICalculateSomething
{
    SomeOutout1 ICalculateSomething.CalculateSomething1(SomeInput1 input1)
    {
        SomeOutout1.Prop1 = calculateFromInput1(input1.Prop1, input1.Prop2);

        SomeOutout1.Prop3 = calculateFromInput2(input1.Prop3, input1.Prop4);

        return SomeOutout1;
    }

    SomeOutout2 ICalculateSomething.CalculateOSomething2(SomeInput2 input2)
    {
        SomeOutout2.Prop1 = calculateFromInput1(input2.Prop1, input2.Prop2);

        SomeOutout2.Prop3 = calculateFromInput2(input2.Prop3, input2.Prop4);

        return SomeOutout2;
    }
}

I would like to test these two methods in the CalculateSomething. Those methods implementation are long and complicated. How should I structure my test?

Upvotes: 1

Views: 240

Answers (3)

The Vermilion Wizard
The Vermilion Wizard

Reputation: 5395

Those are explicitly implemented properties, so you have to use an interface reference to test them.

var input1 = new SomeInput1();
// setup required data in input1.

ICalculateSomething calculator = new CalculateSomething();
var output = calculator.CalculateSomething1(input1);

// Have assert statements on the properties of output to verify the calculation.

Don't use var for calculator, because that will give you a CalculateSomething reference and the interface methods are hidden.

Upvotes: 1

Heinzi
Heinzi

Reputation: 172200

I don't see a reason for not using a straight-forward unit test implementation. I'd start with a basic test method:

[TestMethod]
public void CalculateSomething1_FooInput
{
    var input = new SomeInput1("Foo");
    var expected = new SomeOutput1(...);

    var calc = new CalculateSomething(...);
    var actual = calc.CalculateSomething1(input);

    Assert.AreEqual(expected.Prop1, actual.Prop1);
    Assert.AreEqual(expected.Prop2, actual.Prop2);
    Assert.AreEqual(expected.Prop3, actual.Prop3);
}

And then, as you add CalculateSomething1_BarInput and CalculateSomething2_FooInput, factor out some common code into helper methods:

[TestMethod]
public void CalculateSomething1_FooInput
{
    var input = new SomeInput1("Foo");
    var expected = new SomeOutput1(...);

    var actual = CreateTestCalculateSomething().CalculateSomething1(input);

    AssertSomeOutput1Equality(expected, actual);
}

Upvotes: 3

Russ W.
Russ W.

Reputation: 48

As far as unit testing is concerned you would have to create the test methods for the functions that you want.

[TestMethod()]
public void CalculateSomething1()
{
    // First we have to define the input for the fucntion
    var input = new SomeInput1(); // Assumes your constructor creates the value for prop1 and prop2.  Change as needed.

    var classToBeTested = new CalculateSomething();
    var output = classToBeTested(input);

    // There are multiple ways to test if the outcome is correct choose the one that is correct for the method/output.
    Assert.IsNotNull(output);
}

The method above would be in a unit test project and associated class file.

Some things to keep in mind when unit testing

  • Unit tests need to be independent
  • Long complicated code should be re-factored down into smaller units of code and tested.
  • Interfaces are an awesome way to remove dependencies. The use of interfaces allows concepts such as mocking. Mocking can be a little complicated at first so take your time when learning it. There are several mocking frameworks out there that can help a lot. i.e. RhinoMocks, Moq just to name a couple.

Upvotes: 1

Related Questions