为什么我的夹具对象在 Rails Test::Unit 测试中不可用? [英] Why are my fixture objects not available in my Rails Test::Unit test?

查看:30
本文介绍了为什么我的夹具对象在 Rails Test::Unit 测试中不可用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据本Rails 指南,如果您创建了一个fixture,它就会变得可用在你的测试课上.

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

我在 users.yml 中有这个装置:

I have this fixture 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 %>

按照 Rails 指南,我尝试像这样访问它:

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

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'

推荐答案

感谢各位的帮助.我意识到我有各种声明和调用的结构不正确.这在我引用的指南中没有明确解释,但显然 users(:stan) 只能在 should 块内工作,或者在 should 块内或在 <代码>测试_方法.

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.

这篇关于为什么我的夹具对象在 Rails Test::Unit 测试中不可用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