0
votes

I am new to rails so I may have completely missed the issue here, but I appear to be having an issue in Capybara when using tests that have a reference to my own Page class.

When I have a test that has an instance of the Page class created (via FactoryGirl) then I am unable to run any of the Capybara page methods (such as visit, fill_in, click, etc). I get the following error:

NoMethodError:
   undefined method `visit' for #<Page:0x007fdf5eb9d990>

When I add a debugger statement and check the page variable it is an instance of my Page class. How can I ensure I am always using the Capybara page when I want to execute the Capybara methods?

My feature spec (I know this is incomplete, but the error occurs in the before loop):

require 'spec_helper'

feature 'Modifying a page' do
  let(:account) { FactoryGirl.create(:account) }
  let!(:page_creator) { FactoryGirl.create(:user, :account => account) }
  let!(:standard_user) { FactoryGirl.create(:user, :account => account) }
  let!(:site) { FactoryGirl.create(:site, :account => account) }
  let!(:page) { FactoryGirl.create(:page, :account => account, :site => site, :creator => page_creator) }

  context 'as the creator of the page' do
    before(:each) { sign_in_via_form account, page_creator, page_creator.password }

    scenario 'with valid information' do
      new_page_name = Faker::Lorem.characters(Random.rand(10..100))
      new_page_url = Faker::Internet.url
      new_page_description = Faker::Lorem.characters(Random.rand(50..250))

      click_link site.name
      click_link 'Pages'
      find("a[href='#{edit_site_page_path([site, page])}'").click
    end
  end
end

The sign_in_via_form method is defined in the follwing file:

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

  def sign_in_via_form(account, user, password)
    visit "http://#{account.subdomain}.example.com"
    fill_in 'Email', :with => user.email
    fill_in 'Password', :with => password
    click_button 'Sign In'
  end
end

I have a number of other features that work against different models that are running (and using the above sign_in_via_form method without any problems, which is what makes me think it is to do with the Page class.

As I say, I'm pretty new to rails, so you'll have to forgive me if these tests look a little ugly :-)

Thanks for any help

1
have u added config.include Capybara::DSL in your spec_helper.rbRahul Singh
I hadn't, but I just added it and I am still getting the same errorDave Kirk

1 Answers

0
votes

OK, so it was so simple that I missed the real issue.

I was creating a variable using the let method called page, which was obviously overwriting the Capybara page variable. I change it to the following and it all runs as expected (note the variable the_page):

feature 'Modifying a page' do
  let(:account) { FactoryGirl.create(:account) }
  let!(:page_creator) { FactoryGirl.create(:user, :account => account) }
  let!(:standard_user) { FactoryGirl.create(:user, :account => account) }
  let!(:site) { FactoryGirl.create(:site, :account => account) }
  let!(:the_page) { FactoryGirl.create(:page, :account => account, :site => site, :creator => page_creator) }