ruby - Rails response.should be_success is never true -
i following michael hartl's excellent tutorial on ruby on rails. i'm stuck trying understand way actiondispatch::response works. derives exercise 9 of chapter 9 (rails version 3.2.3).
in particular we're asked make sure admin user unable user#destroy
himself. have idea how that, since i'm trying follow tdd methodology, i'm first writing tests.
this relevant snippet in test:
describe "authorization" describe "as non-admin user" let(:admin) {factorygirl.create(:admin)} let(:non_admin) {factorygirl.create(:user)} before{valid_signin non_admin} describe "submitting delete request users#destroy action" before delete user_path(admin) #puts response.message puts response.succes? end specify{ response.should redirect_to(root_path) } specify{ response.should_not be_success } end end #exercise 9.6-9 prevent admin destroying himself describe "as admin user" let(:admin){factorygirl.create(:admin)} let(:non_admin){factorygirl.create(:user)} before valid_signin admin end "should able delete user" expect { delete user_path(non_admin) }.to change(user, :count).by(-1) end describe "can destroy others" before puts admin.admin? delete user_path(non_admin) puts response.success? end #specify{response.should be_success} specify{response.should_not be_redirect} end describe "cannot destroy himself" before delete user_path(admin) puts response.success? end #specify{response.should_not be_success} specify{response.should be_redirect} end end . . . end
all tests pass except "can destroy others"
test.
however, if puts response.success?
after every delete
request, false
, none of requests "succeed".
manually interacting webapp , deleting users works fine, assume response.success
not mean detroy
(or whatever request matter) not successful, else. read has difference between http responses 200/302/400, i'm not totally sure.
for record, user#destroy
:
def destroy user.find(params[:id]).destroy flash[:success]="user destroyed." redirect_to users_path end
any light on this? thanks!
edit
this factory:
factorygirl.define factory :user sequence(:name){ |n| "person #{n}" } sequence(:email){ |n| "person_#{n}@example.com"} password "foobar" password_confirmation "foobar" factory :admin admin true end end end
edit 2 suggested @peter alfvin, changed lines
let(:user){factorygirl.create(:user)}
to
let(:admin){factorygirl.create(:admin)}
and user
admin
in general. added puts admin.admin?
before delete
request. still not working!
edit 3
changing test "can destroy others"
as:
describe "can destroy others" before puts admin.admin? delete user_path(non_admin) puts response.success? end #specify{response.should be_success} specify{response.should_not be_redirect} end
does not seem either.
for "admin" case, you're still creating , logging in "regular" user instead of admin user, why can't destroy else.
Comments
Post a Comment