난 railstutorial.org에 대한 최신 레일 튜토리얼을 진행하고 있는데 특정 운동 (# 8 http://ruby.railstutorial.org/chapters/updating-showing-and-deleting-users#sec:updating_deleting_exercises)에 붙어 있습니다. 관리자가 자신을 삭제할 수 없도록 rspec/capybara 테스트를 작성해야합니다. 구현이 작동하지만 테스트가 제대로 작동하지 않습니다. 여기 내 코드가있다. 비슷한 질문을 여기에서 발견했습니다 : Ruby on Rails syntax 및 https://getsatisfaction.com/railstutorial/topics/how_to_prevent_admin_user_from_deleting_themselves. 그러나 나는 그것이 이전의 튜토리얼이고 같은 질문이 아니라고 생각합니다. 여기 삭제 링크가없는 경우 파괴를위한 Rspec 테스트
은 사양/요청/user_pages_spec.rb의 관련 코드입니다 : 가 여기에 오류 메시지가 사용자 수는 완성도 1.감소지고 있다고
describe "User pages" do
subject { page }
describe "delete links" do
describe "as an admin user" do
let(:admin) { FactoryGirl.create(:admin) }
before do
sign_in admin
visit users_path
end
it "should not be able to delete themself" do
expect { admin.delete }.should_not change(User, :count)
end
end
end
end
end
내 (작동) 구현 :
class UsersController < ApplicationController
before_filter :current_admin, only: :destroy
def current_admin
@user = User.find(params[:id])
redirect_to users_path, notice: "Cannot delete current admin" if current_user?(@user)
end
end
나는 잘못 가고있다. (몇 가지 방법을 생략했지만 잘하면 내가 뭘 하려는지 알기에 충분하다.)
편집 : Ruby v1.9.3, Rails v3.2.3을 사용한다. 기본적으로 관리자에게는 삭제 링크가 없습니다.
Edit2가이 :
사양/컨트롤러/users_controller_spec.rb
require 'spec_helper'
describe UsersController do
describe "admins" do
let(:admin) { FactoryGirl.create(:admin) }
it "should not be able to delete themself" do
sign_in admin
expect { delete :destroy, :id => admin.id }.should_not change(User, :count)
end
end
end
users_controller.rb
def destroy
@user = User.find(params[:id])
if current_user?(@user)
flash[:error] = "Cannot delete current admin"
else
@user.destroy
flash[:success] = "User destroyed."
end
redirect_to users_path
end
답장을 보내 주셔서 감사합니다. 나는 당신의 대답으로 놀고 그것이 효과가 있는지 볼 것입니다. 그러나 기본적으로 관리자를위한 destroy 작업에 대한 링크는 없습니다. 그래서 click_link 메서드를 사용할 수 있다고 생각하지 않습니다. 또한 컨트롤러에 대한 삭제 링크를 추가하면 내 컨트롤러를 테스트하기 위해 이전의 before_filter가 작동하고있었습니다. 그러나, 그것을하는 것이 최선의 방법이 아닐 수도 있습니다. 내가 페이지를 테스트하지 않고 컨트롤러를 테스트하기 때문에 내 주제가 정확하지 않을 수도 있습니다. –
"delete : destroy"를 사용하여 파괴 행동을 직접 호출 할 수 있습니다. 대답에 예제를 추가했습니다. – rpedroso
current_user 메서드가 테스트를 위해 존재하지 않습니다. 아마 나는 하나를 써야한다. admin 객체를 사용하는 또 다른 방법이 있습니까? 추가 된 테스트를 spec/controllers/users_controller_spec.rb와 같은 다른 파일로 옮겨야합니까? –