C# Unit Testing in C# Test Driven Development TDD

Katrin Nitsche
Katrin Nitsche
23,462 Points

Unit Testing in C# - Test Driven Development (Challenge Task 4 of 4)

I don't know how to pass this. I think is because the numbers are not equal like 0.900001 and 0.900002. I don't know how to fix. Could somebody give me a hand, please?

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(expected, target.Result);
    }
}

1 Answer

James Churchill
STAFF
James Churchill
Treehouse Teacher

Katrin,

You're on the right track!

xUnit's Assert.Equal has an overload with the following method signature:

void Assert.Equal(double expected, double actual, int precision)

This method "verifies that two double values are equal, within the number of decimal places given by precision".

Once you get past that issue, the code challenge runner will complain that task 3 isn't passing anymore. In order to resolve that issue I had to rename your Substract method to Subtract.

I hope this helps :)

Thanks ~James

Katrin Nitsche
Katrin Nitsche
23,462 Points

Hello James,

Thank you very much. It worked. Would a little typo can do to you. :-(

Katrin