ruby on rails - Why does this delete test not delete my object? -


this spec looks like:

  "should successfullly destroy user record"     expect {       delete :destroy, :id => @user1.id     }.to change{user.count}.by(-1)   end 

that calls action in userscontroller

  def destroy     @user = user.find(params[:id])     if @user.destroy       redirect_to root_path, notice: "you have cancelled account."     else       redirect_to :back     end   end 

but when run spec, this:

rspec::expectations::expectationnotmeterror: expected result have changed -1, changed 0 

how fix this?

edit 1

@user1 being initialized in before_each call @ top of context block it statement lives in. looks this:

context "when inviter being deleted , invited has not accepted invitation"   before :each     @user1 = create(:user, gender: 0)     @user2 = create(:user)     @member = create(:member, email: @user2.email, first_name: @user2.first_name, last_name: @user2.last_name, bio: @user2.bio, gender: @user2.gender)     @membership = create(:membership, member: @member, family_tree: @user1.family_tree, inviter: @user1, relation: "sister")     @connection = create(:connection, inviter_membership: @membership)     login_user   end 

edit 2

when swap out test this, works:

  "should successfullly destroy user record"     delete :destroy, id: @user1     expect(response).to redirect_to(root_path)     # expect {     #   delete :destroy, id: @user1     # }.to change(user,:count).by(-1)   end 

that test passes.

finished in 1.2 seconds (files took 6.08 seconds load) 19 examples, 0 failures, 15 pending 

edit 3

for it's worth, in userscontroller calling load_and_authorize_resource @ top. when remove that, test seem pass. question is, how work cancancan?

this ability.rb looks like:

class ability   include cancan::ability    def initialize(user)     user ||= user.new # guest user (not logged in)     if user.has_role? :admin       can :manage, :all     end      can :manage, familytree, user_id: user.id     can :manage, comment, user_id: user.id     can :manage, node, user_id: user.id     can :manage, event, user_id: user.id     can :manage, user, id: user.id      can :read, comment, user_id: user.id     can :read, event, user_id: user.id   end end 

edit 4

when add raise destroy action...nothing happens. appear destroy action indeed not being executed.

edit 5

this login_user method looks like:

module specauthentication   def login_user     @request.env["devise.mapping"] = devise.mappings[:user]     @user = factorygirl.create :user     sign_in @user   end end 

with cancancan gem have allow controller actions. need add ability.rb file.

can :destroy, user

this allow role destroy user object. can add more specifics fit use case.

edit: saw :manage documentation on cancancan gem.

your ability file allows users manage themselves. login_user method creating user on fly while test trying destroy separate user. can either update cancan ability let users destroyed or try destroying current_user.id assuming have current_user method. suggest changing login_user method accept user object know user logged in , have access object.

edit:

as noted above, use method sign_in specific user can access object. replace login_user call sign_in @user1 or similar method accepts user object.


Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

javascript - Get parameter of GET request -

javascript - Twitter Bootstrap - how to add some more margin between tooltip popup and element -