2017-08-15 6 views
0

コントローラにrspecというテストを書き込もうとしています。コントローラはデータベースレコード数を変更しません

class Restream::CustomsController < Restream::BaseController 
    def create 
    @custom = Restream::Custom.new(custom_params) 
    @custom.user_id = current_user.id 
    @custom.restream = current_user.restreams.find(params[:restream]) 

    if @custom.save 
     @custom.restream.update_attributes!(provider: @custom) 
     redirect_after_create 
    else 
     render 'edit' 
    end 
    end 

    private 
    def custom_params 
    attributes = params.require(:restream_custom).permit(
     :server_url, 
     :key, 
     :admin_url, 
     :view_url 
    ) 

    if attributes[:view_url].present? 
     attributes[:additional_parameters] = { view_url: attributes[:view_url] } 
     attributes.delete(:view_url) 
    end 

    if attributes[:admin_url].present? 
     if attributes[:additional_parameters].present? 
     attributes[:additional_parameters].merge!({ manage_url: attributes[:admin_url] }) 
     else 
     attributes[:additional_parameters] = { manage_url: attributes[:admin_url] } 
     end 
     attributes.delete(:admin_url) 
    end 

    attributes 
    end 

    def redirect_after_create 
    if params[:redirect].present? 
     redirect_to params[:redirect] 
    else 
     redirect_to restreams_path 
    end 
    end 
end 

だから、私はこのの:createアクションをテストしてみてください:今、私はそのようなコードを持っています。ここ は仕様です:

require 'rails_helper' 

describe Restream::CustomsController do 
    let(:restream) { create(:restream) } 
    let(:user)  { restream.user } 

    before do 
    login(user) 
    end 
    describe '#create' do 
    let(:params_hash) { { 
     'server_url' => "rtmp://example.com", 
     'key' =>  "somekey", 
     'admin_url' => "http://example.com/admin_url", 
     'view_url' => "http://example.com/view_url" 
     } } 
    it 'creates provider if params correct' do 
     puts Restream::Custom.find_each.count 
     expect { 
     post :create, { :restream_custom => params_hash, :restream => restream.id } 
     }.to change(Restream::Custom, :count).by 1 
     # Restream::Custom.create(params_hash.merge(:user => user, :restream => restream)) 
     puts Restream::Custom.find_each.count 

     expect(response).to redirect_to restreams_path 
    end 
    end 
end 

テストが失敗したこと。そして、両方のデバッグプリントに00を出力します。 コントローラが動作し、新しいレコードを作成するはずです。 はまた、私は私がこだわって作るデータベース出力、持っている:だから、それはレコードを作成し

D, [2017-08-15T10:38:46.807264 #1] DEBUG -- : SQL (1.1ms) INSERT INTO "restream_services" ("type", "server_url", "key", "additional_parameters", "user_id", "restream_id", "created_at", "updated_at") VALUES ($1, $2, $3, $4, $5, $6, $7, $8) RETURNING "id" [["type", "custom"], ["server_url", "rtmp://example.com"], ["key", "somekey"], ["additional_parameters", "{\"view_url\"=>\"http://example.com/view_url\", \"manage_url\"=>\"http://example.com/admin_url\"}"], ["user_id", 1], ["restream_id", 1], ["created_at", "2017-08-15 10:38:46.803307"], ["updated_at", "2017-08-15 10:38:46.803307"]] 
D, [2017-08-15T10:38:46.810992 #1] DEBUG -- : (0.4ms) RELEASE SAVEPOINT active_record_1 
D, [2017-08-15T10:38:46.811829 #1] DEBUG -- : (0.3ms) SAVEPOINT active_record_1 
D, [2017-08-15T10:38:46.815250 #1] DEBUG -- : Restream::Service Load (0.7ms) SELECT "restream_services".* FROM "restream_services" WHERE "restream_services"."restream_id" = $1 LIMIT 1 [["restream_id", 1]] 
D, [2017-08-15T10:38:46.818478 #1] DEBUG -- : SQL (0.6ms) DELETE FROM "restream_services" WHERE "restream_services"."id" = $1 [["id", 1]] 
D, [2017-08-15T10:38:46.821715 #1] DEBUG -- : Restream::Service Exists (1.0ms) SELECT 1 AS one FROM "restream_services" WHERE ("restream_services"."restream_id" = 1 AND "restream_services"."id" != 1) LIMIT 1 
D, [2017-08-15T10:38:46.827232 #1] DEBUG -- : (0.5ms) RELEASE SAVEPOINT active_record_1 
I, [2017-08-15T10:38:46.828039 #1] INFO -- : Redirected to http://test.host/restreams 
I, [2017-08-15T10:38:46.828454 #1] INFO -- : Completed 302 Found in 51ms (ActiveRecord: 7.8ms) 
D, [2017-08-15T10:38:46.831293 #1] DEBUG -- : Restream::Custom Load (1.0ms) SELECT "restream_services".* FROM "restream_services" WHERE "restream_services"."type" IN ('custom', 'periscope', 'odnoklassniki', 'vkontakte') ORDER BY "restream_services"."id" ASC LIMIT 1000 
D, [2017-08-15T10:38:46.834181 #1] DEBUG -- : (0.6ms) ROLLBACK 

をが、その後ほとんどすぐにそれを削除します。

最初にexpectブロックにコメントし、Restream::Custom.createという文字列をコメントアウトすると、それは動作し、countが変更されます。

なぜこれが起こり、それを正しくテストするために何ができますか?

+0

@ custom.saveを試してください!あなたのコントローラでは、保存が失敗した場合に例外が発生し、なぜより良い結果が得られるのでしょうか? – arieljuod

答えて

0

Restream::Custom.create(params_hash.merge(:user => user, :restream => restream)) を削除してください。テストでcreateに電話する必要はありません。レコードはあなたの:createコントローラーアクションへの要求によって作成されます。テストの要点は、そのアクションへのリクエストをシミュレートし、リクエスト後に何が起こるかをテストすることです。

+0

ご覧のとおり、コメントされています。デバッグ中に追加しました。そして、問題は、要求をシミュレートすることが機能しないことです。 – Ngoral

+0

ああ、申し訳ありません。 'expect'ブロックの' post'も。 –

+0

あなたは、 'expect'ブロックから' post'を移動することを意味しますか?そして、私はどのように '(。)。1で変更するのですか? 'また、私はこれをやろうとしました。どちらのデバッグ・プリントも '0'を出力します。だから、助けにならない。 – Ngoral

関連する問題