与Capybara和Rails保持会话3 [英] Maintaining Session with Capybara and Rails 3

查看:69
本文介绍了与Capybara和Rails保持会话3的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个水豚测试,第一个用于登录用户,第二个用于测试仅登录用户可用的功能。

I have two capybara tests, the first of which signs in a user, and the second which is intended to test functions only available to a logged in user.

但是,由于无法在各个测试之间维护会话(显然应该如此),所以我无法使第二个测试正常工作。

However, I am not able to get the second test working as the session is not being maintained across tests (as, apparently, it should be).

require 'integration_test_helper'

class SignupTest < ActionController::IntegrationTest

  test 'sign up' do  
    visit '/'
    click_link 'Sign Up!'
    fill_in 'Email', :with => 'bob@wagonlabs.com'
    click_button 'Sign up'
    assert page.has_content?("Password can't be blank")
    fill_in 'Email', :with => 'bob@wagonlabs.com'
    fill_in 'Password', :with => 'password'
    fill_in 'Password confirmation', :with => 'password'
    click_button 'Sign up'
    assert page.has_content?("You have signed up successfully.")
  end

  test 'create a product' do
    visit '/admin'
    save_and_open_page
  end

end

save_and_open_page调用生成的页面是全局登录屏幕,而不是我期望的管理主页(注册将登录)。我在这里做错了什么?

The page generated by the save_and_open_page call is the global login screen, not the admin homepage as I would expect (the signup logs you in). What am I doing wrong here?

推荐答案

发生这种情况的原因是测试是事务性的,因此您在测试之间会失去状态。要解决此问题,您需要在函数中复制登录功能,然后再次调用它:

The reason this is happening is that tests are transactional, so you lose your state between tests. To get around this you need to replicate the login functionality in a function, and then call it again:


def login
  visit '/'
  fill_in 'Email', :with => 'bob@wagonlabs.com'
  fill_in 'Password', :with => 'password'
  fill_in 'Password confirmation', :with => 'password'
  click_button 'Sign up'
end

test 'sign up' do
 ...
 login
 assert page.has_content?("You have signed up successfully.")
end

test 'create a product' do
  login
  visit '/admin'
  save_and_open_page
end

这篇关于与Capybara和Rails保持会话3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