New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

can't use fixtures with a created engine #4971

Closed
viniciusgati opened this Issue Feb 9, 2012 · 29 comments

Comments

Projects
None yet
@viniciusgati

viniciusgati commented Feb 9, 2012

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?

@kennyj

This comment has been minimized.

Show comment
Hide comment
@kennyj

kennyj Mar 13, 2012

Contributor

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

Sorry, If I misunderstand.

Contributor

kennyj commented Mar 13, 2012

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

Sorry, If I misunderstand.

@viniciusgati

This comment has been minimized.

Show comment
Hide comment
@viniciusgati

viniciusgati Mar 13, 2012

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

viniciusgati commented Mar 13, 2012

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

@arunagw

This comment has been minimized.

Show comment
Hide comment
@arunagw

arunagw Apr 30, 2012

Member

Issue still exists?

Member

arunagw commented Apr 30, 2012

Issue still exists?

@viniciusgati

This comment has been minimized.

Show comment
Hide comment
@viniciusgati

viniciusgati May 1, 2012

yes...

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

viniciusgati commented May 1, 2012

yes...

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

@viniciusgati

This comment has been minimized.

Show comment
Hide comment
@viniciusgati

viniciusgati May 5, 2012

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...

viniciusgati commented May 5, 2012

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...

@tkrotoff

This comment has been minimized.

Show comment
Hide comment
@tkrotoff

tkrotoff May 13, 2012

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

tkrotoff commented May 13, 2012

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

@tkrotoff

This comment has been minimized.

Show comment
Hide comment
@tkrotoff

tkrotoff Jun 2, 2012

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!

tkrotoff commented Jun 2, 2012

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!

@steveklabnik

This comment has been minimized.

Show comment
Hide comment
@steveklabnik

steveklabnik Jun 27, 2012

Member

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

Member

steveklabnik commented Jun 27, 2012

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

@ghost

This comment has been minimized.

Show comment
Hide comment
@ghost

ghost Aug 2, 2012

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'

ghost commented Aug 2, 2012

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 comment has been minimized.

Show comment
Hide comment
@murdoch

murdoch Oct 11, 2012

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?

murdoch commented Oct 11, 2012

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?

@steveklabnik steveklabnik reopened this Oct 12, 2012

@steveklabnik

This comment has been minimized.

Show comment
Hide comment
@steveklabnik

steveklabnik Oct 12, 2012

Member

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

Member

steveklabnik commented Oct 12, 2012

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

@steveklabnik

This comment has been minimized.

Show comment
Hide comment
@steveklabnik

steveklabnik Oct 12, 2012

Member

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?

Member

steveklabnik commented Oct 12, 2012

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

This comment has been minimized.

Show comment
Hide comment
@murdoch

murdoch Oct 12, 2012

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.

murdoch commented Oct 12, 2012

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.

@LeandroSNunes

This comment has been minimized.

Show comment
Hide comment
@LeandroSNunes

LeandroSNunes Oct 26, 2012

Contributor

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
Contributor

LeandroSNunes commented Oct 26, 2012

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
@ruseel

This comment has been minimized.

Show comment
Hide comment
@ruseel

ruseel Nov 28, 2012

Thank you @murdoch, @tkrotoff.

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

ruseel commented Nov 28, 2012

Thank you @murdoch, @tkrotoff.

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

@erpe

This comment has been minimized.

Show comment
Hide comment
@erpe

erpe Mar 3, 2013

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

erpe commented Mar 3, 2013

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

@thelucid

This comment has been minimized.

Show comment
Hide comment
@thelucid

thelucid Mar 19, 2013

Contributor

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!

Contributor

thelucid commented Mar 19, 2013

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!

@thelucid

This comment has been minimized.

Show comment
Hide comment
@thelucid

thelucid Mar 19, 2013

Contributor

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

Contributor

thelucid commented Mar 19, 2013

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

@cmattson

This comment has been minimized.

Show comment
Hide comment
@cmattson

cmattson Jun 18, 2013

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>

cmattson commented Jun 18, 2013

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>
@chrodriguez

