Reputation: 60089
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'
Upvotes: 4
Views: 3595
Reputation: 61
This is a SUPER old question, but I had a similar problem just now when going through the rails tutorial book.
I have a setup method at the beginning of my UsersLoginTest integration test, and for some reason, the fixture I was referencing in that, didn't seem to be working inside the tests. I had to have a line at the top of the test @user = users(:michael) to get it working.
In the end, I found that I had duplicated the Class declaration at the top of the file, by LAZY copy and pasting from the tutorial book. So if anyone else comes across this thread with a similar problem related to fixtures not working, check if you've made the same silly mistake as me, and duplicated the top of the file!
require 'test_helper'
class UsersLoginTest < ActionDispatch::IntegrationTest
def setup
@user = users(:michael)
end
Upvotes: -1
Reputation: 60089
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.
Upvotes: 5