0

I'm running Rails 4, trying to set up some integration test with Rspec and Capybara. I want to set up guard to run 'zeus test .' whenever I make changes. Problem is, whenever anything form the Capybara DSL is called, or whenever I try to use route helpers, I'm given errors like this:

undefined local variable or method `root_path' for #<RSpec::Core::ExampleGroup::Nested_2::Nested_1:0x007f55682c6d60>

If I replace root_path with '/' it get:

undefined method `visit' for #<RSpec::Core::ExampleGroup::Nested_2::Nested_1:0x007f556833bbb0>

If I just run 'rspec spec .' or 'zeus test .' it works fine.

I've tried removing the 'cmd: 'zeus test .' option from my Guardfile, but I'm having the same issues. It seems clear that the problem is with Guard and unrelated to zeus.

In my Gemfile:

group :development, :test do
   gem 'capybara'
   gem 'rspec-rails'
   gem 'factory_girl_rails'
   gem 'guard-rspec', require: false
   gem 'better_errors'
   gem 'binding_of_caller'
end

Spec helper:

ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
require 'rspec/autorun'
require 'factory_girl_rails'

Dir[Rails.root.join("spec/support/**/*.rb")].each { |f| require f }

ActiveRecord::Migration.check_pending! if defined?(ActiveRecord::Migration)

RSpec.configure do |config|

 config.include FactoryGirl::Syntax::Methods
 config.fixture_path = "#{::Rails.root}/spec/fixtures"

 config.use_transactional_fixtures = true

 config.infer_base_class_for_anonymous_controllers = false

 config.order = "random"
end

Spec I'm trying to run:

require 'spec_helper'
describe "HomePage" do
  describe "Root page" do
  before { visit root_path }
    it "works!" do
      page.status_code.should be(200)
    end
    it 'contains bottom nav buttons' do
      page.should have_link 'How it works'
      page.should have_link 'Customer Service'
      page.should have_link 'Terms of Service'
      page.should have_link 'Contact Us'
    end
  end
end
Joeman29
  • 712
  • 5
  • 14

3 Answers3

2

try add

config.include Capybara::DSL

to your spec_helper.rb

goodniceweb
  • 167
  • 1
  • 10
  • 2
    Welcome to Stack Overflow! This answer turned up in the low quality review queue, presumably because you didn't explain what this does. If you do explain this (in your answer), you are far more likely to get more upvotes—and the questioner actually learns something! – The Guy with The Hat May 05 '14 at 22:40
  • Sure. Problem is look like `Capybara` just doesnt connect to Rspec tests. When we add `config.include Capybara::DSL` to block `RSpec.configure do |config| ... end` its join Capybara DLS to your tests. Sometimes we forgot first steps. – goodniceweb May 05 '14 at 23:20
1

Try to add url_helpers to spec_helper.rb

RSpec.configure do |config|
  ...
  config.include Rails.application.routes.url_helpers
end

and check my other answer about visit method missing

Community
  • 1
  • 1
ole
  • 5,166
  • 5
  • 29
  • 57
  • Thanks, but I did mention in my question that it works fine outside of Guard. I tried this as well as config.include Capybara::DSL, but neither one helped Guard work. – Joeman29 May 12 '14 at 00:16
0

I fixed this problem by commenting out this line in my spec_helper.rb:

require 'rspec/autorun'

Again, the problem was only with Guard and I suppose that autorun somehow skips over the config block in the spec_helper, so everything works fine now. Hope this helps someone else having the same problem.

Joeman29
  • 712
  • 5
  • 14