Capybara没有找到提交按钮

时间:2014-05-25 12:17:37

标签: ruby-on-rails rspec capybara

rspec - 测试期间,我尝试通过填写表单并单击“重置密码”链接来重置密码。

Capybara给了我以下错误:

1) ResetPasswords emails user a reset link when resetting password
   Failure/Error: click_link 'Reset Password'
   Capybara::ElementNotFound:
     Unable to find link "Reset Password"

显然它找不到我想要点击的链接。但是,当我尝试使用puts page.body打印正在查看的页面时,此元素存在:

<div><input name="commit" type="submit" value="Reset Password" /></div>

我尝试使用clickclick_buttonclick_link,但没有使用雪茄。

为什么找不到元素?

更新1:click_button输出

1) ResetPasswords emails user a reset link when resetting password
   Failure/Error: click_button 'Reset Password'
   ActionView::Template::Error:
     Missing host to link to! Please provide the :host parameter, set
     default_url_options[:host], or set :only_path to true

更新2:测试文件

require 'spec_helper'

include Warden::Test::Helpers
Warden.test_mode!

describe "ResetPasswords" do
  it "emails user a reset link when resetting password" do
    visit login_path
    click_link 'password'

    fill_in 'Email', :with => @user.email

    puts page.body
    click_link 'Reset Password'

    page.should have_content "You will receive an email"
  end

  before(:each) do
    @user = create(:user)
    #login_as @user, :scope => :user
  end
end

2 个答案:

答案 0 :(得分:1)

将此代码段放入config/environments/test.rb解决了它:

config.action_mailer.default_url_options = {:host => "localhost:3000"}

答案 1 :(得分:0)

尝试直接转到页面重置密码

describe "ResetPasswords" do
  it "emails user a reset link when resetting password" do
    visit reset_password_path

    fill_in 'Email', :with => @user.email

    click_link 'Reset Password'

    page.should have_content "You will receive an email"
  end

  before(:each) do
    @user = create(:user)
    #login_as @user, :scope => :user
  end
end