2016-08-31 6 views
1

現在、レールユニットテストを行っています。ミニテストレールを使用しています。編集可能なブートストラップ付きミニテストレール

私はブートストラップ編集可能なjsを使用して、ビュー内のデータを直接更新しています。

値を正しくアサートできない場合は、失敗の結果が出ます。

ブートストラップ編集可能は、通常のRails更新アクションよりもパラメータを送信するために使用されているためです。

私のコードを見てください。私含まれるモジュール

def edit_job_type 
    update_common_table('job_types', params[:pk], params[:name], params[:value]) 
end 

:私minitestレールにおいて

def update_common_table(table, id, key, value) 
    begin 
    case table 
    when 'job_types' 
     @record = JobType.find(id) 
    end 

    case key 
    when 'en_name' 
     @record.en_name = params[:value] 
     edit_field = 'English Name' 
    end 

    @record.last_updated_by = session[:username] 
    @record.save 

    render json: { 
     status: 'success', 
     message: "#{edit_field} was successfully updated.", 
     updated_at: @record.updated_at.to_time.strftime("%a, %e %b %Y %H:%M"), 
     updated_by: session[:username] 
    } 
    rescue => error 
    render json: {status: 'error', message: error.message} 
    end 
end 

コントローラ

コントローラ

器具で

setup do @job_type = job_types(:waiter) end test "should update job_type" do patch :edit_job_type, id: @job_type.id, job_type: { pk: @job_type.id, name: 'en_name', value: "janitor" } assert_response :success, message: 'English Name was successfully updated.' @job_type.reload assert_equal "janitor", @job_type.en_name # this one FAILS, not updated value end 

>をjob_types:

waiter: 
en_name: waiter 

私はすくいテスト実行すると:

I got failure result, because the update was failed. 

    Expected: "New Job Type Updated" 
    Actual: "waiter" 

Still getting the default value "waiter", instead of "janitor" 

を、私は私のテストを固定する方法を把握することを助けてください。

答えて

0

は最後に、私は徹底的に検索した後の周りの作品を作った

を解決しました。

解決策は、ブートストラップ編集可能なのがPOSTメソッドを使用するため、XHRメソッドを使用することでした。

前:後

test "should update job_type" do 
    patch :edit_job_type, id: @job_type.id, job_type: { pk: @job_type.id, name: 'en_name', value: "janitor" } 
    assert_response :success, message: ' Successfully updated.' 
    @job_type.reload 
    assert_equal "janitor", @job_type.en_name # this one FAILS, not updated value 
end 

:このチュートリアルに

test "should update job_type" do 
    xhr :post, :edit_job_type, format: :js, pk: @job_type.id, name: 'en_name', value: "janitor" 
    assert_response :success, ' Successfully updated.' 
    @job_type.reload 
    assert_equal "janitor", @job_type.en_name 
end 

おかげで、Building Rails Test

関連する問題