【问题标题】:Rspec request Active Model Serializer response doesn't contain root keyRspec 请求 Active Model Serializer 响应不包含根密钥
【发布时间】:2016-07-06 15:59:53
【问题描述】:

我正在尝试通过 JSON 适配器测试使用活动模型序列化程序 gem(版本 0.10.2)构建的 JSON 端点(Rails 5)。

当我从 http://localhost:3000/api/posts 发出请求时,我会在响应中获得根节点,使其看起来像文档:

{
  "posts": [{
    "id": 1,
    "title": "Awesome Post Tile",
    "content": "Post content"
  }]
}

但是当我像这样在 Rspec(3.5 版)中提出请求时:

获取'/api/posts'

根节点没有通过,所以响应体如下所示:

[{
   "id": 1,
   "title": "Awesome Post Tile",
   "content": "Post content"
}]

我的适配器正在初始化器中设置:

ActiveModelSerializers.config.adapter = :json

我在这里做错了吗?我真的不太了解这是 AMS 或 Rspec 的问题还是我的设置问题。

【问题讨论】:

    标签: rspec rspec-rails active-model-serializers ruby-on-rails-5


    【解决方案1】:

    听起来您可能为开发环境启用了include_root_in_json,但没有为测试环境启用?

    http://api.rubyonrails.org/classes/ActiveModel/Serializers/JSON.html

    【讨论】:

    • 我检查了项目源代码,找不到任何我使用过 include_root_in_json 的实例。
    猜你喜欢
    • 1970-01-01
    • 2012-02-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多