Unexpected Capybara Warnings - Rails Tutorial Chapter 9 Exercise 9
I'm wrestling with Harlt's Chapter 9 exercise 9.
When I design my test for this exercise with :no_capybara set to true,
like in other sections of the tutorial, the test PASSES, but I get the
following warning:
WARNING: ignoring the provided expectation message argument (true) since
it is not a string.
This version of the test is here:
*spec/requests/authentication_pages_spec.rb*
describe "as an admin user" do
let(:admin) {FactoryGirl.create(:admin)}
before do
sign_in(admin, :no_capybara => true)
end
describe "should not be able to delete itself by direclty submitting
request" do
before { delete user_path(admin) }
specify { response.should redirect_to(users_path),
flash[:error].should =~ /Can't delete own admin account/i }
end
end
Note this this is how Hartl uses that method in other spaces of the
tutorial, as follows:
*spec/requests/authentication_pages_spec.rb*
describe 'signed in user' do
let(:user) { FactoryGirl.create(:user) }
before { sign_in user, no_capybara: true }
describe 'unable to access now' do
before {get new_user_path}
specify { expect(response).to redirect_to(root_url)}
end
..
..
.ect...
However, when I do not set :no_capybara, my test fails:
describe "as an admin user" do
let(:admin) {FactoryGirl.create(:admin)}
before { sign_in(admin) }
.
.
.
Failures:
1) Authentication authorization as a non-admin user submitting a DELETE
request to the Users#destroy action
Failure/Error: before { delete user_path(user)}
NoMethodError:
undefined method `admin?' for nil:NilClass
# ./app/controllers/users_controller.rb:68:in `admin_user'
# ./spec/requests/authentication_pages_spec.rb:74:in `block (5
levels) in <top (required)>'
My two main questions are:
Why is that warning occurring during that test, but not in other tests
where I pass :no_capybara to the sign_in method?
Why is my test failing if I don't pass it no_capybara? I've seen the other
questions on stackoverflow related to this exercise, and other people's
solutions don't require no_capybara.
Below is all code within my app that might be applicable. If I should
include more, please let me know.
*users_controller.rb*
before_action :admin_user, only: :destroy
def destroy
user = User.find(params[:id])
if !current_user?(user)
user.destroy
flash[:success] = "User destroyed. ID: #{user.id}"
else
flash[:error] = "Can't delete own admin account"
end
redirect_to users_path
end
def admin_user
redirect_to(root_url) unless current_user.admin?
end
*controllers/helpers/session_helper.rb*
def current_user
remember_token = User.encrypt(cookies[:remember_token])
@current_user ||= User.find_by(remember_token: remember_token)
end
No comments:
Post a Comment