Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails unit testing doesn't load fixtures

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 rake db:fixtures:load RAILS_ENV=test the unit tests are working, but rake purges the test database.

My test_helper includes fixtures :all and my tests are inheriting from it - but the fixtures are simply not loading.

I'm kind of clueless at the moment and could really need some help!

I've tried a lot and I think it has to do with some environment settings or plugins used in this project. Does anyone know where to read about which files are loaded for the testing environment?

like image 847
xijo Avatar asked Oct 10 '09 10:10

xijo


People also ask

What is test fixture in unit test framework?

In generic xUnit, a test fixture is all the things that must be in place in order to run a test and expect a particular outcome. Frequently fixtures are created by handling setUp() and tearDown() events of the unit testing framework.

How do I run unit tests in Rails?

We can run all of our tests at once by using the bin/rails test command. Or we can run a single test file by passing the bin/rails test command the filename containing the test cases. This will run all test methods from the test case.

What are fixtures in Rails?

Fixtures are data that you can feed into your unit testing. They are automatically created whenever rails generates the corresponding tests for your controllers and models. They are only used for your tests and cannot actually be accessed when running the application.

What is unit test Rails?

The Tests − They are test applications that produce consistent result and prove that a Rails application does what it is expected to do. Tests are developed concurrently with the actual application. The Assertion − This is a one line of code that evaluates an object (or expression) for expected results.


2 Answers

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
like image 94
lingceng Avatar answered Sep 23 '22 19:09

lingceng


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
like image 42
Chris Avatar answered Sep 21 '22 19:09

Chris