This comment has been minimized.

Show comment
Hide comment
@chrodriguez

chrodriguez Jun 27, 2013

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

chrodriguez commented Jun 27, 2013

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

This comment has been minimized.

Show comment
Hide comment
@onemanstartup

onemanstartup Nov 8, 2013

Contributor

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__)
Contributor

onemanstartup commented Nov 8, 2013

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__)
@hajder

This comment has been minimized.

Show comment
Hide comment
@hajder

hajder Nov 24, 2013

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)

hajder commented Nov 24, 2013

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)

@viniciusgati viniciusgati added the stale label Apr 23, 2014

@rafaelfranca

This comment has been minimized.

Show comment
Hide comment
@rafaelfranca

rafaelfranca May 1, 2014

Member

This issue has been automatically marked as stale because it has not been commented on for at least
three months.

The resources of the Rails team are limited, and so we are asking for your help.

If you can still reproduce this error on the 4-1-stable, 4-0-stable branches or on master,
please reply with all of the information you have about it in order to keep the issue open.

Thank you for all your contributions.

Member

rafaelfranca commented May 1, 2014

This issue has been automatically marked as stale because it has not been commented on for at least
three months.

The resources of the Rails team are limited, and so we are asking for your help.

If you can still reproduce this error on the 4-1-stable, 4-0-stable branches or on master,
please reply with all of the information you have about it in order to keep the issue open.

Thank you for all your contributions.

@rails-bot

This comment has been minimized.

Show comment
Hide comment
@rails-bot

rails-bot May 27, 2014

This issue has been automatically closed because of inactivity.

If you can still reproduce this error on the 4-1-stable, 4-0-stable branches or on master,
please reply with all of the information you have about it in order to keep the issue open.

Thank you for all your contributions.

rails-bot commented May 27, 2014

This issue has been automatically closed because of inactivity.

If you can still reproduce this error on the 4-1-stable, 4-0-stable branches or on master,
please reply with all of the information you have about it in order to keep the issue open.

Thank you for all your contributions.

@rails-bot rails-bot closed this May 27, 2014

@MichaelSp

This comment has been minimized.

Show comment
Hide comment
@MichaelSp

MichaelSp Aug 25, 2014

Just reproduced this in Rails 4.1.5

Steps to reproduce:

$ rails plugin new blog --mountable
$ cd blog
$ rails g scaffold Post tile:string content:text
$ rake db:migrate
$ rake test
  • First issue: Minitest::UnexpectedError: NoMethodError: undefined methodposts' for #Blog::PostsControllerTest:0x000000045bf410`
  • Work around:
    • posts --> blog_posts and
    • test_helper.rb
#if ActiveSupport::TestCase.method_defined?(:fixture_path=)
  ActiveSupport::TestCase.fixture_path = File.expand_path("../fixtures", __FILE__)
#end
class ActiveSupport::TestCase
  fixtures :all
end
  • Second issue: Minitest::UnexpectedError: ActionController::UrlGenerationError: No route matches {:action=>"create", :controller=>"blog/posts", :post=>{:content=>"MyText", :tile=>"MyString"}}
  • Work around:
    • test_helper.rb
module Blog
  class ActionController::TestCase
    setup do
      @routes = Engine.routes
    end
  end
end

So please reopen

MichaelSp commented Aug 25, 2014

Just reproduced this in Rails 4.1.5

Steps to reproduce:

$ rails plugin new blog --mountable
$ cd blog
$ rails g scaffold Post tile:string content:text
$ rake db:migrate
$ rake test
  • First issue: Minitest::UnexpectedError: NoMethodError: undefined methodposts' for #Blog::PostsControllerTest:0x000000045bf410`
  • Work around:
    • posts --> blog_posts and
    • test_helper.rb
#if ActiveSupport::TestCase.method_defined?(:fixture_path=)
  ActiveSupport::TestCase.fixture_path = File.expand_path("../fixtures", __FILE__)
#end
class ActiveSupport::TestCase
  fixtures :all
