Using FluentAssertions in dotnet core unit tests

Posted by Anuraj on Thursday, December 1, 2016

dotnet core FluentAssertions xUnit

This post is about using FluentAssertions in xUnit unit tests. Fluent Assertions is a set of .NET extension methods that allow you to more naturally specify the expected outcome of a TDD or BDD-style test. It has better support for exceptions and some other features that improves readability and makes it easier to write unit tests. In this post I am using ASPNET Yo man generator to create unit tests. And I have added FluentAssertions reference via project.json file.

Here is the project.json file.

{
    "version": "1.0.0-*",
    "testRunner": "xunit",
    "dependencies": {
        "dotnet-test-xunit": "2.2.0-preview2-build1029",
        "xunit": "2.2.0-beta2-build3300",
        "FluentAssertions" : "4.17.0"
    },
    "frameworks": {
        "netcoreapp1.0": {
            "dependencies": {
                "Microsoft.NETCore.App": {
                    "type": "platform",
                    "version": "1.0.1"
                }
            }
        }
    },
    "buildOptions": {
        "copyToOutput": {
            "include": [ "xunit.runner.json" ]
        }
    },
    "tooling": {
        "defaultNamespace": "UnitTest"
    }
}

Now in the unit test code, I have added two more unit test from FluentAssertions home page.

[Fact]
public void StringTest()
{
    string actual = "ABCDEFGHI";
    actual.Should().StartWith("AB").And.EndWith("HI").And.Contain("EF").And.HaveLength(9);
}

[Fact]
public void CollectionTest()
{
    var collection = new[] { 1, 2, 3 };
    collection.Should().HaveCount(4, "because we thought we put three items in the collection");
}

The first test is to verify that a string begins, ends and contains a particular phrase. And the second one is to verify that a collection contains a specified number of elements and that all elements match a predicate.

Now you can do a dotnet restore command to restore the dependencies and run the unit tests with dotnet test command. And here is the results.

XUnit with FluentAssertions

Happy Programming :)


Similar Posts


Did you like this article? Share it with your friends

Facebook Twitter Google+ LinkedIn Reddit StumbleUpon



MVP
Subscribe
Archives


Copyright © 2017 - Anuraj P. Blog content licensed under the Creative Commons CC BY 2.5 | Unless otherwise stated or granted, code samples licensed under the MIT license. This is a personal blog. The opinions expressed here represent my own and not those of my employer. Hosted with ❤ by GitHub