Samantha Cabral
Samantha Cabral

Reputation: 285

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

I am using Rails 4 and Ruby 2.2 with RSPEC, Capybara and FactoryGirl.

I am trying to test that a user can write a story adding characters (other users). It works in the browser, but when I run the tests, I get the following message, indicating that the title is missing:

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"

When I add save_and_open_page, I can see that the title has been inserted. When I remove the lines to select characters, the tests pass.

Here is the test file:

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

And here is the new_story_form support file:

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

Edit

After much debugging, I realized that the tests are failing because of a validation that is not working correctly.

Upvotes: 0

Views: 182

Answers (1)

Thomas Walpole
Thomas Walpole

Reputation: 49890

It looks like your submit is failing and so it's redirecting back to the edit page, and not showing the "Thank you for sharing a story." text - Since you say it works if you don't select the characters I would guess it's some kind of nested attributes error, but your test log should explain exactly why the submit is failing. The reason the title isn't shown in the text searched is because the value of an <input type="text"/> element (which is being shown because it's redirecting to the edit page) is not part of the text content of a page

Upvotes: 0

Related Questions