0
以下規範失敗並重新執行。我已經嘗試了一切,但無法讓它工作。如果我手動測試它看起來很好:(一些幫助/提示將非常好!爲什麼水豚/ rspec會失去current_user?
如果他/她擁有正確的令牌(在URL中),加入操作應該將已登錄的用戶添加到公會/ )如果用戶沒有登錄,動作將重定向到登錄頁面,並將令牌和ID保存到cookie。登錄後,如果cookie被設置,用戶將被重定向到加入頁面
I'我發現在測試過程中current_user會丟失,會話變量仍然存在,我有一個標準的Authlogic設置,所有其他測試都通過了,所以我真的不知道發生了什麼問題,我是RSpec /水豚,但黃瓜/水豚測試(我從中遷徙)也失敗了,所以我認爲這是水豚問題。
失敗規格:
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
控制器動作:
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"
謝謝你這個答案,但問題仍然存在。 – RD010 2010-09-30 08:17:13
我試過但不行。作爲實例,在使用水豚方法登錄後,current_user在控制器中爲零,你記得RedDragon是如何修復它的? – 2012-01-09 14:41:44