【问题标题】:rails api testing json responserails api测试json响应
【发布时间】:2016-08-06 14:58:25
【问题描述】:

我有一个 4.2.5 rails api,我想由于提取的响应程序,我得到了这个错误。我应该改变什么才能让它工作?

错误:

1) Error:
Api::V1::GraphControllerTest#test_should_get_show:
ActionController::UnknownFormat: ActionController::UnknownFormat
    app/controllers/api/v1/graph_controller.rb:7:in `show'
    test/controllers/api/v1/graph_controller_test.rb:5:in `block in <class:GraphControllerTest>'

控制器

class Api::V1::CategoryController < ApplicationController
  def show
    category = params[:category] || "sports"
    @category = Category.where(cat_title: category.capitalize).first
    respond_to do |format|
      format.json { render json: @category, serializer: CategorySerializer, root: "category" }
    end
  end
end

测试

require 'test_helper'

class Api::V1::CategoryControllerTest < ActionController::TestCase
  test "should get show" do 
    get :show, {'category' => 'science'}, format: :json
    assert_response :success
    assert_equal "{\"category\":{\"title\":\"science\",\"sub_categories\":1}}", @response.body
  end
end

更新:

class Api::V1::CategoryController < ApplicationController
  def show
    category = params[:category] || "sports"
    @category = Category.where(cat_title: category.capitalize).first
    render json: @category, serializer: CategorySerializer, root: "category"
  end
end

这样可以正常工作,但不使用根目录:

--- expected
+++ actual
@@ -1 +1 @@
-"{\"category\":{\"title\":\"science\",\"sub_categories\":1}}"
+"{\"title\":\"science\",\"sub_categories\":1}"

更新2:

以下代码不使用活动模型序列化器:

render json: { category: @category , serializer: CategorySerializer }

-"{\"category\":{\"title\":\"science\",\"sub_categories\":1}}"
+"{\"category\":    {\"cat_id\":2,\"cat_title\":\"Science\",\"cat_pages\":1,\"cat_subcats\":1,\"cat_files\":1,\"created_at\":\"2016-08-06T15:35:29.000Z\",\"updated_at\":\"2016-08-06T15:35:29.000Z\"},\"serializer\":{}}"

【问题讨论】:

  • 你为什么不使用这个?渲染 json:{类别:@category}
  • steady_daddy,我更新了我的问题
  • 啊,我是个白痴。右括号应该在行尾。
  • steady_daddy,再次更新问题。使用{ category: @category } 它不使用序列化程序:(
  • 这里为什么需要序列化器?

标签: ruby-on-rails json unit-testing active-model-serializers


【解决方案1】:

看起来您发出了 http 请求,请在调用时添加标头 Content-Type = application/json 以请求或更改控制器代码以使其正常工作

class Api::V1::CategoryController < ApplicationController
  def show
    category = params[:category] || "sports"
    @category = Category.where(cat_title: category.capitalize).first
    render json: { category: @category }
  end
end

希望对你有帮助

【讨论】:

  • hgsongra,谢谢!如果您不介意,请回答 2 个简短的问题: 1. 请查看我的更新。由于某种原因,root 选项不起作用。你能猜出原因吗? 2. 为什么不带respond_to 不带respond_to 可以?
  • 问题(2)读出This
  • For some reason the root option is not working. Can you guess the why? 将详细说明这一点
  • 再次更新了我的问题
  • 我是个白痴,右括号应该在行尾。
【解决方案2】:

从您的代码看来,您错误地使用了活动模型序列化程序。 This Railcastthis sitepoint.com 文章解释了如何在 Rails 中正确使用序列化程序。

【讨论】:

  • 很奇怪,如果我使用render json: @category, serializer: CategorySerializer, root: "category" 这一行,那么序列化程序正在工作(根节点除外),但使用此render json: {category: @category, serializer: CategorySerializer} 就不行了。
  • 因为在第二种情况下,符号序列化器被视为具有值 CategorySerializer 的键。呈现 json:{} 正在对数据进行 JSON 化处理。
  • 我找到了解决方案。我必须创建和初始化程序,然后添加:ActiveModelSerializers.config.adapter = :json
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-09-19
  • 1970-01-01
  • 1970-01-01
  • 2023-02-07
  • 2012-05-30
  • 1970-01-01
相关资源
最近更新 更多