Assertions

Learn how to use Bellatrix API built-in response assertion methods.

Example

[TestClass]
public class ApiAssertionsTests : APITest
{
    private ApiClientService _apiClientService;

    public override void TestInit()
    {
        _apiClientService = App.GetApiClientService();
    }

    [TestMethod]
    public void AssertSuccessStatusCode()
    {
        var request = new RestRequest("api/Albums");

        var response = _apiClientService.Get(request);

        response.AssertSuccessStatusCode();
    }

    [TestMethod]
    public void AssertStatusCodeOk()
    {
        var request = new RestRequest("api/Albums");

        var response = _apiClientService.Get(request);

        response.AssertStatusCode(HttpStatusCode.OK);
    }

    [TestMethod]
    public void AssertResponseHeaderServerIsEqualToKestrel()
    {
        var request = new RestRequest("api/Albums");

        var response = _apiClientService.Get(request);

        response.AssertResponseHeader("server", "Kestrel");
    }

    [TestMethod]
    public void AssertExecutionTimeUnderIsUnderOneSecond()
    {
        var request = new RestRequest("api/Albums");

        var response = _apiClientService.Get(request);

        response.AssertExecutionTimeUnder(1);
    }

    [TestMethod]
    public async Task AssertContentTypeJson()
    {
        var request = new RestRequest("api/Albums/10");

        var response = await _apiClientService.GetAsync<Albums>(request);

        response.AssertContentType("application/json; charset=utf-8");
    }

    [TestMethod]
    public async Task AssertContentContainsAudioslave()
    {
        var request = new RestRequest("api/Albums/10");

        var response = await _apiClientService.GetAsync<Albums>(request);

        response.AssertContentContains("Audioslave");
    }

    [TestMethod]
    public async Task AssertContentEncodingUtf8()
    {
        var request = new RestRequest("api/Albums/10");

        var response = await _apiClientService.GetAsync<Albums>(request);

        response.AssertContentEncoding("gzip");
    }

    [TestMethod]
    public async Task AssertContentEquals()
    {
        var request = new RestRequest("api/Albums/10");

        var response = await _apiClientService.GetAsync<Albums>(request);

        response.AssertContentEquals("{\"albumId\":10,\"title\":\"Audioslave\",\"artistId\":8,\"artist\":null,\"tracks\":[]}");
    }

    [TestMethod]
    public async Task AssertContentNotContainsRammstein()
    {
        var request = new RestRequest("api/Albums/10");

        var response = await _apiClientService.GetAsync<Albums>(request);

        response.AssertContentNotContains("Rammstein");
    }

    [TestMethod]
    public async Task AssertContentNotEqualsRammstein()
    {
        var request = new RestRequest("api/Albums/10");

        var response = await _apiClientService.GetAsync<Albums>(request);

        response.AssertContentNotEquals("Rammstein");
    }

    [TestMethod]
    public async Task AssertResultEquals()
    {
        var expectedAlbum = new Albums
                            {
                                AlbumId = 10,
                            };
        var request = new RestRequest("api/Albums/10");

        var response = await _apiClientService.GetAsync<Albums>(request);

        response.AssertResultEquals(expectedAlbum);
    }

    [TestMethod]
    public async Task AssertResultNotEquals()
    {
        var expectedAlbum = new Albums
                            {
                                AlbumId = 11,
                            };
        var request = new RestRequest("api/Albums/10");

        var response = await _apiClientService.GetAsync<Albums>(request);

        response.AssertResultNotEquals(expectedAlbum);
    }

    [TestMethod]
    public async Task AssertCookieExists()
    {
        var request = new RestRequest("api/Albums/10");

        var response = await _apiClientService.GetAsync<Albums>(request);

        response.AssertCookieExists("whoIs");
    }

    [TestMethod]
    public async Task AssertCookieWhoIsBella()
    {
        var request = new RestRequest("api/Albums/10");

        var response = await _apiClientService.GetAsync<Albums>(request);

        response.AssertCookie("whoIs", "Bella");
    }
}

Explanations

Bellatrix API library brings many convenient assertion methods on top of RestSharp. Of course, you can write similar methods yourself using MSTest or NUnit. All Bellatrix assertions comes with full BDD logging and extensibility hooks.

response.AssertSuccessStatusCode();

Assert that the status code is successful.

response.AssertStatusCode(HttpStatusCode.OK);

Assert that the status code is OK.

response.AssertResponseHeader("server", "Kestrel");

Assert that the header named ‘server’ has the value ‘Kestrel’.

response.AssertExecutionTimeUnder(1);

Assert that the execution time of the GET request is under 1 second.

response.AssertContentType("application/json; charset=utf-8");

Assert that the content type is of the specified type.

response.AssertContentContains("Audioslave");

Assert that the native text content (JSON or XML) contains the specified value.

response.AssertContentEncoding("gzip");

Assert the response’s content encoding.

response.AssertContentEquals("{\"albumId\":10,\"title\":\"Audioslave\",\"artistId\":8,\"artist\":null,\"tracks\":[]}");

Assert the native text content.

response.AssertContentNotContains("Rammstein");

Assert that the native text content doesn’t contain specific text.

response.AssertContentNotEquals("Rammstein");

Assert that the native text content is not equal to a specific text.

response.AssertResultEquals(expectedAlbum);

Assert C# collections directly.

response.AssertResultNotEquals(expectedAlbum);

Assert response is not equal to an object.

response.AssertCookieExists("whoIs");

Assert that a specific cookie exists.

response.AssertCookie("whoIs", "Bella");

Assert that a cookie’s value is equal to a specific value.