ruby on rails - How to DRY up this RSpec controller spec? -


how can dry spec up?

describe api::taskscontroller, type: :controller   'allows creator of task destroy it'     set_request_auth_header @user     delete :destroy, id: @task.id     expect(response).to be_success     expect(task.count).to eq 0   end    'does not allow assignee of task destroy it'     set_request_auth_header @assignee     delete :destroy, id: @task.id     expect(response).to be_forbidden     expect(task.count).to eq 1   end    'does not allow unrelated task destroy it'     set_request_auth_header @spy     delete :destroy, id: @task.id     expect(response).to be_forbidden     expect(task.count).to eq 1   end end 

you extract methods:

describe api::taskscontroller, type: :controller   'allows creator of task destroy it'     expect_delete_to_succeed @user   end    'does not allow assignee of task destroy it'     expect_delete_to_be_forbidden @assignee   end    'does not allow unrelated task destroy it'     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 

side notes:

  • using instance (@) variables not current standard practice in rspec. let 'variables'.
  • task.exists? @task.id seems clearer way of checking deletion checking task.count.

Comments

Popular posts from this blog

mysql - Dreamhost PyCharm Django Python 3 Launching a Site -

java - Sending SMS with SMSLib and Web Services -

java - How to resolve The method toString() in the type Object is not applicable for the arguments (InputStream) -