0

Does anybody understand why the expectation below is ignoring the argument message considering it's being passed as a string?

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

the controller

 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

The warning message

.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.
vinibol12
  • 468
  • 4
  • 21

2 Answers2

1

The second argument is for a custom failure message:

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

But here it looks like you're trying to combine two expectations. For that you would need to use the compound syntax:

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

Andy Waite
  • 10,785
  • 4
  • 33
  • 47
1

This is not a valid Rspec usage. You are assuming that to is capable of handling 2 or more expectations.

You should split them

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

However, your code is also redundant. If the message is equal to a string, it's for sure present. Hence the first expectation is completely useless.

Just add

  expect(flash[:alert]).to eq("Deletion failed. Incident can only be deleted by user who created it!")
Simone Carletti
  • 173,507
  • 49
  • 363
  • 364
  • I agree with you @Simone Carletii. I realised the redundancy of that yesterday. I kept only the second line as you suggested. – vinibol12 Jun 27 '16 at 19:38
  • 'You are assuming that to is capable of handling 2 or more expectations.' Isn't this Compound Expectation going against what you said above? https://www.relishapp.com/rspec/rspec-expectations/v/3-4/docs/compound-expectations. I agree that if there's a string as flash it's consequently present. But in another case where you might need to pass 2 expectations to #to then the syntax above seems to work. – vinibol12 Jun 27 '16 at 19:43
  • The syntax for compound expectations is definitely different than your example. – Simone Carletti Jun 27 '16 at 19:57