Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you solve FixtureClassNotFound: No class attached to find

When running my tests I get this error:

FixtureClassNotFound: No class attached to find

What causes this error and how to fix it?

like image 938
Yoni Baciu Avatar asked Aug 11 '11 03:08

Yoni Baciu


2 Answers

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

like image 99
Yoni Baciu Avatar answered Nov 11 '22 18:11

Yoni Baciu


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

like image 34
Rob Di Marco Avatar answered Nov 11 '22 19:11

Rob Di Marco