Rails response.should be_success永远不会为真 [英] Rails response.should be_success is never true

查看:109
本文介绍了Rails response.should be_success永远不会为真的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在关注Michael Hartl关于Ruby on Rails的出色教程.我一直想了解 ActionDispatch :: Response 的工作方式.这源自第9章的练习9(Rails版本3.2.3).

I am following Michael Hartl's excellent tutorial on Ruby on Rails. I'm stuck trying to understand the way ActionDispatch::Response works. This derives from Exercise 9 of Chapter 9 (Rails version 3.2.3).

尤其是要求我们确保 admin 用户无法自己进行User#destroy.我有一个方法,但是由于要遵循TDD方法,因此我首先编写测试.

In particular we're asked to make sure that the admin user is unable to User#destroy himself. I have an idea how to do that, but since I'm trying to follow a TDD methodology, I'm first writing the tests.

这是我测试中的相关代码段:

This is the relevant snippet in my test:

describe "authorization" do
    describe "as non-admin user" do
        let(:admin) {FactoryGirl.create(:admin)}
        let(:non_admin) {FactoryGirl.create(:user)}

        before{valid_signin non_admin}

        describe "submitting a DELETE request to the Users#destroy action" do
            before do
                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 from destroying himself
    describe "as admin user" do
        let(:admin){FactoryGirl.create(:admin)}
        let(:non_admin){FactoryGirl.create(:user)}

        before do 
            valid_signin admin
        end
        it "should be able to delete another user" do
            expect { delete user_path(non_admin) }.to change(User, :count).by(-1)
        end

        describe "can destroy others" do
            before do 
                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" do
            before do
                delete user_path(admin)
                puts response.success?
            end
            #specify{response.should_not be_success}
            specify{response.should be_redirect}
        end 
    end

.
.
.
end

"can destroy others"测试外,所有测试均通过.

All the tests pass except the "can destroy others" test.

但是,如果我在每个delete请求之后puts response.success?,我总是得到False,所以所有请求都不会成功".

However, if I puts response.success? after every delete request, I always get False, so none of the requests "succeed".

与Web应用程序手动交互并删除用户可以正常工作,因此我认为response.success并不意味着detroy(或对此问题的任何请求)均不成功,而是其他原因.我读到它与 HTTP响应200/302/400 之间的区别有关,但我不确定.

Manually interacting with the webapp and deleting users works just fine, so I assume that response.success does not mean that the detroy(or whatever request for that matter) was not successful, but something else. I read it has to do with the difference between HTTP responses 200/302/400, but I'm not totally sure.

记录下来,这是我的User#destroy:

def destroy
    User.find(params[:id]).destroy
    flash[:success]="User destroyed."
    redirect_to users_path
end

对此有何看法? 谢谢!

Any light on this? thanks!

修改

这是我的工厂:

FactoryGirl.define do
    factory :user do
        sequence(:name){ |n| "Person #{n}" }
        sequence(:email){ |n| "person_#{n}@example.com"}
        password "foobar"
        password_confirmation "foobar"

        factory :admin do
            admin true
        end
    end

end

根据@Peter Alfvin的建议,

编辑2 ,我更改了行

Edit 2 as suggested by @Peter Alfvin, I changed lines

let(:user){FactoryGirl.create(:user)}

let(:admin){FactoryGirl.create(:admin)}

通常所有useradmin.我还在delete请求之前添加了puts admin.admin?.仍然无法正常工作!

And all user to admin in general. I also added a puts admin.admin? before the delete request. Still not working!

编辑3

将测试"can destroy others"更改为:

describe "can destroy others" do
  before do 
    puts admin.admin?
    delete user_path(non_admin)
    puts response.success?
  end

  #specify{response.should be_success}
  specify{response.should_not be_redirect}

end

似乎也没有帮助.

推荐答案

response.success确实引用了HTTP响应代码.默认情况下,我认为这是200范围内的任何值. redirect_to的范围是300.

response.success does indeed refer to the HTTP response code. By default, I believe this is anything in the 200 range. redirect_to is in the 300 range.

这篇关于Rails response.should be_success永远不会为真的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