views:

414

answers:

3

According to this Rails guide, if you create a fixture, it becomes available in your test class.

I have this fixture in users.yml:

<%
  stan = User.new
  stan.username = 'stan'
  stan.set_hashed_password('mysterio')
  stan.email = '[email protected]'
%>

stan:
  username: <%= stan.username %>
  hashed_password: <%= stan.hashed_password %>
  password_salt: <%= stan.password_salt %>
  email: <%= stan.email %>

Following the Rails guide, I'm trying to access it like this:

class SessionsControllerTest < ActionController::TestCase

  @user = users(:stan)

  # ...

end

I get this error:

./test/functional/sessions_controller_test.rb:5: undefined method `users' for SessionsControllerTest:Class (NoMethodError)
    from /usr/local/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in `gem_original_require'
    from /usr/local/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in `require'
    from /usr/local/lib/ruby/gems/1.8/gems/activesupport-2.2.2/lib/active_support/dependencies.rb:155:in `require'
+3  A: 

Make sure you have

fixtures :all

In your test_helper.rb

Jarin Udom
+3  A: 

Try putting

fixtures :users

after your class declaration

DanSingerman
A: 

Thanks for the help guys. I realized that I had various declarations and calls structured incorrectly. This isn't clearly explained in the guide I cited, but apparently users(:stan) only works inside a should block, or in pure Test::Unit inside a test_ method.

Ethan
Yes, if you use it outside of the test methods or outside a shoulda block then you're calling a class method, not an instance method.
Andrew Vit