I tried to get the code in the original message to work, but no luck. Instead, I made it work this way (so that it would fail and then go through). This verifies that the redirect is correct, as well as the correct flash message.
TEST: authentication_pages_spec.rb
describe "as admin user" do let(:admin) { FactoryGirl.create(:admin) } before { sign_in admin } describe "can't delete self by submitting DELETE request to Users#destroy" do before { delete user_path(admin) } specify { response.should redirect_to(users_path), flash[:error].should =~ /Can not delete own admin account!/i } end end
IMPLEMENTATION: Users # destroy
def destroy user = User.find(params[:id]) if (current_user == user) && (current_user.admin?) flash[:error] = "Can not delete own admin account!" else user.destroy flash[:success] = "User destroyed." end redirect_to users_path end
Perhaps the reason the original test didn't work is because we are issuing a request? I tried to add each of the following elements to the description block, and all failed:
it { should have_selector('div.alert.alert-error', text: 'delete own admin') } it { should have_selector('title', text: 'All users') } it { should have_selector('h1', text: 'All users') }
So, it looks like Capybara is not actually being redirected to the page to check for these selectors. I tried the "title" and "h1", thinking maybe there was some problem with the selector "div.alert.alert-error" ... but the "title" and "h1" failed with the same "expected CSS to return that "..."
Does anyone know more about how the style tests specify { response.should ... }
? If they do not follow call forwarding, when do they click on the controller action?
source share