Ruby on rails Rspec-如何调用GET and send flash消息?

Ruby on rails Rspec-如何调用GET and send flash消息?,ruby-on-rails,rspec,Ruby On Rails,Rspec,我知道通过flash传递数据是很奇怪的,但是为什么会发生这种情况,有一些(可能有缺陷的)逻辑,我不是现在就重新布线,而是想试着让这个测试工作起来。当测试根据合法的闪存[:thing\u id]返回:success时,它会不断返回:error模板(实际的应用程序行为有效) 来自控制器: def post_multiple_new @thing = Thing.find_by_id(flash[:thing_id].to_i) unless @thing render :error

我知道通过flash传递数据是很奇怪的,但是为什么会发生这种情况,有一些(可能有缺陷的)逻辑,我不是现在就重新布线,而是想试着让这个测试工作起来。当测试根据合法的
闪存[:thing\u id]
返回
:success
时,它会不断返回
:error
模板(实际的应用程序行为有效)

来自控制器:

def post_multiple_new
  @thing = Thing.find_by_id(flash[:thing_id].to_i)
  unless @thing
    render :error
  else
    render :success
  end
end
context 'when valid id' do
  let(:thing) { create(:thing) }
  let(:flash) { {thing_id: thing.id.to_s} }
  it 'renders correctly' do
    get :post_multiple_new
    expect(response).to render_template(:success)
  end
end
来自规范:

def post_multiple_new
  @thing = Thing.find_by_id(flash[:thing_id].to_i)
  unless @thing
    render :error
  else
    render :success
  end
end
context 'when valid id' do
  let(:thing) { create(:thing) }
  let(:flash) { {thing_id: thing.id.to_s} }
  it 'renders correctly' do
    get :post_multiple_new
    expect(response).to render_template(:success)
  end
end

谢谢@Kristján-你的工作中最底层的答案

context'当游戏id有效时'do'
允许(:thing){创建(:thing)}
它“正确渲染”了吗
flash\u hash=ActionDispatch::flash::FlashHash.new
flash\u hash[:thing\u id]=thing.id
会话['flash']=flash\u散列值到会话值
获取:发布多个新的
期望(响应)。呈现_模板(:成功)
终止
终止

我强烈建议安装
byebug
gem,以确保控制器中的变量是它应该是的。在您的规范中测试是否创建了“thing”,并将什么id传递给thing_idof