Assertions - adamralph/xbehave.net GitHub Wiki

Assertions can be written using xUnit.net assertions or the library of your choice (our recommendation is FluentAssertions, which has some better documentation).

Examples

Using xUnit.net assertion

// Simple assertion
[Scenario]
public void CreatingARequest(string url, HttpWebRequest request)
{
    "Given a valid HTTP URL"
        .x(() => url = "http://www.example.com");

    "When I create an HTTP web request"
        .x(() => request = (HttpWebRequest)WebRequest.Create(url));

    "Then the user agent is null"
        .x(() => Assert.Null(request.UserAgent));
}

// Expected exception assertion
[Scenario]
public void UsingAnEmptyUrl(string url, Exception ex)
{
    "Given an empty URL string"
        .x(() => url = String.Empty);

    "When create a web request"
        .x(() => ex = Record.Exception(() => WebRequest.Create(url)));

    "Then a UriFormatException is thrown"
        .x(() => Assert.IsType<UriFormatException>(ex));
}

Using FluentAssertions assertion


using FluentAssertions;

// Simple assertion
[Scenario]
public void CreatingARequest(string url, HttpWebRequest request)
{
    "Given a valid HTTP URL"
        .x(() => url = "http://www.example.com");

    "When I create an HTTP web request"
        .x(() => request = (HttpWebRequest)WebRequest.Create(url));

    "Then the user agent is null"
        .x(() => request.UserAgent.Should().BeNull());
}

// Expected exception assertion
[Scenario]
public void UsingAnEmptyUrl(string url, Exception ex)
{
    "Given an empty URL string"
        .x(() => url = String.Empty);

    "When I add a site"
        .x(() => ex = Record.Exception(() => WebRequest.Create(url)));

    "Then a UriFormatException is thrown"
        .x(() => ex.Should().BeOfType<UriFormatException>());
}