end
  • Second issue: Minitest::UnexpectedError: ActionController::UrlGenerationError: No route matches {:action=>"create", :controller=>"blog/posts", :post=>{:content=>"MyText", :tile=>"MyString"}}
  • Work around:
    • test_helper.rb
module Blog
  class ActionController::TestCase
    setup do
      @routes = Engine.routes
    end
  end
end

So please reopen

@rafaelfranca

This comment has been minimized.

Show comment
Hide comment
@rafaelfranca

rafaelfranca Sep 4, 2014

Member

I can reopen but this will not means it will be fixed. I recommend who care with this issue to give a shot and fix it or in three months the bot will close it again and I'll not reopen anymore.

Member

rafaelfranca commented Sep 4, 2014

I can reopen but this will not means it will be fixed. I recommend who care with this issue to give a shot and fix it or in three months the bot will close it again and I'll not reopen anymore.

@rafaelfranca rafaelfranca reopened this Sep 4, 2014

@rafaelfranca rafaelfranca removed the stale label Sep 4, 2014

@dyson

This comment has been minimized.

Show comment
Hide comment
@dyson

dyson Sep 17, 2014

+1 however nothing above works. I am using Rails 4.1.6 and a non engine plugin and I need to use postgresql as the database because the plugin uses postgresql features.

$ rails plugin new -d postgresql my_plugin
$ cd my_plugin/test/dummy/
$ bundle exec rails g model user name:string
# Set db database config details
$ bundle exec rake db:create db:migrate

I then modified the generated user test to call the database:

require 'test_helper'

class UserTest < ActiveSupport::TestCase
  test "the truth" do
    user = User.first
    assert true
  end
end

Go back to the gems root and run:

$ bundle exec rake test
Run options: --seed 39086

# Running:

.E

Finished in 0.005416s, 369.2572 runs/s, 184.6286 assertions/s.

  1) Error:
UserTest#test_the_truth:
ActiveRecord::StatementInvalid: PG::UndefinedTable: ERROR:  relation "users" does not exist
LINE 5:                WHERE a.attrelid = '"users"'::regclass
                                          ^
:               SELECT a.attname, format_type(a.atttypid, a.atttypmod),
                     pg_get_expr(d.adbin, d.adrelid), a.attnotnull, a.atttypid, a.atttypmod
                FROM pg_attribute a LEFT JOIN pg_attrdef d
                  ON a.attrelid = d.adrelid AND a.attnum = d.adnum
               WHERE a.attrelid = '"users"'::regclass
                 AND a.attnum > 0 AND NOT a.attisdropped
               ORDER BY a.attnum

    activerecord (4.1.6) lib/active_record/connection_adapters/postgresql_adapter.rb:822:in `async_exec'
    activerecord (4.1.6) lib/active_record/connection_adapters/postgresql_adapter.rb:822:in `block in exec_no_cache'
    activerecord (4.1.6) lib/active_record/connection_adapters/abstract_adapter.rb:373:in `block in log'
    activesupport (4.1.6) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
    activerecord (4.1.6) lib/active_record/connection_adapters/abstract_adapter.rb:367:in `log'
    activerecord (4.1.6) lib/active_record/connection_adapters/postgresql_adapter.rb:822:in `exec_no_cache'

...

It seems like the test database isn't even loading the tables, let alone the fixtures.

If I run from within the test/dummy app:

$ bundle exec rails c test

I get an empty table with no user model.

Any ideas?

dyson commented Sep 17, 2014

+1 however nothing above works. I am using Rails 4.1.6 and a non engine plugin and I need to use postgresql as the database because the plugin uses postgresql features.

$ rails plugin new -d postgresql my_plugin
$ cd my_plugin/test/dummy/
$ bundle exec rails g model user name:string
# Set db database config details
$ bundle exec rake db:create db:migrate

I then modified the generated user test to call the database:

require 'test_helper'

class UserTest < ActiveSupport::TestCase
  test "the truth" do
    user = User.first
    assert true
  end
end

Go back to the gems root and run:

$ bundle exec rake test
Run options: --seed 39086

# Running:

.E

Finished in 0.005416s, 369.2572 runs/s, 184.6286 assertions/s.

  1) Error:
UserTest#test_the_truth:
ActiveRecord::StatementInvalid: PG::UndefinedTable: ERROR:  relation "users" does not exist
LINE 5:                WHERE a.attrelid = '"users"'::regclass
                                          ^
:               SELECT a.attname, format_type(a.atttypid, a.atttypmod),
                     pg_get_expr(d.adbin, d.adrelid), a.attnotnull, a.atttypid, a.atttypmod
                FROM pg_attribute a LEFT JOIN pg_attrdef d
                  ON a.attrelid = d.adrelid AND a.attnum = d.adnum
               WHERE a.attrelid = '"users"'::regclass
                 AND a.attnum > 0 AND NOT a.attisdropped
               ORDER BY a.attnum

    activerecord (4.1.6) lib/active_record/connection_adapters/postgresql_adapter.rb:822:in `async_exec'
    activerecord (4.1.6) lib/active_record/connection_adapters/postgresql_adapter.rb:822:in `block in exec_no_cache'
    activerecord (4.1.6) lib/active_record/connection_adapters/abstract_adapter.rb:373:in `block in log'
    activesupport (4.1.6) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
    activerecord (4.1.6) lib/active_record/connection_adapters/abstract_adapter.rb:367:in `log'
    activerecord (4.1.6) lib/active_record/connection_adapters/postgresql_adapter.rb:822:in `exec_no_cache'

