1

I setup Devise so I can write controller specs with this. Then I setup Devise so users cannot delete their accounts.

Now I want to write a spec to make sure the controller is unable to call the destroy action on the Devise user. How do I write this?

In my controller the Devise part looks like this

devise_for :users, skip: :registrations do
  resource :registration,
    only: [:new, :create, :edit, :update],
    path: 'users',
    path_names: { new: 'sign_up' },
    controller: 'devise/registrations',
    as: :user_registration do
    get :cancel
  end
end

In my spec I'm trying to do the following but it doesn't work. I'm not even sure I'm writing it right. I think the page I'm trying to access is wrong.

describe UsersController do
  login_user # from devise controller helper

  it "does not allow deleting of user" do
    get :users, :method => :delete

    # assert here user was not deleted
  end
end
Brand
  • 1,681
  • 1
  • 24
  • 32

3 Answers3

0

I think what you really want to test is whether or not the route exists for the registrations#destroy action. If there is no route, then the action will not be called since it can't be routed to the controller.

For a destroy action, we need to try to route a DELETE action to the users path. so, something like this might do the trick:

{ :delete=> "/users" }.should_not be_routable

Test syntax pulled from a similar answer here: Rails RSpec Routing: Testing actions in :except do NOT route

Community
  • 1
  • 1
Pete
  • 17,885
  • 4
  • 32
  • 30
0

Your mixing your http verbs for one thing. You should be doing

delete :destroy, id: @user

Your going to have to get @user from somewhere, I have it set by controller macros personally.

Then you can either check the response header for unsuccessful, or more easily

@user.should exist
DVG
  • 17,392
  • 7
  • 61
  • 88
0

I would put the following in my controller spec when testing this kind of thing (although i'd use FactoryGirl to create my test user):

it "does not allow deletion of a user" do
  user = User.create!([insert valid args here])
  expect {
    delete :destroy, id: user
  }.not_to change(User, :count)
end
griswoldbar
  • 499
  • 3
  • 10