2

Capybara 2 removed these and suggests to separate them, but we have some situations where we'd like to use both in a test (enabling an api key through the view, then hitting the api, etc).

I tried including include ::Rack::Test::Methods but I'm getting:

undefined local variable or method `app' for #<RSpec::Core::ExampleGroup::Nested_1::Nested_1:0x007fb737932ba0>
Brian Armstrong
  • 19,707
  • 17
  • 115
  • 144

3 Answers3

3

I ran into the same error using Rails and RSpec to test an API. I found a helpful blog post for Rails 2.3: http://eddorre.com/posts/using-rack-test-and-rspec-to-test-a-restful-api-in-rails-23x

module ApiHelper
  require 'rack/test'
  include Rack::Test::Methods

  def app
    ActionController::Dispatcher.new
  end
end

My solution for Rails 3.2 was (look in config.ru for MyAppName):

module ApiHelper
  require 'rack/test'
  include Rack::Test::Methods

  def app
    MyAppName::Application
  end
end
Ultrasaurus
  • 3,031
  • 2
  • 33
  • 52
0

try this

def app
  Rails.application
end
Zoran Kikic
  • 247
  • 3
  • 7
0

For anyone else banging their heads against the wall getting the "NameError: undefined local variable or method `app'" error. It also happens when you are running all your tests at once (multiple files) and one of them does an include Rack::Test::Methods - the include "infects" the other tests. So the symptom is that all tests pass when the files are run individually, but then they fail with the "no app" error when run together. At least this happens with rails 3.0.9 and rspec 3.0

The solution to this issue is to remove the includes. Alternatively you might try something like @ultrasaurus' answer to make sure the includes are properly contained to only the examples that need it.

Nico Brenner
  • 690
  • 7
  • 11