Lädt...


🔧 Writing XUnit Tests without Object Arrays


Nachrichtenbereich: 🔧 Programmierung
🔗 Quelle: dev.to

Introduction

In unit testing with XUnit, we often use object arrays for test data. However, managing complex test data can quickly become cumbersome and hard to read. In this post, we’ll explore how to write cleaner and more scalable XUnit tests using TheoryData<T> without relying on object[] arrays.

Example Scenario: Retrieving Food by Type

Let's implement a FoodService class with a method that retrieves food items by type.

public enum FoodType { Fruit, Fast }

public interface IFoodService
{
    public IEnumerable<string> GetByType(FoodType type);
}

public class FoodService : IFoodService
{
    public IEnumerable<string> GetByType(FoodType type) => type switch
    {
        FoodType.Fruit => ["Apple", "Banana"],
        FoodType.Fast => ["Pizza", "Burger"],
        _ => throw new NotImplementedException()
    };
}

Here, we use TheoryData<T> to hold our test cases, defining a FoodTestData record that includes input parameters, expected results, and a custom name for each test case:

public class FoodTests
{
    // override ToString method allow us to specify name for individual tests in TheoryData<T>
    public record FoodTestData(FoodType Type, IEnumerable<string> Expected, string TestName)
    {
        public override string ToString() => TestName;
    }

    // this method has the input parameters and expected values. we can scale this method with more tests without changes the test itself.
    public static TheoryData<FoodTestData> GetFoodTestData() => [
        new(Type: FoodType.Fruit, Expected: ["Apple","Banana"], TestName: "Test should return expected fruits"),
        new(Type: FoodType.Fast, Expected: ["Pizza","Burger"], TestName: "Test should return expected fast food")
    ];

    [Theory]
    [MemberData(nameof(GetFoodTestData))]
    public void ShouldReturnExpectedFood(FoodTestData foodTestData)
    {
        // Arrange
        var mockFoodService = new Mock<IFoodService>();
        mockFoodService
            .Setup(x => x.GetByType(FoodType.Fruit))
            .Returns(["Apple", "Banana"]);

        // Act
        var food = new FoodService().GetByType(foodTestData.Type);

        // Assert
        Assert.Equal(foodTestData.Expected, food);
    }
}

Output

Image description

Conclusion

Using TheoryData<T> with a named record keeps our tests clean, easy to read, and scalable for future test cases.

Thank you for reading.

...

🔧 Writing XUnit Tests without Object Arrays


📈 72.85 Punkte
🔧 Programmierung

🔧 C# | Unit Tests with xUnit and Complex Inline Data Object


📈 41.54 Punkte
🔧 Programmierung

🔧 Automating unit tests with xUnit on GitHub Actions


📈 33.32 Punkte
🔧 Programmierung

🔧 Streamlining Your Tests with IClassFixture in xUnit


📈 33.32 Punkte
🔧 Programmierung

🔧 The Ultimate Guide to Unit Testing in .NET (C#) Using xUnit and Moq (Without the Boring Examples)


📈 31.76 Punkte
🔧 Programmierung

🔧 Arrays.mismatch() and Arrays.compare() in Java


📈 27.76 Punkte
🔧 Programmierung

🔧 JUnit Tests in Java: A Guide to Writing Effective Unit Tests


📈 27.7 Punkte
🔧 Programmierung

🔧 Why JavaScript Says "[object Object]" and Not Just "[object]" 🤔


📈 24.65 Punkte
🔧 Programmierung

🕵️ CVE-2022-44108 | pdftojson 94204bb Object.cc Object::copy(Object*) stack-based overflow


📈 24.65 Punkte
🕵️ Sicherheitslücken

🔧 From Bug Fixes to Best Practices: My Open Source Contributions to ChatCraft and xUnit


📈 24.61 Punkte
🔧 Programmierung

🔧 Unit testing with Xunit: Introduction


📈 24.61 Punkte
🔧 Programmierung

🔧 NUnit vs xUnit vs MSTest: Choosing the Best Unit Testing Framework


📈 24.61 Punkte
🔧 Programmierung

🔧 Ensuring Quality and Reliability with xUnit: Best Practices for Automated Testing in .NET Core


📈 24.61 Punkte
🔧 Programmierung

🔧 C# | TDD Example using xUnit and Moq


📈 24.61 Punkte
🔧 Programmierung

🔧 XUnit In ASP.NET Core – What You Need To Know To Start


📈 24.61 Punkte
🔧 Programmierung

🔧 Harnessing Class Fixtures in xUnit


📈 24.61 Punkte
🔧 Programmierung

🔧 Testing with the xUnit Framework - Overview (2 of 12) | Automated Software Testing


📈 24.61 Punkte
🔧 Programmierung

🔧 Testing with the xUnit Framework - More Assertions (4 of 12) | Automated Software Testing


📈 24.61 Punkte
🔧 Programmierung

🔧 Testing with the xUnit Framework - Theories and Assertions (3 of 12) | Automated Software Testing


📈 24.61 Punkte
🔧 Programmierung

🔧 Mastering JavaScript Objects: From Singletons to Object Arrays 🎯Part 2


📈 22.1 Punkte
🔧 Programmierung

🔧 Copying Arrays and Objects in JavaScript Without References


📈 21.03 Punkte
🔧 Programmierung

🔧 Started Out Writing About Algorithms - Somehow Ended Up Writing Something Totally Different


📈 20.55 Punkte
🔧 Programmierung

📰 10 Best AI Writing Tools (2024): Enhance Your Writing with AI Magic


📈 20.55 Punkte
📰 IT Nachrichten

🐧 Comparing the writing speeds of ISO writing tools


📈 20.55 Punkte
🐧 Linux Tipps

🎥 Can an AI writing assistant match my personal writing style?


📈 20.55 Punkte
🎥 Videos

🔧 5 Content Writing Tools to Improve your Content Writing Skills


📈 20.55 Punkte
🔧 Programmierung

🔧 What is Content Writing? 10 Best Tips for Great Content Writing


📈 20.55 Punkte
🔧 Programmierung

🔧 Writing Visual Studio Extensions with Mads - Writing your first extension


📈 20.55 Punkte
🔧 Programmierung

🔧 Writing Visual Studio Extensions with Mads - Writing JSON Schemas


📈 20.55 Punkte
🔧 Programmierung

📰 Writing Cybersecurity Articles – Setting Up Your Writing Process


📈 20.55 Punkte
📰 IT Security Nachrichten

🔧 Writing Good Automated Tests with AI: A Case Study


📈 18.99 Punkte
🔧 Programmierung

matomo