gpt4 book ai didi

c# - 类型定义应以 '{' 开头,期望序列化类型 'ErrorResponse' ,得到以 : MOCK FOR URL NOT FOUND 开头的字符串

转载 作者:行者123 更新时间:2023-11-30 16:40:35 24 4
gpt4 key购买 nike

我正在使用 XunitMoqServiceStack 微服务上编写单元测试。

我可以获得 GET 路由的单元测试以用于一个类,但对于不同的类它拒绝工作。我正在这条 route 最小化 ServiceStack.JsonHttpClient.GET:

public SampleResponse Get(SampleRequest request)
{
var myServiceRequest = new SampleService.myRequest() { PrimaryId = request.PrimaryId };
using (client = ClientFactory.CreateSampleService()) // client is extension of ServiceStack.JsonHttpClient
{
// This code runs on local but throws the exception with unit testing
SampleService.myResponse myResponse = client.Get(myServiceRequest);
...
}
}

这是测试:

// Arrange
var myServiceRequest = new SampleService.myRequest { PrimaryId = 1234 };
var myServiceResponse = new SampleService.myResponse { ... };

// assigning the request with the desired response
mockService.MockGet(myServiceRequest, myServiceResponse);

// Act
var request = new ServiceModel.SampleRequest { PrimaryId = 1234 };
// This calls the above function and fails
var response = frontEndService.Get(request);

// Assert
Assert.NotEmpty(response.Items);
Assert.Equal(3, response.Items.ANumber);

但是我得到了这个异常和 InnerException:

ServiceStack.WebException: 'Not Found'

Inner Exception

SerializationException: Type definitions should start with a '{', expecting serialized type 'ErrorResponse', got string starting with: MOCK FOR URL NOT FOUND

我为一个更简单的类做了几乎相同的设置并且它有效,但是这个类拒绝正确地进行单元测试。它在本地运行时运行良好。

最佳答案

当 JSON 与您尝试反序列化的类型不匹配时,会发生此异常。

关于c# - 类型定义应以 '{' 开头,期望序列化类型 'ErrorResponse' ,得到以 : MOCK FOR URL NOT FOUND 开头的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51085586/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com