如何解决 FixtureClassNotFound: No class Attached to find

发布于 2024-11-29 03:37:10 字数 143 浏览 1 评论 0原文

运行测试时出现此错误:

FixtureClassNotFound: No class attached to find

What Causes this error and how to fix it?

When running my tests I get this error:

FixtureClassNotFound: No class attached to find

What causes this error and how to fix it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

椵侞 2024-12-06 03:37:10

发生这种情况很可能是因为模型使用了自定义表名称(使用 set_table_name)或者模型位于模块中。

要解决这个问题,您需要在 test_helper.rb 中的 fixtures :all 行之前添加 set_fixture_class 行:

class ActiveSupport::TestCase

  self.use_transactional_fixtures = true
  .
  .
  .
  set_fixture_class my_table_name: MyModule::MyClass

  fixtures :all

end

在这种情况下,fixtures 文件应该称为 my_table_name.yml

Most likely this happens because a custom table name is used for a Model (using the set_table_name) or the model is in a module.

To solve, you need to add a set_fixture_class line in the test_helper.rb before the fixtures :all line:

class ActiveSupport::TestCase

  self.use_transactional_fixtures = true
  .
  .
  .
  set_fixture_class my_table_name: MyModule::MyClass

  fixtures :all

end

In this case the fixtures file should be called my_table_name.yml

假面具 2024-12-06 03:37:10

注意:如果包含堆栈跟踪和完整的错误消息,将会很有帮助。

在您的 test/test_helper.rb 类中,有一行类似

fixtures :all

This 告诉框架在目录 test/fixtures 中查找并尝试加载它在那里看到的每个 YAML 文件,然后将它们保存到数据库中。所以我的预感是你有一个文件在 app/models 中没有具有单数名称的类。换句话说,如果有一个文件 test/fixtures/posts.yml,那么当您运行测试时,框架将查找名为 Post 的类来加载您的数据。

所以我要做的第一件事就是检查以查看如果您有一个与模型类之一不关联的固定装置文件(也许您删除了模型但忘记删除固定装置?)

如果这不起作用,请尝试更改测试助手中的行以显式加载固定装置你需要。因此,如果您只想加载名为 Post 的对象和名为 User 的对象的固定装置,则将 test_helper.rb 中的: 更改为:

fixtures :all

fixtures :posts, :users

您应该会看到错误消失(尽管现在可能会出现其他错误,因为您的固定装置未加载。 0

NOTE: It would be helpful if you included the stack trace and the full error message.

In your test/test_helper.rb class, there is a line like

fixtures :all

This tells the framework to look in the directory test/fixtures and try to load each of the YAML files that it sees there and then save them to the DB. So my hunch is that you have a file in there that does not have class in app/models with the singularized name. In other words, if there is a file test/fixtures/posts.yml, then when you run your tests, the framework will look for a class named Post to load your data in.

So the first thing I would do is check to see if you have a fixture file that is not associated with one of your model classes (maybe you deleted a model but forgot to delete the fixture?)

If that doesn't work, try changing the line in your test helper to explicitly load the fixtures you need. So if you only want to load fixtures for an object named Post and an object named User, you will change:

fixtures :all

to

fixtures :posts, :users

in test_helper.rb and you should see the error go away (although other errors may now appear because your fixtures are not loaded.0

去了角落 2024-12-06 03:37:10

如果该类是命名不规范的类,例如 fishsms
它可以通过使用 --force-plural 标志创建
IE
rails g model sms --force-plural

在这种情况下,您将设置一个在下面设置的变形
config/initializers/inflections.rb

这样的一个例子就是这样,

ActiveSupport::Inflector.inflections(:en) do |inflect|
  inflect.plural /^(ox)$/i, '\1en'
  inflect.singular /^(ox)en/i, '\1'
  inflect.irregular 'person', 'people'
  inflect.uncountable %w( fish sheep )
end

通过这种方式,可以在您声明它时识别该类

In the Event that the Class is an irregular class in terms of naming such as fish, sms
it could have been created by using the --force-plural flag
i.e
rails g model sms --force-plural

in that case you would set up an inflection which is set up under
config/initializers/inflections.rb

an example of such is this

ActiveSupport::Inflector.inflections(:en) do |inflect|
  inflect.plural /^(ox)$/i, '\1en'
  inflect.singular /^(ox)en/i, '\1'
  inflect.irregular 'person', 'people'
  inflect.uncountable %w( fish sheep )
end

In this way the class can be recognized as you declared it

污味仙女 2024-12-06 03:37:10

当我生成一些脚手架代码时出错时,我收到了此错误。我使用了复数模型名称,我猜这只是混淆了夹具加载。使用单一模型名称重新生成支架解决了问题。

I got this error when I made a mistake generating some scaffold code. I used a plural model name, and that just confused the fixture loading, I guess. Regenerating the scaffold with a singular model name fixed the problem.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文