Rails unit testing doesn't load fixtures

后端 未结 6 1085
梦谈多话
梦谈多话 2020-12-16 01:54

rake test:units fails in my current application, because the needed data of the fixtures is missing.

If I\'m loading the fixtures manually via rak

相关标签:
6条回答
  • 2020-12-16 02:19

    My problem is forgot to put "require 'test_helper'" at the head. eg.

    require 'test_helper'
    
    class AdminUserTest < ActiveSupport::TestCase
      # test "the truth" do
      #   assert true
      # end
    end
    
    0 讨论(0)
  • 2020-12-16 02:20

    Not sure if you already did this, but export the test data from the database to the yml files in test/fixtures using a plugin like ar_fixtures

    0 讨论(0)
  • 2020-12-16 02:30

    Put the call to fixtures :all in your test class, not the super class (test_helper). My guess is that initialization of the super class isn't working the way you're expecting and that fixtures :all isn't be called. Perhaps try putting the call in the initialize method of test_helper.

    My test/test_helper.rb looks like this:

    ENV['RAILS_ENV'] ||= 'test'
    require File.expand_path('../../config/environment', __FILE__)
    require 'rails/test_help'
    
    class ActiveSupport::TestCase
      # Setup all fixtures in test/fixtures/*.yml for all tests in alphabetical order.
      fixtures :all
    
      # Add more helper methods to be used by all tests here...
    end
    
    0 讨论(0)
  • 2020-12-16 02:35

    I finally found the problem, although the solutions is kind of hacky.

    One plugin is relying that there is some data in the database, at least one row. So what happened was:

    1. rake loads database schema
    2. rake tries to load environment
    3. environment includes plugin
    4. plugin loading fails because of missing at least one row
    5. no fixtures are loaded

    The hacky solution is: put the needed data directly into schema and not into a fixtures, because it's loaded to late.

    I'll search for a more convenient solution and will update this answer if I found one.

    0 讨论(0)
  • 2020-12-16 02:41

    Another approach is to write your own custom rake task for testing.

    For example:


    task :test_units do
    
      RAILS_ENV = 'test' # Force the environment to test
    
      puts "Recreate the test database"
      Rake::Task['db:test:prepare'].invoke
    
      puts "Seed the database with fixtures"
      Rake::Task['db:fixtures:load'].invoke
    
      puts "Executing Unit Tests"
      Rake::Task['test:units'].prerequisites.clear 
      Rake::Task['test:units'].invoke
    end
    

    0 讨论(0)
  • 2020-12-16 02:42

    I had the same problem. Or rather, the problem was that my fixtures were not current with the database schema. Instead of throwing an exception, rails just used the test database as a fallback solution. Most iffy.

    0 讨论(0)
提交回复
热议问题