在 Rails 中加载固定装置

Load fixtures in Rails

我正在为模型编写测试 Friends::New。我的 new_test.rb 文件看起来像:

require 'test_helper'

class Friends::New < ActiveSupport::TestCase
  fixtures :news.yml
end

我的 news.yml 位于 test/fixtures/friends/news.yml。但是在 运行 测试期间,它试图在 test/fixtures/ 中找到 news.yml。我如何告诉它在 test/fixtures/friends 中搜索 yml 文件?

你需要明确地告诉rails。

class Friends::New < ActiveSupport::TestCase
  set_fixture_class 'friends/news' => Friends::New
end

阅读set_fixture_class

Sets the model class for a fixture when the class name cannot be inferred from the fixture name.