错误 CS0246 找不到类型或命名空间名称 'JsonDeserializer' (RestSharp v107)
Error CS0246 The type or namespace name 'JsonDeserializer' could not be found (RestSharp v107)
我在以下行收到错误(更新 RestSharp - v107 后):
var contacts = new JsonDeserializer().Deserialize<List<Contact>>(response);
错误 CS0246 找不到类型或命名空间名称 'JsonDeserializer'(是否缺少 using 指令或程序集引用?)
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).Result;
//or better
var response = this.client.Execute(request);
....
var contacts = JsonSerializer.Deserialize<List<Contact>>(response.Content);
var response = this.client.ExecuteAsync(request);
顾名思义,这个函数returnsTask<RestResponse>
。您需要等待电话。
也无需显式反序列化响应,RestSharp 会为您完成。
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"));
}
我在以下行收到错误(更新 RestSharp - v107 后):
var contacts = new JsonDeserializer().Deserialize<List<Contact>>(response);
错误 CS0246 找不到类型或命名空间名称 'JsonDeserializer'(是否缺少 using 指令或程序集引用?)
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).Result;
//or better
var response = this.client.Execute(request);
....
var contacts = JsonSerializer.Deserialize<List<Contact>>(response.Content);
var response = this.client.ExecuteAsync(request);
顾名思义,这个函数returnsTask<RestResponse>
。您需要等待电话。
也无需显式反序列化响应,RestSharp 会为您完成。
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"));
}