NUnit

前情提要

以下範例是 .NET Core 6.0.0 的範例

需要額外下載套件

Moq (用來做 mockito 的相關行為)

FluentAssertions (用來提升可讀性)

單元測試

硬斷言

[Test]
public void Hard_Assertion()
{
    // Arrange
    string str = "123";
    string expected = "2";

    // Act
    string actual = str.Substring(1, 1);

    // Assert
    // 原生寫法
    Assert.AreEqual(actual, expected);
    // 使用 FluentAssertions 提升可讀性
    actual.Should().Be(expected);
}

軟斷言

驗證 Exception

API 測試

參考資料:(https://www.ruyut.com/2023/04/nunit-aspnet-core6-api-integration-testing.html)

*還有重構空間,此處僅是API測試的範例,可讀性方面請參考單元測試部分

Last updated

Was this helpful?