在控制器规范中提供强参数

问题描述 投票:0回答:1

我正在为控制器的一种方法编写单元测试,如下所示:

def update
  @key = current_user.keys.find_by_key(params[:id])
  @key.update_attributes(key_params)
  redirect_to :back
end

private

def key_params
  params.require(:key).permit(:note)
end

这条路线是:

PUT              /projects/:project_id/keys/:id           keys#update

到目前为止,我有以下内容:

describe '#update' do
  before :each do
    @user = FactoryGirl.create(:user)
    @project= FactoryGirl.create(:project, user: @user)
    @key = FactoryGirl.create(:key, id: 40, project: @project, user: @user)
    controller.stub(:current_user).and_return(@user)
  end

  it 'update key' do
    put :update, project_id:@project.id, id:@key.id
    expect(response.code).to eq "302"
  end
end

但是这会产生如下错误:

KeysController#update update key
 Failure/Error: put :update, project_id:@project.id, id:@key.id
 ActionController::ParameterMissing:
   param is missing or the value is empty: key

任何线索都会非常有帮助。谢谢

ruby-on-rails rspec rspec-rails
1个回答
1
投票

您需要将关键参数传递给操作。并且最好不仅要检查响应状态,还要检查操作结果

it 'updates key' do
  # supposing that "note" is a string column
  expect do
    put :update, project_id: @project.id, id: @key.id, key: { note: 'New note' } 
  end.to change { @key.note }.from('Old note').to('New note')
  expect(response.code).to eq "302"
end

更新:

在控制器中,您尝试按键属性查找键实例

@key = current_user.keys.find_by_key(params[:id])

但是你在规范中传递了key.id.它在应用程序中如何工作?我想,你传递一个密钥作为:id参数,所以它应该是

put :update, project_id: @project.id, id: @key.key, key: { note: 'New note' } 

在你的规格。此外,如果找不到任何东西,find_by_key不会引发错误,它只返回nil。这意味着你不会得到RecordNotFound。而且,这是一个弃用的方法,你应该使用find_by(key: params[:id])提出错误使用爆炸方法find_by!(key: params[:id])

如果您在应用程序中传递key.id,则需要对控制器操作进行更改

@key = current_user.keys.find(params[:id])
© www.soinside.com 2019 - 2024. All rights reserved.