为什么 capybara/rspec 会丢失 current_user?
以下规范失败并再次失败。我已经尝试了一切,但无法让它发挥作用。如果我手动测试它,一切看起来都很好:(一些帮助/提示会非常好!
如果登录用户有正确的令牌(在 URL 中),加入操作应该将他/她添加到公会/组。如果用户未登录,操作将重定向到登录页面并将令牌和 ID 保存到 cookie 中,如果设置了 cookie,则用户将被重定向到加入页面
。在测试期间,会话变量仍然存在。我有一个标准的 Authlogic 设置,并且所有其他测试都通过了,所以我真的不知道出了什么问题,但是黄瓜/水豚测试(我正在迁移的地方)也失败了,所以我认为这是一个失败的规格
:
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
“rake spec”结果:
...................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
Gems:
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"
The following spec is failing and failing over again. I've tried everything but can't get it to work. If I test it manually all looks fine :( Some help/tips would be really nice!
The join action should add an logged-in user to a guild/group if he/she has the right token (in the URL). If the user isn't logged in, the action redirects to the login page and saves the token and the ID to cookies. After login the user gets redirected to the join page if the cookies are set.
I've found out that the current_user get lost during the test. The session variable is still present. I've a standard Authlogic setup and all other tests are passing so I really don't know what's going wrong. I'm new to RSpec/capybara but the cucumber/capybara test (from which I'm migrating) is also failing so I think it's a capybara issue.
Failing Spec:
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
Controller Action:
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
"rake spec" result:
...................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
Gems:
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"
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是来自 http://gist.github.com/470808
This is from http://gist.github.com/470808