...

It seems like the test database isn't even loading the tables, let alone the fixtures.

If I run from within the test/dummy app:

$ bundle exec rails c test

I get an empty table with no user model.

Any ideas?

grk added a commit to grk/rails that referenced this issue Sep 23, 2014

Fix loading fixtures in engine tests
fixture_path is a class attribute of ActiveSupport::TestCase, so
ActiveSupport::TestCase.method_defined?(:fixture_path=) would always
return false.

This should fix #4971

@spastorino spastorino closed this in #17029 Sep 23, 2014

murtuzakz added a commit to murtuzakz/rails that referenced this issue Oct 3, 2014

Fix loading fixtures in engine tests
fixture_path is a class attribute of ActiveSupport::TestCase, so
ActiveSupport::TestCase.method_defined?(:fixture_path=) would always
return false.

This should fix #4971

sachin004 added a commit to sachin004/rails that referenced this issue Dec 13, 2014

Fix loading fixtures in engine tests
fixture_path is a class attribute of ActiveSupport::TestCase, so
ActiveSupport::TestCase.method_defined?(:fixture_path=) would always
return false.

This should fix #4971

senny added a commit that referenced this issue Jan 29, 2015

Merge pull request #18719 from 5t111111/add-fix-for-loading-fixtures-…
…in-engine-tests

Add fix for loading fixtures in engine tests (additional fix for #4971)

Conflicts:
	railties/lib/rails/generators/rails/plugin/templates/test/test_helper.rb

senny added a commit that referenced this issue Jan 29, 2015

Merge pull request #18719 from 5t111111/add-fix-for-loading-fixtures-…
…in-engine-tests

Add fix for loading fixtures in engine tests (additional fix for #4971)

Conflicts:
	railties/lib/rails/generators/rails/plugin/templates/test/test_helper.rb

Conflicts:
	railties/lib/rails/generators/rails/plugin/templates/test/test_helper.rb
@cdcooksey

This comment has been minimized.

Show comment
Hide comment
@cdcooksey

cdcooksey Apr 20, 2016

This issue continues to exist in 4.2.6, even tried downgrading to 4.2.4.

cdcooksey commented Apr 20, 2016

This issue continues to exist in 4.2.6, even tried downgrading to 4.2.4.

@iturgeon

This comment has been minimized.

Show comment
Hide comment
@iturgeon

iturgeon May 4, 2016

Just leaving a note in here for those dumb enough to do what I did. If you wrote your own fixtures, make sure they have a .yml file extension not .yaml

iturgeon commented May 4, 2016

Just leaving a note in here for those dumb enough to do what I did. If you wrote your own fixtures, make sure they have a .yml file extension not .yaml

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment