[英]How to DRY up RSpec tests shared by different actions in same controller
[英]How to DRY up this RSpec controller spec?
我怎么能干这个规格?
describe Api::TasksController, type: :controller do
it 'allows the creator of a task to destroy it' do
set_request_auth_header @user
delete :destroy, id: @task.id
expect(response).to be_success
expect(Task.count).to eq 0
end
it 'does not allow the assignee of a task to destroy it' do
set_request_auth_header @assignee
delete :destroy, id: @task.id
expect(response).to be_forbidden
expect(Task.count).to eq 1
end
it 'does not allow anyone unrelated to a task to destroy it' do
set_request_auth_header @spy
delete :destroy, id: @task.id
expect(response).to be_forbidden
expect(Task.count).to eq 1
end
end
你没有。 瞧,欢迎你。
你可以提取一些方法:
describe Api::TasksController, type: :controller do
it 'allows the creator of a task to destroy it' do
expect_delete_to_succeed @user
end
it 'does not allow the assignee of a task to destroy it' do
expect_delete_to_be_forbidden @assignee
end
it 'does not allow anyone unrelated to a task to destroy it' do
expect_delete_to_be_forbidden @spy
end
def expect_delete_to_succeed(requester)
delete_task requester
expect(response).to be_success
expect(Task.count).to eq 0
end
def expect_delete_to_be_forbidden(requester)
delete_task requester
expect(response).to be_forbidden
expect(Task.count).to eq 1
end
def delete_task(requester)
set_request_auth_header requester
delete :destroy, id: @task.id
end
end
附注:
let
“变量” 。 Task.exists? @task.id
Task.exists? @task.id
似乎比检查Task.count
更清楚地检查删除。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.