C# Unit Testing in C# Test Driven Development TDD

Challenge Task 4 of 4, Implement the Subtract method so that the test passes. Not passing.

Code not passing, some silly mistake cannot find.

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.Substract(0.2);
        var expected = 0.9;
        Assert.Equal((); target.Result >= 0.9);
    }
}

1 Answer

Jeff Wilton
Jeff Wilton
16,626 Points

There is a typo in the name of the Subtract method. Other than that, the test should look like this:

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

And don't forget that the same spelling in "calculator.js" will also need to be changed.

Thank you.