Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/67.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 为什么我的夹具对象在Rails测试::单元测试中不可用?_Ruby On Rails_Testing - Fatal编程技术网

Ruby on rails 为什么我的夹具对象在Rails测试::单元测试中不可用?

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

根据,如果您创建了一个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 %>
确保你有

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