C# FoundationsยทPractice drills 7

Drill 17: Find the core line โ€” Testing with xUnit

Point to the line that does the main work and explain why.

Beginner 5 min read Lesson 175 of 300 Challenge included
What you will be able to do
  • Point to the line that does the main work and explain why.

Practice task

Point to the line that does the main work and explain why.

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

Point to the line that does the main work and explain why.

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 175 of 300 ยท 0% of course