12

Rails appears to not be loading any fixtures for unit or functional tests. I have a simple 'products.yml' that parses and appears correct:

ruby:
  title: Programming Ruby 1.9
  description:
    Ruby is the fastest growing and most exciting dynamic
    language out there. If you need to get working programs
    delivered fast, you should add Ruby to your toolbox.
  price: 49.50
  image_url: ruby.png

My controller functional test begins with:

require 'test_helper'

class ProductsControllerTest < ActionController::TestCase
  fixtures :products

  setup do
    @product = products(:one)    
    @update = {
      :title => 'Lorem Ipsum' ,
      :description => 'Wibbles are fun!' ,
      :image_url => 'lorem.jpg' ,
      :price => 19.95
    }
  end

According to the book, Rails should "magically" load the fixtures (as my test_helper.rb has fixtures :all in it. I also added the explicit fixtures load (seen above). Yes Rails complains:

user @ host ~/Dropbox/Rails/depot > rake test:functionals
(in /Somewhere/Users/user/Dropbox/Rails/depot)
/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/bin/ruby -Ilib:test "/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/gems/1.8/gems/rake-0.8.3/lib/rake/rake_test_loader.rb" "test/functional/products_controller_test.rb" 
Loaded suite /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/gems/1.8/gems/rake-0.8.3/lib/rake/rake_test_loader
Started
EEEEEEE
Finished in 0.062506 seconds.

  1) Error:
test_should_create_product(ProductsControllerTest):
NoMethodError: undefined method `products' for ProductsControllerTest:Class
    /test/functional/products_controller_test.rb:7

  2) Error:
test_should_destroy_product(ProductsControllerTest):
NoMethodError: undefined method `products' for ProductsControllerTest:Class
    /test/functional/products_controller_test.rb:7
...

I did come across the other Rails test fixture question Rails unit testing doesn't load fixtures, but that leads to a plugin issue (something to do with the order of loading fixtures).

BTW, I am developing on Mac OS X 10.6 with Rail 2.3.5 and Ruby 1.8.7, no additional plugins (beyond the base install).

Any pointers on how to debug, why the magic of Rails appears to be failing here? Is it a version problem? Can I trace code into the libraries and find the answer? There are so many "mixin" modules I can't find where the fixtures method really lives.

2
  • APIdock is useful for times like this when you need to find method docs. apidock.com/rails/ActiveRecord/TestFixtures/ClassMethods/…
    – x1a4
    Commented Jun 6, 2010 at 17:44
  • thanks x1a4! apidocks IS a very handy tool that I'll be adding to my toolbox. still not sure what the root cause is though ...
    – Deano
    Commented Jun 6, 2010 at 19:32

3 Answers 3

2

With Rails 3.2, I find that when running a single test file (specifying TEST=/test/unit/foo.rb), the fixtures are not loaded. However when running the tests with

ruby -Itest /test/unit/foo.rb

the fixtures are loaded as expected.

1

Using ruby 1.8.7 and rails 2.1.1, I resolved the issue by setting self.use_instantiated_fixtures = true in test_helper.rb.

0

Having given up, I tried a reinstall/rebuild onto rail 3.0.0beta3. this resolved the issue. I can only assume this was some kind of version mismatch.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.