A Unit Test in Arrange-Act-Assert Style (xUnit)
csharpC# and .NETinterview-prepintermediatebest-practices
An xUnit test class with two tests, each laid out as Arrange, Act, Assert. Add the xunit package to a test project to run it.
using Xunit;
public class PriceCalculatorTests
{
[Fact]
public void ApplyDiscount_TenPercent_ReducesPrice()
{
// Arrange
var calculator = new PriceCalculator();
// Act
decimal result = calculator.ApplyDiscount(200m, 10);
// Assert
Assert.Equal(180m, result);
}
[Theory]
[InlineData(100, 0, 100)]
[InlineData(100, 100, 0)]
[InlineData(50, 50, 25)]
public void ApplyDiscount_ValidPercent_ReturnsExpected(decimal price, int percent, decimal expected)
{
var calculator = new PriceCalculator();
Assert.Equal(expected, calculator.ApplyDiscount(price, percent));
}
}
public class PriceCalculator
{
public decimal ApplyDiscount(decimal price, int percent) => price - price * percent / 100m;
}
// Run with: dotnet test
// Test names follow Method_Scenario_ExpectedResult. Tests must be fast, isolated and deterministic.