类型定义应以'{'开头,期望序列化类型为'ErrorResponse',字符串开头为:MOCK FOR URL NOT FOUND

时间:2018-06-28 14:35:26

标签: c# servicestack moq xunit

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

我可以对GET路由进行单元测试,以使用一个类,但是对于另一个类,它拒绝工作。我正在按照以下路线添加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:“未找到”

     

内部异常

     

SerializationException:类型定义应以'{'开头,期望序列化的类型为'ErrorResponse',字符串开头为:MOCK FOR URL NOT FOUND

我为一个更简单的类做了几乎相同的设置,并且可以工作,但是该类拒绝正确的单元测试。在本地运行时,它运行正常。

1 个答案:

答案 0 :(得分:2)

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