2015-11-07 39 views
1

我在編寫代碼讓我的Rspec測試通過我的api。我正在使用apipie gem來生成文檔,而且似乎我的測試失敗了,因爲您期待一個數字,這很有趣,因爲這正是我想要測試的。 當:bpm參數不是數字時頁面失敗。有什麼方法可以解決這個問題嗎?apipie gem和rspec在rails 4中的問題

context "when is not created" do 
    before(:each) do 
    user = FactoryGirl.create :user 
    @invalid_lesson_attributes = { title: "California Dreamin", 
            bpm: "Hello" 

    } 
    request.headers['Authorization'] = user.auth_token 
    post :create, { user_id: user.id, lesson: @invalid_lesson_attributes } 
    end 


    it "renders an errors json" do 
    lesson_response = json_response 
    expect(lesson_response).to have_key(:errors) 
    end 
    it "renders the json errors on why the user could not be created" do 
    lesson_response = json_response 
    expect(lesson_response[:errors][:bpm]).to include "is not a number" 
    end 

    it { should respond_with 422 } 

    end 
end 

更新規格:

context "when is not updated" do 
    before(:each) do 
    patch :update, { user_id: @user.id, id: @lesson.id, 
        lesson: { bpm: "ten" }, format: :json } 
    end 

    it "renders an errors json" do 
    lesson_response = json_response 
    expect(lesson_response).to have_key(:errors) 
    end 

    it "renders the json errors on why the user could not be updated" do 
    lesson_response = json_response 
    expect(lesson_response[:errors][:bpm]).to include "is not a number" 
    end 

    it { should respond_with 422 } 
end 
在我users_controller

api :POST, '/teachers/:user_id/lessons/', "Create lesson" 
param :lesson, Hash, desc: 'Lesson information', :required => true do 
    param :title, String, desc: 'Title of the lesson', :required => true 
    param :bpm, :number, desc: 'tempo of the lesson (beats per second)', :required => true 

end 
error :code => 422, :desc => "Unprocessable Entity" 

我的錯誤,當我跑我的RSpec的測試:

Apipie::ParamInvalid: Invalid parameter 'bpm' value "Hello": Must be a number. 

回答

0

添加格式JSON來POST請求

post :create, { user_id: user.id, lesson: @invalid_lesson_attributes, format: :json } 

這對我有效。

+0

適用於:創建但不適用於:更新。我編輯了我的代碼以顯示更新。有什麼建議麼 ? – Snake

+0

我使用了方法更新而不是補丁 'put:update,{id:event [:id],event:event_attributes,format :: json}'爲我工作 – German