使用rspec和authlogic进行测试时用户登录

Roh*_*hit 5 ruby testing rspec ruby-on-rails authlogic

我有一个测试控制器的规范如下

require 'spec_helper'

describe ProductsController do
setup :activate_authlogic

describe "user not logged in" do

it "should not GET index" do
get :index
response.should redirect_to(login_path)
end

end

describe "user logged in" do

before(:each) do
UserSession.create :username => "rohit", :password => "test123"
end

it "should GET index" do
get :index
response.should redirect_to(products_path)
end

end

end
Run Code Online (Sandbox Code Playgroud)

我也在spec_helper.rb中使用过这一行

require "authlogic/testcase"
Run Code Online (Sandbox Code Playgroud)

"用户未登录通过"但"登录用户"的测试失败

'ProductsController user is logged in should GET index' FAILED
expected redirect to "/products", got no redirect
Run Code Online (Sandbox Code Playgroud)

shi*_*ara -1

这看起来很正常,因为您使用登录用户获取“/products”url。然后他看到了这一页。他没有重定向到他看到的页面。

每个测试都是独立的。之前的测试中没有保存任何状态。