Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

C# Unit Testing in C# Test Driven Development TDD

evanpavan
evanpavan
5,025 Points

Struggling with the code challenge.

I'm having a tough time in the first exercise of the code challenge stubbing out the actual classes from the given tests.

Right now I'm getting a compilation error that my constructor can't have the same name as it's enclosing class.

Calculator.cs
public class Calculator
{
    public double intialValue;

    //constructor related to the Initialization test method
    public double Calculator(double x)
    {
        intialValue = x;
    }

    //adding method related to the BasicAdd method
    public double Add(double y)
    {
        //adding adds the value passed to the add method to the value the calculator was initialized with
        double total;
        total = y + initialValue;
        return total;        
    }
}
CalculatorTests.cs
using Xunit;

public class CalculatorTests
{
    [Fact]
    public void Initialization()
    {
        var expected = 1.1;
        var target = new Calculator(1.1);
        Assert.Equal(expected, target.Result, 1);
    }

    [Fact]
    public void BasicAdd()
    {
        var target = new Calculator(1.1);
        target.Add(2.2);
        var expected = 3.3;
        Assert.Equal(expected, target.Result, 1);
    }
}
evanpavan
evanpavan
5,025 Points

Steven Parker I've recreated the question with my code. Thanks for the tips of this.

Alan Brown
Alan Brown
20,443 Points

Remove the return type on your constructor.

1 Answer

Steven Parker
Steven Parker
222,348 Points

I see three issues:

  • "initialValue" is spelled "intialValue" in two places
  • constructors should not have a return type (not even "void")
  • the class to be tested must have a "Result" property or field