Ruby on rails 3 为什么capybara/rspec会失去当前的用户?

Ruby on rails 3 为什么capybara/rspec会失去当前的用户?,ruby-on-rails-3,rspec2,Ruby On Rails 3,Rspec2,以下规范正在失败并再次失败。我什么都试过了,但都没用。如果我手动测试,一切看起来都很好:(一些帮助/提示会非常好 如果登录用户拥有正确的令牌(在URL中),则加入操作应将其添加到公会/团体。如果用户未登录,则该操作将重定向到登录页面,并将令牌和ID保存到Cookie。登录后,如果设置了Cookie,则会将用户重定向到加入页面 我发现当前的用户在测试过程中迷路了。会话变量仍然存在。我有一个标准的Authlogic设置,所有其他测试都通过了,所以我真的不知道出了什么问题。我是RSpec/capyba

以下规范正在失败并再次失败。我什么都试过了,但都没用。如果我手动测试,一切看起来都很好:(一些帮助/提示会非常好

如果登录用户拥有正确的令牌(在URL中),则加入操作应将其添加到公会/团体。如果用户未登录,则该操作将重定向到登录页面,并将令牌和ID保存到Cookie。登录后,如果设置了Cookie,则会将用户重定向到加入页面

我发现当前的用户在测试过程中迷路了。会话变量仍然存在。我有一个标准的Authlogic设置,所有其他测试都通过了,所以我真的不知道出了什么问题。我是RSpec/capybara的新手,但是cucumber/capybara测试(我正在从中迁移)也失败了,所以我认为这是一个capybara问题

不合格规格:

describe GuildsController do
  fixtures :roles

  def login
    @user = Factory(:User)
    visit login_path
    fill_in 'Login', :with => @user.login
    fill_in 'Password', :with => 'password'
    click 'Login'
    page.should have_css(".notice")
  end

  def assing_user_to_guild_as(role)
    role_id = Role.where(:name => role).first.id
    @guild.assignments << Assignment.new(:role_id => role_id, :user_id => @user.id, :guild_id => @guild.id)
  end

  before(:each) do
    @guild = Guild.first || Factory(:Guild).build 
    visit root_path 
  end

  context "a user" do
    before(:each) do
      login
    end

    it "should be able to join a guild with a valid token" do
      visit "guilds/#{@guild.id}/join/#{@guild.token}"
      @guild.members.include?(@user.login).should be_true
      page.should have_css(".notice")
    end

    it "shouldn't be able to join a guild with a invalid token" do
      visit "guilds/#{@guild.id}/join/#{@guild.token+"invalid"}"
      @guild.members.include?(@user.login).should be_false
      page.should have_css(".error")
    end
  end
end

这是从

开始的,我试过了,但不起作用。例如,在使用capybara方法登录后,控制器中的当前用户为零,RedDragon您还记得如何修复它吗?
  def join
    @guild = Guild.find(params[:id])
    respond_to do |format|
      if current_user.nil?
        flash[:error] = t("have_to_be_logged_in")
        unless params[:token].nil?
          cookies[:rguilds_jg_token] = params[:token]
          cookies[:rguilds_jg_gid] = params[:id]
        end
        format.html { redirect_to(login_path) }
      else
        unless cookies[:rguilds_jg_token].nil? &&  cookies[:rguilds_jg_gid].nil?
          cookies.delete(:rguilds_jg_token)
          cookies.delete(:rguilds_jg_gid)
        end
        if @guild.verified?
          if params[:token] == @guild.token
            unless @guild.users.include?(current_user)
              @guild.assignments << Assignment.create(:user_id => current_user.id, :role_id => Role.find_by_name("member").id)
              flash[:notice] = t('guilds.joined')
              format.html { redirect_to(@guild) }
            else
              flash[:error] = t('guilds.already_joined')
              format.html { redirect_to(@guild) }
            end
          else
            flash[:error] = t('guilds.invalid_token')
            format.html { redirect_to(@guild) }
          end
        else
          flash[:error] = t('guilds.not_verified')
          format.html { redirect_to(@guild) }
        end
      end
    end
  end
...................FF.....................................................................

Failures:
  1) GuildsController a user should be able to join a guild with a valid token
     Failure/Error: @guild.members.include?(@user.login).should be_true
     expected false to be true
     # ./spec/integration/guilds_spec.rb:72:in `block (3 levels) in <top (required)>'

  2) GuildsController a user shouldn't be able to join a guild with a invalid token
     Failure/Error: page.should have_css(".error")
     expected #has_css?(".error") to return true, got false
     # ./spec/integration/guilds_spec.rb:79:in `block (3 levels) in <top (required)>'

Finished in 7.87 seconds
90 examples, 2 failures
gem 'rails', '3.0.0.rc'
gem "mocha"
gem "rspec-rails", ">= 2.0.0.beta.19"
gem "factory_girl_rails"
gem 'capybara'
gem "authlogic", :git => "http://github.com/odorcicd/authlogic.git", :branch => "rails3"
# In your test_helper.rb / spec_helper.rb

class ActiveRecord::Base
  mattr_accessor :shared_connection
  @@shared_connection = nil

  def self.connection
    @@shared_connection || retrieve_connection
  end
end

# Forces all threads to share the same connection. This works on
# Capybara because it starts the web server in a thread.
ActiveRecord::Base.shared_connection = ActiveRecord::Base.connection