C# FoundationsยทPractice drills 5

Drill 12: Teach it โ€” Testing with xUnit

Explain the example to someone who has never seen it.

Beginner 5 min read Lesson 125 of 300 Challenge included
What you will be able to do
  • Explain the example to someone who has never seen it.

Practice task

Explain the example to someone who has never seen it.

CalculatorTests.csC#
using Xunit;

public class Calculator
{
    public int Add(int a, int b) => a + b;

    public int Divide(int a, int b)
    {
        if (b == 0) throw new ArgumentException("Cannot divide by zero");
        return a / b;
    }
}

public class CalculatorTests
{
    private readonly Calculator calculator = new Calculator();

    [Fact]
    public void Add_ReturnsSum()
    {
        var result = calculator.Add(2, 3);
        Assert.Equal(5, result);
    }

    [Theory]
    [InlineData(6, 3, 2)]
    [InlineData(9, 3, 3)]
    public void Divide_ReturnsQuotient(int a, int b, int expected)
    {
        Assert.Equal(expected, calculator.Divide(a, b));
    }

    [Fact]
    public void Divide_ByZero_Throws()
    {
        Assert.Throws<ArgumentException>(() => calculator.Divide(1, 0));
    }
}
Your turn

Write your answer

Easy

Explain the example to someone who has never seen it.

TEXT ยท starter
Your answerโ€ฆ
Study tools

Make it yours.

Keep a private note for this lesson, then turn the key ideas into flashcards you can revisit.

Sign in to save notes and flashcards.Both are private to your account and stay with your progress.
Sign in
Finished reading?

Lock in what you just learned.

Mark this lesson complete to save your place and move one step closer to the certificate.

Lesson 125 of 300 ยท 0% of course