I get an error (after update RestSharp - v107) on the following line:
var contacts = new JsonDeserializer().Deserialize<List<Contact>>(response);
Error CS0246 The type or namespace name 'JsonDeserializer' could not be found (are you missing a using directive or an assembly reference?)
using NUnit.Framework;
using RestSharp;
using System.Text.Json;
using System;
using System.Collections.Generic;
using System.Net;
namespace ContactBook.ApiTests
{
public class ContactBookApiTests
{
const string ApiBaseUrl = "URl/api";
RestClient client;
[SetUp]
public void Setup()
{
this.client = new RestClient(ApiBaseUrl);
}
[Test]
public void Test_ListContacts_CheckForSteveJobs()
{
// Arrange
var request = new RestRequest("/contacts", Method.Get);
// Act
var response = this.client.ExecuteAsync(request);
// Assert
Assert.That(response.Status, Is.EqualTo(HttpStatusCode.OK));
var contacts = new JsonDeserializer().Deserialize<List<Contact>>(response);
Assert.That(contacts.Count, Is.GreaterThan(0));
var firstContact = contacts[0];
Assert.That(firstContact.firstName, Is.EqualTo("Steve"));
Assert.That(firstContact.lastName, Is.EqualTo("Jobs"));
}
}
var response = this.client.ExecuteAsync(request);
As the name suggests, this function returns Task<RestResponse>
. You need to await the call.
There's also no need to explicitly deserialize the response, RestSharp will do it for you.
public async Task Test_ListContacts_CheckForSteveJobs() {
var request = new RestRequest("/contacts", Method.Get);
var response = await client.ExecuteAsync<Contact[]>(request);
var contacts = response.Data;
// Assert
Assert.That(response.Status, Is.EqualTo(HttpStatusCode.OK));
Assert.That(contacts.Count, Is.GreaterThan(0));
var firstContact = contacts[0];
Assert.That(firstContact.firstName, Is.EqualTo("Steve"));
Assert.That(firstContact.lastName, Is.EqualTo("Jobs"));
}