Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I load fixtures into a namespaced models table?

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: [email protected]
  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 ...

like image 888
toolz Avatar asked Jul 30 '14 18:07

toolz


2 Answers

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
...
like image 96
Brett Allred Avatar answered Nov 16 '22 04:11

Brett Allred


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)

like image 2
rigyt Avatar answered Nov 16 '22 05:11

rigyt