2

我有一个请求规范,它一直传递到我需要检查页面上是否存在内容的地方,这是我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类外部。

4

1 回答 1

0

所以我似乎通过, :js => true do在“它”旁边添加一行并使用硒网络驱动程序来通过测试。我在想,必须有一种方法可以在没有硒的情况下做到这一点,因为你必须坐下来等待它实际上在浏览器中运行,这是不利的。

也许我走错了路,我实际上应该检查视图规范中的部分(目前它只是功能测试的一部分)。

于 2013-10-27T07:17:54.617 回答