В решении упражнения 5 я столкнулся с проблемой, аналогичной этой вопрос .Я произвел рефакторинг на основе ответа на этот вопрос, но все еще получаю ошибку:
1) UsersController DELETE 'destroy' as an admin user should not self-destruct
Failure/Error: lambda do
count should have been changed by 0, but was changed by -1
# ./spec/controllers/users_controller_spec.rb:354:in `block (4 levels) in <top (required)>'
Моя спецификация:
it "should destroy the user" do
lambda do
delete :destroy, :id => @user
end.should change(User, :count).by(-1)
end
it "should redirect to the users page" do
delete :destroy, :id => @user
response.should redirect_to(users_path)
end
it "should not self-destruct" do
lambda do
delete :destroy, :id => @user.id
end.should change(User, :count).by(0)
end
и мой контроллер:
def destroy
@user = User.find(params[:id])
if current_user == @user
flash[:notice] = "You cannot destroy yourself"
else
@user.destroy
flash[:success] = "User destroyed"
end
redirect_to users_path
end
Я проверил поведение в браузере, и он работает, как ожидалось.Как всегда, любая помощь приветствуется.Спасибо!
Обновленный, рабочий код:
describe "as an admin user" do
before(:each) do
@admin = Factory(:user, :email => "admin@example.com", :admin => "true")
test_sign_in(@admin)
end
it "should have links to destroy a user" do
get :index
response.should have_selector("a", :content => "delete" )
end
it "should destroy the user" do
lambda do
delete :destroy, :id => @user
end.should change{ User.count }.by(-1)
end
it "should redirect to the users page" do
delete :destroy, :id => @user
response.should redirect_to(users_path)
end
it "should not be allowed to delete itself" do
lambda do
delete :destroy, :id => @admin
end.should_not change{ User.count }
end
end