Skip to content
This repository

can't use fixtures with a created engine #4971

Open
viniciusgati opened this Issue February 09, 2012 · 22 comments
viniciusgati

hi all, i have trouble testing an engine

rails plugin new something --full

i create an engine, and some models, then i started to test, on 'rake test' command i realized that the fixtures are not loaded, so as the helper methods to load fixtures like:

people(:one)

it is a bug or maybe i understand this wrong and I'm using it the wrong way?

Toshinori Kajihara
Collaborator

Hi! @viniciusgati
Is this issue related to #5193 ?
or auto loading problem ???

Sorry, If I misunderstand.

viniciusgati

yeah the load fixtures issue is because of that issue, but i cant get the helper methods on model_plural(:fixture) only with this fix:

ActiveSupport::TestCase.fixture_path = File.expand_path("../fixtures", __FILE__) 

i will get the new version and try it again

Arun Agrawal
Collaborator

Issue still exists?

viniciusgati

yes...

~/Code/ruby/testes/something $ rails -v
Rails 3.2.3

viniciusgati

ok it won't work directly, but when i opened the PeopleControllerTest and write

fixtures :all in his body the tests worked...

I think the "fixtures :all" line of code should come with the generated code of the scaffold, because the generated code should run at first time, with no changes or fixes...

Tanguy Krotoff

Hi all, I have the same issue using Rails 3.2.3. Scaffolding inside a Rails engine contains several bugs (exluding issue 5193 which is fixed). Steps to reproduce the problems:

# Create a new mountable engine
rails plugin new issue4971 --mountable --full

cd issue4971/

# Scaffolding, generates among other files:
# - test/unit/issue4971/my_model_test.rb
# - test/fixtures/issue4971/my_models.yml
# - test/functional/issue4971/my_models_controller_test.rb
rails g scaffold MyModel title:string description:text

# Create the database
rake db:migrate

# Run the unit tests
# This is OK
rake app:test:units

# Run the functional tests
# This fails with: NoMethodError: undefined method `my_models'
rake app:test:functionals

Edit file test_helper.rb and add this at the end:

class ActiveSupport::TestCase
  fixtures :all
end

fixtures :all is missing with engines, with a regular Rails app it is included. --> first issue

And now edit file my_models_controller_test.rb and change the line @my_model = my_models(:one) by @my_model = issue4971_my_models(:one) because of the namespace. --> second issue

Re-run rake app:test:functionals or rake test, there are still some errors: NoMethodError: undefined method my_model_path. --> third issue
And here I'm stuck...

Edit:
To fix the third issue, edit file test_helper.rb and add this at the end:

# Fix routing inside the engine when testing
# See https://github.com/rails/journey/issues/39
module Issue4971
  class ActionController::TestCase
    setup do
      @routes = Engine.routes
    end
  end
end

See rails/journey#39

The following code is also missing from test_helper.rb:

if ActionDispatch::IntegrationTest.method_defined?(:fixture_path=)
  ActionDispatch::IntegrationTest.fixture_path = File.expand_path("../fixtures", __FILE__)
end

Without it integration tests cannot use fixtures.
See #5193

Tanguy Krotoff

You can close this issue.
The third issue I was reporting about routing and engines (NoMethodError: undefined method my_model_path) has been fixed here: #6573

Please test and make sure that the generated tests for a newly generated plugin are working!

Steve Klabnik
Collaborator

It does seem that #6573 has closed this, yes. Feel free to re-open if I'm wrong.

Steve Klabnik steveklabnik closed this June 27, 2012
SixArm

This is step is still breaking in Rails 3.2.7 on a fresh vanilla engine:

rake app:test:functionals #=> fails

My app is called "Access" and the scaffold creates a "Role" class. Here's one of the test error messages:

Error:
test_should_create_role(Access::RolesControllerTest):
NoMethodError: undefined method `roles' for #<Access::RolesControllerTest:0x000000032a1888>
/home/sixarm/git/access/vendor/bundle/ruby/1.9.1/gems/actionpack-3.2.7/lib/action_dispatch/testing/assertions/routing.rb:176:in `method_missing'
/home/sixarm/git/access/test/functional/access/roles_controller_test.rb:6:in `block in <class:RolesControllerTest>'
/home/sixarm/git/access/vendor/bundle/ruby/1.9.1/gems/activesupport-3.2.7/lib/active_support/callbacks.rb:462:in `_run__222176480460835741__setup__217017113298252779__callbacks'
/home/sixarm/git/access/vendor/bundle/ruby/1.9.1/gems/activesupport-3.2.7/lib/active_support/callbacks.rb:405:in `__run_callback'
/home/sixarm/git/access/vendor/bundle/ruby/1.9.1/gems/activesupport-3.2.7/lib/active_support/callbacks.rb:385:in `_run_setup_callbacks'
/home/sixarm/git/access/vendor/bundle/ruby/1.9.1/gems/activesupport-3.2.7/lib/active_support/callbacks.rb:81:in `run_callbacks'
/home/sixarm/git/access/vendor/bundle/ruby/1.9.1/gems/activesupport-3.2.7/lib/active_support/testing/setup_and_teardown.rb:35:in `run'
murdoch

This is still very much an issue in 3.2.8. The instructions provided by @tkrotoff resolve the issue.

To be clear, I had to do the following:

1 - Add this to test_helper.rb

class ActiveSupport::TestCase
  fixtures :all
