根据此Rails指南,如果您创建一个fixture,它将在您的测试类中可用.
我有这个夹具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 %>
按照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'
DanSingerman.. 7
试试看
fixtures :users
在你的课堂宣言之后
试试看
fixtures :users
在你的课堂宣言之后
确保你有
fixtures :all
在你的test_helper.rb中
谢谢你的帮助.我意识到我有各种声明和调用结构不正确.这在我引用的指南中没有明确解释,但显然users(:stan)
只能在一个should
块内部工作,或者在一个test_
方法中的纯Test :: Unit中工作.