RSpec期望忽略消息参数

时间:2016-06-27 09:04:25

标签: ruby-on-rails ruby rspec rspec-rails rspec-expectations

是否有人理解为什么下面的期望忽略了参数消息,因为它被作为字符串传递?

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.

2 个答案:

答案 0 :(得分:1)

第二个参数是自定义失败消息:

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

但是在这里看起来你正试图结合两个期望。为此,您需要使用复合语法:

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

答案 1 :(得分:1)

这不是有效的Rspec用法。您假设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!")

但是,您的代码也是多余的。如果消息等于字符串,则肯定存在。因此,第一个期望是完全没用的。

添加

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