0

我目前使用 devise 作为身份验证,使用 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

任何帮助表示赞赏..真的谢谢

4

2 回答 2

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
于 2012-06-21T13:34:58.670 回答
0

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

于 2012-06-21T10:01:39.647 回答