【问题标题】:integration testing api having fluent validation具有流畅验证的集成测试api
【发布时间】:2017-05-12 17:11:27
【问题描述】:

我有使用 FluentValidations 的 API。

我正在编写集成测试并想断言错误的请求响应包含错误字段名称和消息。我想检查字段名和消息,并确保它们与从 fluentvalidations 返回的相同。我收到一个充满验证错误的 json 响应,但不确定我应该反序列化到哪个对象。

response.StatusCode.Should().Be(HttpStatusCode.BadRequest);

var result = JsonConvert.DeserializeObject<?>(await response.Content.ReadAsStringAsync());
result.Should().BeOfType<?>();
result.Should().NotBeNull();
result.Should().HaveCount(something);

示例响应是 fluentvalidation 响应

{
  "Name": [
    "Name is required.",
    "Name length cannot be more that 255 chars"
  ],
  "ListTypeId": [
    "Invalid listtypeid"
  ],
  "PartyRoleId": [
    "Invalid partyroleid"
  ]
}

【问题讨论】:

  • 举个例子说明json响应内容是什么样子的
  • 已更新。它来自 fluentvalidation 的基本原始响应

标签: c# asp.net-web-api2 fluentvalidation


【解决方案1】:

鉴于提供的 JSON 示例,IDictionary&lt;string,string[]&gt; 应该能够满足该模型

response.StatusCode.Should().Be(HttpStatusCode.BadRequest);
var json = await response.Content.ReadAsStringAsync();
var result = JsonConvert.DeserializeObject<IDictionary<string,string[]>>(json);
result.Should().BeOfType<IDictionary<string,string[]>>();
result.Should().NotBeNull();
result.Should().HaveCount(something);

【讨论】:

  • 啊。非常感谢..我很抱歉我这么早就放弃了..我应该弄清楚的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-02-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多