你如何使用Capybara测试js重定向?

时间:2013-03-08 01:08:58

标签: ruby-on-rails capybara

在我的控制器中,使用以下代码完成重定向:

会话控制器

def create
  render js: "window.location.pathname = #{home_path.to_json}"
end

但是当我运行capybara时,login提交后,它无法访问home_page:

describe "Login Test", :js => true do
  before do
    @user = FactoryGirl.create(:user)
  end
  it 'should show the users first name' do
    visit login_path
    fill_in 'email', with: @user.email
    fill_in 'password', with: @user.password
    click_button "Log In"
    save_and_open_page
    assert page.has_content? @user.first_name
  end
end

当我通过添加launchy gem并运行save_and_open_page进行问题排查时,我会看到一个包含以下文字的页面: window.location.pathname = "/"

Capybara给了我以下错误: Capybara::ElementNotFound: Unable to find xpath "/html"

似乎水豚从未通过登录按钮。

1 个答案:

答案 0 :(得分:0)

你试过这个吗?

respond_to do |format|
  format.js { render :js => 'window.location.href = "#{home_path.to_json}"' }
end