Michael Hartl Rails Tutorial (3.2) - Chapter 5 Rspec issue

Go To StackoverFlow.com

1

I've completed Chapter 5 and all the exercises. As part of the final exercise, MH has you write tests for the ApplicationHelper method.

Once that is done, individual tests pass with:

  • shift-command-T in ST2 (using the sublime-text-2-ruby-tests package)
  • rspec spec/requests/user_pages_spec.rb in the terminal
  • rspec spec/requests/static_pages_spec.rb in the terminal

However, if I run all tests with rspec spec/ in the terminal I get this error:

/Users/anonymouscoward/rails/railstut/sample_app/spec/helpers/
application_helper_spec.rb:1:in `<top (required)>': uninitialized
constant ApplicationHelper (NameError)

If I run spork and try rspec spec/ again, all tests pass.

I'm not sure I understand why this works the way it does, or whether I'll always need to have spork running in the future when I want to run all my tests. Thanks.

2012-04-05 22:39
by rda3000
Which code is on line 1 in applicationhelperspec.rb - Daniel Blaichinger 2012-04-05 22:43


3

I'm not sure, but maybe you forgot to require spec_helper. If so, just insert this in the first line of application_helper_spec.rb

require 'spec_helper'
2012-04-05 22:51
by Daniel Blaichinger
That was it, thanks. If I understand correctly, the Spork.prefork block in spec_helper.rb loads the Rails environment and makes ApplicationHelper available - rda3000 2012-04-06 03:26


0

I ran into the same issue. Thanks for the fix!

I found this response on another forum, which seems helpful in thinking about how Spork relates to Rspec.

RSpec does not "run" spec_helper. By default, rspec loads files that end with "_spec.rb" and it is up to those files to require spec_helper.rb. http://www.ruby-forum.com/topic/167768

2012-04-10 21:33
by Brett Sanders


0

describe "ApplicationHelper" do

ApplicationHelper should be a string, not constant

2014-06-17 13:23
by ken
No, it doesn't have to be. rspec allows class types in the describe clause. It will extrapolate the name when it prints it in the rspec output. It is actually preferable to do it this way to take advantage of rspec's implicit subject feature - Javid Jamae 2014-07-09 00:40
Ads