如何通过Rspec创建多个连接?

时间:2015-09-18 12:00:12

标签: ruby-on-rails ruby rspec

问题是如何创建少量连接,至少有两个连接用于测试页面上的在线更改?这只是在线聊天。

此时我只有

let(:user) { FactoryGirl.create(:user) }

before do
  visit signin_path
  fill_in "Name",    with: user.name
  fill_in "Password", with: user.password
  click_button "Sign in"
end

但我正在寻找类似的东西:

let(:first_user) { FactoryGirl.create(:user) }
let(:second_user) { FactoryGirl.create(:user) }

at connection: :first_connection do 
    before do
        visit signin_path
        fill_in "Name",    with: first_user.name
        fill_in "Password", with: first_user.password
        click_button "Sign in"
    end
end

at connection: :second_connection do
    before do
        visit signin_path
        fill_in "Name",    with: second_user.name
        fill_in "Password", with: second_user.password
        click_button "Sign in"
    end
end

# manipulate with connections

感谢。

1 个答案:

答案 0 :(得分:0)

为了做到这一点,我认为你必须手动创建会话。 From the doc

# For ultimate control, you can instantiate and use a Session manually.

require 'capybara'

session = Capybara::Session.new(:webkit, my_rack_app)
session.within("//form[@id='session']") do
  session.fill_in 'Email', :with => 'user@example.com'
  session.fill_in 'Password', :with => 'password'
end
session.click_button 'Sign in'

还有其他一些有趣的事情,比如使用多个窗口。您可以在文档中找到它们。此外,既然你说这是一个实时聊天,请特别小心,因为并非所有驱动程序都支持JavaScript和Ajax。您可能需要其他设置代码。