Question

My model is CWB::Account (they are namespaced because I inherited this project and they used a few reserved words as model names)

I'm using minitest-spec-rails gem for testing and I'm getting this error -

ActiveRecord::FxitureClassNotFound: No class attached to find.

test/test-helper.rb

...

class ActiveSupport::TestCase
  fixtures :all
end
...

test/controllers/sessions_controller_test.rb

require 'test_helper'

class SessionsControllerTest < ActionDispatch::IntegrationTest
  before do
    account_one = Accounts(:account_one)
    register(account_one)
  end
...

test/fixtures/accounts.yml

account_one:
  id: 1
  name: testnameone
  email: one@gmail.com
  password_hash: passwordone
...

If I do this in test_helper.rb

set_fixture_class :accounts => 'CWB::Account'
fixtures :all

I get error - StandardError no fixture named <CWB::Account:0x0000004bdf32> found for fixture set 'accounts'

EDIT

Interesting update, if I put account.yml in fixtures/cwb/accounts.yml I get a bunch of errors about circular dependencies

but if I put it in fixture/CWB/accounts.yml (notice uppercase) I get an error saying

undefined method accounts for ...

Was it helpful?

Solution

1) put your fixture in /test/fixtures/cwb/accounts.yml

2) call cwb_accounts(:account_one) to get the record

require 'test_helper'

class SessionsControllerTest < ActionDispatch::IntegrationTest
  before do
    account_one = cwb_accounts(:account_one)
    register(account_one)
  end
...

OTHER TIPS

I had the same error and have combined everything I did to resolve it into this answer.

From Brett Allred's answer, but loading just the specific named fixture: (after putting the fixture in /test/fixtures/cwb/accounts.yml)

require 'test_helper'

class SessionsControllerTest < ActionDispatch::IntegrationTest

  fixtures: 'cwb/accounts'

  before do
    account_one = cwb_accounts(:account_one)
    ...

From infused's answer, but assuming namespaced table names (as in a full engine):

set_fixture_class 'cwb/accounts' => CWB::Account

I'm actually using rspec and namespaced table names. I think the only difference for the OP would be

set_fixture_class 'accounts' => CWB::Account

as his table is not namespaced (just the model)

The class should be the actual class, not a string:

set_fixture_class :accounts => CWB::Account
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top