在控制器规范中提供强大的参数
Provide strong parameters in controller spec
我正在为控制器的一种方法编写单元测试,如下所示:
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
任何线索都会很有帮助。
谢谢
您需要将关键参数传递给操作。不仅要检查响应状态,还要检查操作结果是个好主意
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
找不到任何内容,它不会引发错误,它只是 returns nil。这意味着您不会获得 RecordNotFound。此外,这是一种已弃用的方法,您应该使用 find_by(key: params[:id])
要引发错误,请使用 bang 方法 find_by!(key: params[:id])
如果您要在应用中传递 key.id
,则需要在控制器操作中进行更改
@key = current_user.keys.find(params[:id])
我正在为控制器的一种方法编写单元测试,如下所示:
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
任何线索都会很有帮助。 谢谢
您需要将关键参数传递给操作。不仅要检查响应状态,还要检查操作结果是个好主意
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
找不到任何内容,它不会引发错误,它只是 returns nil。这意味着您不会获得 RecordNotFound。此外,这是一种已弃用的方法,您应该使用 find_by(key: params[:id])
要引发错误,请使用 bang 方法 find_by!(key: params[:id])
如果您要在应用中传递 key.id
,则需要在控制器操作中进行更改
@key = current_user.keys.find(params[:id])