end

if ActionDispatch::IntegrationTest.method_defined?(:fixture_path=)
  ActionDispatch::IntegrationTest.fixture_path = File.expand_path("../fixtures", __FILE__)
end

module Blorgh
  class ActionController::TestCase
    setup do
      @routes = Engine.routes
    end
  end
end

2 - Manually prepend the name of the engine to the fixture method (in my case posts)

class PostsControllerTest < ActionController::TestCase
  setup do
    @post = blorgh_posts(:one)
  end

Also seen in Rails 3.2.4, 3.2.5, 3.2.6 and 3.2.7.

Thanks to @tkrotoff for kindly posting the instructions to fix this.

Can someone reopen the issue please, or is this going to be fixed in 4.0?

Steve Klabnik steveklabnik reopened this October 11, 2012
Steve Klabnik
Collaborator

I've re-opened it since you've seen it happening.

Steve Klabnik
Collaborator

Actually, yes, this should only be in rails 4, and not 3.2.8. Can you test on edge possibly, and see if it works? @drogus is this worth a backport?

murdoch

The problem seems to manifest in a similar way on Rails 4.0.0.beta, although this time none of the fixes detailed above resolve the problem.

With fixes from above, I see the following upon running rake test:

Errno::ENOENT: No such file or directory - /blorgh/test/fixtures/posts.yml

It seems to be ignoring the fact that fixtures dir is namespaced, it should be looking for blorgh/test/fixtures/blorgh/posts.yml

If I reference the blorgh directory in my test_helper like so:

ActiveSupport::TestCase.fixture_path = File.expand_path("../fixtures/blorgh", __FILE__)

Then I get the following:

ActiveRecord::StatementInvalid: SQLite3::SQLException: no such table: posts: DELETE FROM "posts"

This is strange because dev and test db's are definitely present in blorgh/test/dummy/db although I wonder if it should be looking for a table called blorgh_posts instead of just posts

I've tried rake db:migrate RAILS_ENV=test, as well as rake app:db:migrate from the root of the engine as well as the root of the dummy app. I've also tried rake app:blorgh:install:migrations, but nothing seems to get this fixed.

At least I got a good look at Rails4.

Leandro Nunes

The ActiveRecord looking for a table with the same name of your fixture.
You're probably calling:

    setup do
        @post = posts(:one)
    end

So you need to tell the table of your fixture has a different name, include that in your test_helper in the block where it is loading the fixtures

    ActiveSupport::TestCase.set_fixture_class :posts => Blorgh::Post
JEONG, MunShik

Thank you @murdoch, @tkrotoff.

I am using 3.2.9 and solution from @murdoch worked well.

rene paulokat
erpe commented March 03, 2013

this one is still an issue with 3.2.13.rc1 - above described solution works.

Jamie Hill

I'm having the same problem as the initial question with Rails 4 beta. Are there any known fixes? Fixtures not getting loaded at all!

Jamie Hill

Basically integration tests in engines do not have access to fixtures, the db is empty.

Colin Mattson

This appears to still be an issue in 4.0.0.rc2. I'm running into it both in an engine I'm working on and @tkrotoff's reduced example. Tested both MRI 1.9.3-p429 and 2.0.0-p195 in case it was interpreter-related.

As the generator creates things:

NoMethodError: undefined method `my_models' for #<Issue4971::MyModelsControllerTest:0x007fde4bc3c0b8>

Prepending the namespace as @murdoch found necessary for 3.2.8:

NoMethodError: undefined method `issue4971_my_models' for #<Issue4971::MyModelsControllerTest:0x007fde4bc3c0b8>
Christian Rodriguez

I have the same problem in 4.0.0 only with models, and fixed it editing test_helper.rb as:

# Configure Rails Environment
ENV["RAILS_ENV"] = "test"

require File.expand_path("../dummy/config/environment.rb",  __FILE__)
require "rails/test_help"

Rails.backtrace_cleaner.remove_silencers!

# Load support files
Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each { |f| require f }

# Load fixtures from the engine
if ActiveSupport::TestCase.method_defined?(:fixture_path=)
  ActiveSupport::TestCase.fixture_path = File.expand_path("../fixtures", __FILE__)
end

ActiveSupport::TestCase.fixture_path = File.expand_path("../fixtures", __FILE__)

class ActiveSupport::TestCase
    fixtures :all 
end

It seems that if ActiveSupport::TestCase.method_defined?(:fixture_path=) returns false always

onemanstartup

If you experiencing this problem you can fix this by appending to fixtures path name of your engine like that

ActiveSupport::TestCase.fixture_path = File.expand_path("../fixtures/engine_name", __FILE__)
Mike Szyndel

Problem is also present in Rails 4!
For reference for people coming here with the same problem.

I got my engine tests working under Rails 4.0.1 and Ruby 2.0 doing following:

In test_helper.rb

# outside of if block
ActiveSupport::TestCase.fixture_path = File.expand_path("../fixtures", __FILE__)

class ActiveSupport::TestCase
  fixtures :all
end

#fixes other problems with controller tests (url helpers)
class ActionController::TestCase
  setup do
    @routes = Bubbles::Engine.routes
  end
end

and in tests where you want to use fixtures - prefix fixture helpers, like:

blorgh_posts(:one)

From other proposed solutions: changing fixture_path doesn't have any effect (in Rails 4.0.1)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.