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

Lone Nielsen
Lone Nielsen
13,764 Points

need help.. Can't find a solution

Implement the Subtract method so that the test passes.

Calculator.cs
public class Calculator
{
    public double Result;

    public Calculator(double number)
    {
        Result = number;
    }     

    public void Add(double number)
    {
        Result += number;
    }

    public void Substract(double number)
    {
        Result -= number;
    }
}
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);
    }

    [Fact]
    public void BasicSubtract()
    {
        var target = new Calculator(1.1);
        target.Subtract(0.2);
        var expected = 0.9;
        Assert.Equal(expected, target.Result, 1);
    }
}

2 Answers

Steven Parker
Steven Parker
220,925 Points

You're really close, but you you have a little spelling/typo error.

You have "Substract" (with an extra "s") instead of "Subtract".

Carel Du Plessis
PLUS
Carel Du Plessis
Courses Plus Student 16,356 Points

Can someone please explain what the last parameter (1) does in this piece of code Assert.Equal(expected, target.Result, 1);.

I have looked at the documentation and found only two object passed in as the parameter.

https://docs.microsoft.com/en-us/dotnet/api/microsoft.visualstudio.testtools.unittesting.assert.equals?view=mstest-net-1.2.0