如何解决 FixtureClassNotFound: No class Attached to find
运行测试时出现此错误:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
发生这种情况很可能是因为模型使用了自定义表名称(使用 set_table_name)或者模型位于模块中。
要解决这个问题,您需要在
test_helper.rb
中的fixtures :all
行之前添加set_fixture_class
行:在这种情况下,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 thetest_helper.rb
before thefixtures :all
line:In this case the fixtures file should be called my_table_name.yml
注意:如果包含堆栈跟踪和完整的错误消息,将会很有帮助。
在您的 test/test_helper.rb 类中,有一行类似
This 告诉框架在目录 test/fixtures 中查找并尝试加载它在那里看到的每个 YAML 文件,然后将它们保存到数据库中。所以我的预感是你有一个文件在 app/models 中没有具有单数名称的类。换句话说,如果有一个文件 test/fixtures/posts.yml,那么当您运行测试时,框架将查找名为 Post 的类来加载您的数据。
所以我要做的第一件事就是检查以查看如果您有一个与模型类之一不关联的固定装置文件(也许您删除了模型但忘记删除固定装置?)
如果这不起作用,请尝试更改测试助手中的行以显式加载固定装置你需要。因此,如果您只想加载名为 Post 的对象和名为 User 的对象的固定装置,则将 test_helper.rb 中的: 更改为:
,
您应该会看到错误消失(尽管现在可能会出现其他错误,因为您的固定装置未加载。 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
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:
to
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
如果该类是命名不规范的类,例如
fish
、sms
它可以通过使用
--force-plural
标志创建IE
rails g model sms --force-plural
在这种情况下,您将设置一个在下面设置的变形
config/initializers/inflections.rb
这样的一个例子就是这样,
通过这种方式,可以在您声明它时识别该类
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
flagi.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
In this way the class can be recognized as you declared it
当我生成一些脚手架代码时出错时,我收到了此错误。我使用了复数模型名称,我猜这只是混淆了夹具加载。使用单一模型名称重新生成支架解决了问题。
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.