microsoft / dotnet

This repo is the official home of .NET on GitHub. It's a great starting point to find many .NET OSS projects from Microsoft and the community, including many that are part of the .NET Foundation.
https://devblogs.microsoft.com/dotnet/
MIT License
14.34k stars 2.21k forks source link

Add NFluent to the .NET Open Source Developer Projects #1310

Closed blabaere closed 1 year ago

blabaere commented 3 years ago

NFluent is a fluent assertion library with crystal-clear error messages.

Apollo9999 commented 1 year ago

NFluent is a fluent assertion library for .NET that provides an expressive and readable way to write assertions in your tests or code. It aims to provide clear error messages to aid in understanding the cause of test failures. Here's how you can define NFluent:

Install NFluent: You can install NFluent via NuGet package manager in Visual Studio or using the .NET CLI. Search for "NFluent" and install the appropriate package for your project.

Import NFluent namespace: In your C# code file, add the following using directive at the top to import the NFluent namespace:

Writing assertions: NFluent provides a fluent interface for writing assertions. You can use various methods and chaining to express your assertions in a more readable and concise way. NFluent offers a wide range of assertion methods to check for equality, inequality, nullity, collections, strings, exceptions, and more. You can chain multiple assertions together to form more complex assertions.

Error messages: One of the key features of NFluent is its clear error messages. When an assertion fails, NFluent provides detailed information about the expected and actual values, making it easier to diagnose the issue. Here's an example of an NFluent error message:

using NFluent; using Xunit;

public class SampleTests { [Fact] public void TestNumberEquality() { int number = 42;

    Check.That(number).IsEqualTo(42);
}

[Fact]
public void TestStringContainment()
{
    string message = "Hello, World!";

    Check.That(message).Contains("Hello");
    Check.That(message).DoesNotContain("Goodbye");
}

[Fact]
public void TestCollection()
{
    int[] numbers = { 1, 2, 3, 4, 5 };

    Check.That(numbers).Contains(3);
    Check.That(numbers).Not.Contains(6);
    Check.That(numbers).IsOnlyMadeOf(1, 2, 3, 4, 5);
}

} we have a test class named SampleTests with three test methods decorated with the [Fact] attribute from xUnit.net. Each test method demonstrates different types of assertions using NFluent.

The first test method, TestNumberEquality, asserts that the variable number is equal to 42.

The second test method, TestStringContainment, asserts that the message string contains the substring "Hello" and does not contain the substring "Goodbye".

The third test method, TestCollection, performs assertions on an array of integers. It checks if the array contains the value 3, does not contain the value 6, and is only made up of the specified values.

To run these tests, you'll need to have xUnit.net and NFluent installed in your project. You can use the test runner of your choice (such as Visual Studio Test Explorer) to execute the tests and see the test results, including any assertion failures.

Make sure to install the NFluent package via NuGet in your project to resolve the NFluent namespace and its assertion method

Apollo9999 commented 1 year ago

NFluent is a fluent assertion library for .NET that provides an expressive and readable way to write assertions in your tests or code. It aims to provide clear error messages to aid in understanding the cause of test failures. Here's how you can define NFluent:

Install NFluent: You can install NFluent via NuGet package manager in Visual Studio or using the .NET CLI. Search for "NFluent" and install the appropriate package for your project.

Import NFluent namespace: In your C# code file, add the following using directive at the top to import the NFluent namespace:

Writing assertions: NFluent provides a fluent interface for writing assertions. You can use various methods and chaining to express your assertions in a more readable and concise way. NFluent offers a wide range of assertion methods to check for equality, inequality, nullity, collections, strings, exceptions, and more. You can chain multiple assertions together to form more complex assertions.

Error messages: One of the key features of NFluent is its clear error messages. When an assertion fails, NFluent provides detailed information about the expected and actual values, making it easier to diagnose the issue. Here's an example of an NFluent error message:

using NFluent; using Xunit;

public class SampleTests { [Fact] public void TestNumberEquality() { int number = 42;

    Check.That(number).IsEqualTo(42);
}

[Fact]
public void TestStringContainment()
{
    string message = "Hello, World!";

    Check.That(message).Contains("Hello");
    Check.That(message).DoesNotContain("Goodbye");
}

[Fact]
public void TestCollection()
{
    int[] numbers = { 1, 2, 3, 4, 5 };

    Check.That(numbers).Contains(3);
    Check.That(numbers).Not.Contains(6);
    Check.That(numbers).IsOnlyMadeOf(1, 2, 3, 4, 5);
}

} we have a test class named SampleTests with three test methods decorated with the [Fact] attribute from xUnit.net. Each test method demonstrates different types of assertions using NFluent.

The first test method, TestNumberEquality, asserts that the variable number is equal to 42.

The second test method, TestStringContainment, asserts that the message string contains the substring "Hello" and does not contain the substring "Goodbye".

The third test method, TestCollection, performs assertions on an array of integers. It checks if the array contains the value 3, does not contain the value 6, and is only made up of the specified values.

To run these tests, you'll need to have xUnit.net and NFluent installed in your project. You can use the test runner of your choice (such as Visual Studio Test Explorer) to execute the tests and see the test results, including any assertion failures.

Make sure to install the NFluent package via NuGet in your project to resolve the NFluent namespace and its assertion method