根據this Rails guide,如果您創建了一個燈具,它將在您的測試課程中可用。爲什麼在我的Rails Test :: Unit測試中我的fixture對象不可用?
我有這樣的支架,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 %>
繼Rails的指導,我試圖訪問它是這樣的:
class SessionsControllerTest < ActionController::TestCase
@user = users(:stan)
# ...
end
我得到這個錯誤:
./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'
是的,如果您在測試方法之外或應用程序塊之外使用它,那麼您將調用類方法,而不是實例方法。 – 2009-02-08 03:26:30