Ruby on rails 为什么我的夹具对象在Rails测试::单元测试中不可用?
根据,如果您创建了一个fixture,它将在您的测试类中可用 我在Ruby on rails 为什么我的夹具对象在Rails测试::单元测试中不可用?,ruby-on-rails,testing,Ruby On Rails,Testing,根据,如果您创建了一个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
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 %>
确保你有
fixtures :all
在test_helper.rb中,尝试放置
fixtures :users
下课后谢谢大家的帮助。我意识到我的各种声明和调用结构不正确。这在我引用的指南中没有明确解释,但显然用户(:stan)
只在should
块中工作,或者在测试方法中的纯Test::Unit中工作。这是一个非常古老的问题,但我刚才在阅读rails教程时遇到了类似的问题
在我的UsersLoginTest集成测试开始时,我有一个设置方法,出于某种原因,我在其中引用的fixture在测试中似乎不起作用。我必须在测试@user=users(:michael)的顶部有一行代码才能让它工作
最后,我发现我复制了文件顶部的类声明,方法是从教程中偷懒地复制和粘贴。因此,如果其他人遇到与夹具不工作相关的类似问题,请检查您是否犯了与我相同的愚蠢错误,并复制了文件的顶部
require 'test_helper'
class UsersLoginTest < ActionDispatch::IntegrationTest
def setup
@user = users(:michael)
end
需要“测试助手”
类UsersLoginTest
是的,如果您在测试方法之外或在shoulda块之外使用它,那么您调用的是类方法,而不是实例方法。这对我来说是有效的——尽管我自己认为“当然所有的装置都加载了”——它们不是。谢谢
fixtures :users
require 'test_helper'
class UsersLoginTest < ActionDispatch::IntegrationTest
def setup
@user = users(:michael)
end