2016-06-27 20 views
0

文字列として渡されたと考えられるため、以下の期待が引数メッセージを無視している理由を誰かが理解していますか?メッセージの引数を無視するRSpecの期待

spec.rb

context 'flash' do 
    context 'fail' do 
    # Changes the admin_user so it has no permission to delete offence 
    it 'flash[:alert]' do 
     admin_user = FactoryGirl.create :admin_user 
     sign_in admin_user 
     delete :destroy, :id => offence.id, :customer_id => offence.job.customer.id 
     expect(flash[:alert]).to(be_present, eq("Deletion failed. Incident can only be deleted by user who created it!")) 
    end 
    end 
end 

コントローラ

def destroy 
    @offence = Offence.find(params[:id]) 
    @events = Event.where(sub_type: 'offence').where("parent_data->> 'id' = ?", @offence.id.to_s) 
    if @offence.admin_user == current_user 
    ActiveRecord::Base.transaction do 
    @events.each do |event| 
     event.destroy! 
    end 
    @offence.destroy! 
    redirect_to admin_customer_offences_path(@customer), notice: 'Incident deleted successfully' 
    end 
    else 
    redirect_to admin_customer_offences_path(@customer), alert: 'Deletion failed. Incident can only be deleted by user who created it!' 
    end 
end 

警告メッセージ

.WARNING: ignoring the provided expectation message argument (#<RSpec::Matchers::BuiltIn::Eq:0x007fac42c5d4b0 @expected="Incident deleted successfully">) since it is not a string or a proc. 
+0

追加しますか? –

+0

それはうまく動作します。これが有効な構文ではないのか、その理由を知りたかっただけです。しかし、今のところそのオプションに行く。 THanks – vinibol12

答えて

1

ではありません。あなたはtoが2つ以上の期待を処理できると仮定しています。

あなたは彼らが

expect(flash[:alert]).to be_present 
    expect(flash[:alert]).to eq("Deletion failed. Incident can only be deleted by user who created it!") 

しかし、あなたのコードは、冗長である分割しなければなりません。メッセージが文字列と等しい場合、それは確かに存在します。したがって、最初の期待はまったく役に立たない。 be_presentをして.EQ:

ちょうどあなたが2つのを期待に分割ならば、それは同じである

expect(flash[:alert]).to eq("Deletion failed. Incident can only be deleted by user who created it!") 
+0

あなたは@ Simone Carletiiに同意します。私はその冗長性を昨日実現しました。私はあなたが示唆したように2行目だけを保持しました。 – vinibol12

+0

'あなたはtoが2つ以上の期待を処理できると仮定しています。この複合的な期待はあなたが上で言ったことに逆らっていませんか? https://www.relishapp.com/rspec/rspec-expectations/v/3-4/docs/compound-expectations。私は、それが結果として存在するフラッシュとして文字列がある場合、私は同意する。しかし、2つの期待を#に渡す必要がある別のケースでは、上記の構文がうまくいくように見えます。 – vinibol12

+0

化合物の期待値の構文は、あなたの例とはまったく異なります。 –

1

第二引数は、カスタムエラーメッセージのためのものである:

https://www.relishapp.com/rspec/rspec-expectations/v/3-4/docs/customized-message

しかし、ここでは2つの期待を組み合わせようとしているようです。そのために、あなたは、化合物の構文を使用する必要があります:これは、有効なRSpecの使用状況

https://www.relishapp.com/rspec/rspec-expectations/v/3-4/docs/compound-expectations

関連する問題