水豚和 RSPEC - 结果显示缺少标题,但是当我 save_and_open_page 时我看到了标题

Capybara and RSPEC - Result says title is missing, but when I save_and_open_page I see the title

我将 Rails 4 和 Ruby 2.2 与 RSPEC、Capybara 和 FactoryGirl 一起使用。

我正在尝试测试用户是否可以编写添加角色的故事(其他用户)。它在浏览器中工作,但是当我 运行 测试时,我收到以下消息,表明标题丢失:

Failure/Error: expect(page).to have_content("Thank you for sharing a story.") expected to find text "Thank you for sharing a story." in "Family Matters Write New Story Add User Log Out * Title DescriptionSo this is what happened that night in 1972 Who was a part of this story?Loni Cabral Fernando Cabral Cover image"

当我添加save_and_open_page时,我可以看到标题已经插入。当我删除 select 个字符的行时,测试通过了。

这是测试文件:

require 'rails_helper'
require_relative '../support/new_story_form'

feature 'create story' do

let(:new_story_form) { NewStoryForm.new}
let(:user) { FactoryGirl.create(:active_user) }
let(:character1) { FactoryGirl.create(:active_user, first_name:"Loni", last_name:"Cabral") }
let(:character2) { FactoryGirl.create(:active_user, first_name:"Fernando", last_name:"Cabral") }

before do 
    login(user)
    user.add_relative(character1, "Child")
    user.add_relative(character2, "Child")
end

scenario 'create new story with valid data' do

    new_story_form.visit_form.fill_in_with(
        title: "Great story",
        cover_image: "cover_image.png"
        ).submit
    expect(page).to have_content("Thank you for sharing a story.")
    expect(page).to have_content("Who was involved:")
    expect(page).to have_content(character1.name)
    expect(page).to have_content(character2.name)
    expect(Story.last.cover_image_identifier).to eq("cover_image.png")  
    expect(Story.last.title).to eq("Great story")
    expect(Story.last.user).to eq(user)
    expect(Story.last.participants.first).to eq(character1)
end



scenario 'cannot create story with invalid data' do
    new_story_form.visit_form.submit
    expect(page).to have_content(" can't be blank")
end
end

这里是 new_story_form 支持文件:

class NewStoryForm

include Capybara::DSL

def visit_form
    visit('/')
    click_on("Write New Story")
    self
end

def fill_in_with(params = {})
    fill_in("Title", with: params.fetch(:title, "Great story"))
    fill_in("Description", with: "So this is what happened that night in 1972")
    attach_file('Cover image', "#{Rails.root}/spec/fixtures/" + params.fetch(:cover_image, "cover_image.png"))
    select("Loni Cabral", from:"Who was a part of this story?")
    select("Fernando Cabral", from:"Who was a part of this story?")
    self
end

def submit
    click_on("Create Story")
end
end

编辑

经过多次调试,我意识到测试失败是因为验证工作不正常。

看起来你的提交失败了,所以它被重定向回编辑页面,并且没有显示 "Thank you for sharing a story." 文本 - 因为你说如果你不 select 字符就可以工作我猜这是某种嵌套属性错误,但您的测试日志应该准确解释提交失败的原因。标题未显示在搜索到的文本中的原因是 元素的值(显示是因为它重定向到编辑页面)不是文本内容的一部分一页