0

私は4.2.5のレールAPIを持っており、抽出されたレスポンダのおかげでこのエラーが発生すると思います。私はそれを動作させるために何を変えるべきですか?rails api testing jsonのレスポンス

エラー:

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 

UPDATE:

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\":{}}" 
+1

なぜこれを使用しませんか?レンダリングjson:{category:@category} –

+0

steady_daddy、私の質問を更新しました –

+0

ああ、私はばかです。クロージングブラケットは、ラインの最後にある必要があります。 –

答えて

1

Loooksのように:

次のコードでは、アクティブなモデル・シリアライザを使用していません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 

希望します。

+0

hgsongra、thx!2つの短い質問が気になるなら:1.私の更新plsをチェックしてくださいなぜなら、なぜそれがrespond_toなしではたらくのですか?respond_toでないのはなぜですか? –

+0

質問(2)のためのreadout [This](http://api.rubyonrails.org) /classes/ActionController/MimeResponds.html) – hgsongra

+0

'何らかの理由でルートオプションが動作しません。なぜでしょうか?' plsはこれについて詳しく説明します – hgsongra

1

あなたのコードからは、アクティブモデルシリアライザが間違っているようです。 This Railcastthis sitepoint.comの記事では、Railsでシリアライザを正しく使用する方法について説明しています。

+0

それは私がこの行 'render json:@category、serializer:CategorySerializer、root:" category "を使用すると、シリアライザが(ルートノードを除いて)動作していますが、この' render json:{category:@category 、シリアライザ:CategorySerializer} 'そうではありません。 –

+0

2番目のケースでは、シンボルシリアライザは値CategorySerializerを持つキーとして扱われるためです。 render json:{}はJSONifyingデータです。 –

+0

解決策が見つかりました。私は、初期化子を作成し、これを追加しなければなりません: 'ActiveModelSerializers.config.adapter =:json' –

関連する問題