【问题标题】:FixtureClassNotFound: No class attached to findFixtureClassNotFound:没有附加类来查找
【发布时间】:2011-09-16 06:56:12
【问题描述】:

这是我的问题。如果我使用 ruby​​ 命令运行测试,则测试全部通过。但是,如果我使用 rake test:functionals,它们都会失败并出现以下错误。我不确定在使用 rake 任务时我必须采取哪些不同的措施才能让他们通过,但我非常感谢您提供的任何指导。

#tasks_controller_test.rb
require File.dirname(__FILE__) + '/../test_helper'

class TasksControllerTest < ActionController::TestCase
    set_fixture_class :Action => 'Task'
    fixtures :Action

    setup :initialize_test

    test "the truth" do
        assert true
    end

    private
    def initialize_test
        @user = users :one
        sign_in @user
        @user.confirm!

        @task = Action :one    # Here's the line that is throwing the error.
    end
end

1) Error:
    test_the_truth(TasksControllerTest):
        FixtureClassNotFound: No class attached to find.
        test/functional/tasks_controller_test.rb:20:in `initialize_test'

【问题讨论】:

  • 如果我理解正确,您有一个Task 模型,其表名为Action。这意味着您应该拥有app/models/task.rbtest/fixtures/Action.yml(或csv)。我说对了吗?

标签: ruby-on-rails-3 functional-testing


【解决方案1】:

该错误表明 Rails 无法确定要为测试加载什么夹具。

我假设夹具名为 Action.yml。 Rails 是否接受包含大写字母的夹具文件名?

根据http://apidock.com/rails/ActiveRecord/TestFixtures/ClassMethods/set_fixture_class 的文档,set_fixture_class 需要一个类名而不是字符串。

所以我会尝试:

set_fixture_class :myaction => Task

我会将fixture文件重命名为myaction.yml

【讨论】:

    【解决方案2】:

    这是一种非常奇怪的处理问题的方式。

    使用开箱即用的导轨,如果您有固定装置,它们会在每次测试之前加载它们。

    您无需在控制器测试中显式加载它们。

    http://guides.rubyonrails.org/testing.html#the-low-down-on-fixtures

    Rails 默认自动从 用于单元和功能测试的 test/fixtures 文件夹。正在加载 涉及三个步骤:

    【讨论】:

    • 只有当您的模型直接映射到表格时才如此。这是必需的,因为我使用的是旧数据库。所以我的问题仍然存在。 :(
    【解决方案3】:

    似乎没有人能够解决这个问题。所以我现在只是关闭它。也许我会切换到 Factory_Girl 或 Machinist。

    【讨论】:

      猜你喜欢
      • 2015-06-24
      • 2014-02-09
      • 2011-08-04
      • 1970-01-01
      • 2021-08-23
      • 2018-12-13
      • 2018-07-28
      • 2014-11-05
      • 1970-01-01
      相关资源
      最近更新 更多