有谁知道如何使 rspec 遵循重定向(在 Controller 规范中)? (例如,测试/单元有 follow_redirect!)
我试过“follow_redirect!”和“follow_redirect”但只得到
undefined method `follow_redirect!' for #<Spec::Rails::Example::ControllerExampleGroup::Subclass_1:0xb6df5294>
例如:
当我创建一个帐户时,该页面被重定向到帐户页面,我的新帐户应该位于列表的顶部。
it "should create an account" do
post :create, :name => "My New Account"
FOLLOW_REDIRECT!
response.code.should == "200"
accounts = assigns[:accounts]
accounts[0].name.should == "My New Account"
end
但是 FOLLOW_REDIRECT!需要更改为实际有效的内容。
最佳答案
我认为这是 rspec-rails 的默认行为 Controller 测试,从某种意义上说,您可以对响应状态和/或路径设置期望,并测试是否成功。
例如:
it "should create an account" do
post :create
response.code.should == "302"
response.should redirect_to(accounts_path)
end
关于ruby-on-rails - Rspec - Rails - 如何跟随重定向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2865378/