使用水豚进行测试时意外注销

时间:2012-06-21 09:44:33

标签: ruby-on-rails devise subdomain capybara

我目前正在使用设计作为身份验证,而capybara + rspec作为我的测试环境。

并且有一种奇怪的行为,管理员似乎意外地被注销了。

这是我的测试代码

require 'spec_helper'                                                                               
require 'helpers/myapp/login_helper'                                                        

describe "Crawler" do                                                                               
  describe "UpdateMainMenus" do                                                                     
    it "should work" do

      @admin = FactoryGirl.create(:myapp_admin)

      login_as @admin, :scope => :admin
      visit admin_cron_jobs_url(subdomain: "myapp")
      current_url.should == admin_cron_jobs_url(subdomain: "myapp")

      puts ""
      puts current_url

      visit admin_cron_jobs_url(subdomain: "myapp")
      current_url.should == admin_cron_jobs_url(subdomain: "myapp")                                


      puts ""
      puts current_url

    end                                                                                             
  end
end

helpers / myapp / login_helper.rb

include Warden::Test::Helpers    
Warden.test_mode!

我已经在rspec_helper.rb中设置了Warden

config.after(:each) { Warden.test_reset! }

输出是:

http://myapp.example.com/admin/cron_jobs
F

Failures:

  1) Crawler UpdateMainMenus should work
     Failure/Error: current_url.should == admin_cron_jobs_url(subdomain: "myapp")
       expected: "http://myapp.example.com/admin/cron_jobs"
            got: "http://myapp.example.com/admins/sign_in" (using ==)
     # ./spec/requests/myapp/crawler_spec.rb:30:in `block (3 levels) in <top (required)>'

Finished in 5.44 seconds
1 example, 1 failure

感谢任何帮助..非常感谢

2 个答案:

答案 0 :(得分:1)

哦,我明白了!

管理员确实已登录。但在第二次“访问”中,它没有登录。

原因是由于我的session_store设置不正确,因此无法在请求之间保持会话

以下是原点设置:

if not Rails.env.production?
  MyApp::Application.config.session_store :cookie_store,
                                         key: '_myapp_session',
                                         domain: '.lvh.me'
end

工作版本(忽略生产设置):

if Rails.env.development?
  MyApp::Application.config.session_store :cookie_store,
                                         key: '_myapp_session',
                                         domain: '.lvh.me'
elsif Rails.env.test?
  MyApp::Application.config.session_store :cookie_store,
                                         key: '_myapp_session',
                                         domain: :all
end

答案 1 :(得分:0)

也许你只是没有成功登录?