Capybara和Rails has_content在规范中未通过测试以查看消息是否部分出现

时间:2013-10-26 04:56:49

标签: ruby rspec ruby-on-rails-4 capybara

我有一个请求规范,直到我需要检查以查看页面上是否存在内容,我正在使用page.should have_content来执行此操作。内容实际上是在成功提交表单时出现的消息,该消息是从消息部分呈现的。即使我通过浏览器进行测试,功能仍按预期工作并且内容按预期显示,测试也会失败。我也使用FactoryGirl来生成用于表单提交的用户。

这是使用--format d选项运行规范后得到的错误:

UserSignup
  shows a thank you message on successful form submission (FAILED - 1)

Failures:

  1) UserSignup shows a thank you message on successful form submission
     Failure/Error: page.should have_content("Thank you. You will be notified of our launch at #{user.email}.")
       expected #has_content?("Thank you. You will be notified of our launch at quinn.purdy@casperzboncak.org.") to return true, got false
     # ./spec/requests/user_signup_spec.rb:21:in `block (2 levels) in <top (required)>'

user_signup_spec.rb:

require 'spec_helper'
describe "UserSignup" do
   it "shows a thank you message on successful form submission" do
     user = FactoryGirl.create(:user)
     visit sign_up_path
     fill_in "user_fullname", with: user.fullname
     fill_in "user_email", with: user.email
     click_button "Sign up"
     current_path.should eq(sign_up_path)
     page.should have_content("Thank you. You will be notified of our launch at #{user.email}.")
   end
end

users_controller.rb:

class UsersController < ApplicationController
  def new
    @user = User.new
  end

def create
    @user = User.new(secure_params)
    if @user.valid?
    @user.subscribe
    flash[:notice] = "Thank you. You will be notified of our launch at #{@user.email}."
    redirect_to sign_up_path
  else
    render :new
  end
end


private

  def secure_params
  params.require(:user).permit(:fullname, :email)
  end

end

我想知道是否可能是因为我从应用程序布局中呈现了部分消息,但是当它在用户视图中输出时,消息显示在源的body内,但在main之外{1}}上课。

1 个答案:

答案 0 :(得分:0)

所以我似乎已经通过在'it'旁添加行, :js => true do并使用selenium web驱动程序来完成测试。必须有一种方法可以在没有硒的情况下做到这一点我正在考虑,因为你必须坐下来等待它实际上在浏览器中运行它是一个缺点。

也许我的方法不对,我应该在视图规范中检查部分内容(目前它只是功能测试的一部分)。