Why are my fixture objects not available in my Rails Test :: Unit test?

According to this Rails manual , if you create a device, it will become available in your test class.

I have this device in users.yml:

<%
  stan = User.new
  stan.username = 'stan'
  stan.set_hashed_password('mysterio')
  stan.email = 'stan@abc.com'
%>

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

Following the Rails tutorial, 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
source share
4 answers

Thanks for helping the guys. I realized that I have different announcements and calls are structured incorrectly. This is not explicitly explained in our guide, but it seems to users(:stan)work only inside a block shouldor in a pure Test :: Unit inside a method test_.

+4

fixtures :users

+7

,

fixtures :all

test_helper.rb

+5

, , .

UserLoginTest, - , , . @user = users (: michael), .

In the end, I found that I duplicated the class declaration at the top of the file, copying LAZY and pasting from the tutorial. Therefore, if someone else encounters this issue with a similar problem related to broken devices, check if you made the same stupid mistake as I did and duplicated the top of the file!

require 'test_helper'

class UsersLoginTest < ActionDispatch::IntegrationTest

  def setup
    @user = users(:michael)
  end
-1
source

Source: https://habr.com/ru/post/1702861/


All Articles