Given a typical Rails 3 environment, why can't I run any tests?

I am working on writing simple unit tests for a Rails 3 project, but I cannot run any tests.

The fact is that an attempt to automatically run the Rails test with an error fails:

require 'test_helper' class UserTest < ActiveSupport::TestCase # Replace this with your real tests. test "the truth" do assert true end end 

Results with the following error:

 <internal:lib/rubygems/custom_require>:29:in `require': no such file to load -- test_helper (LoadError) from <internal:lib/rubygems/custom_require>:29:in `require' from user_test.rb:1:in `<main>' 

Commenting out the required line test_helper and trying to run the test results with this error:

 user_test.rb:3:in `<main>': uninitialized constant Object::ActiveSupport (NameError) 

The gems from the action pack must be correctly installed and updated:

 actionmailer (3.0.3, 2.3.5) actionpack (3.0.3, 2.3.5) activemodel (3.0.3) activerecord (3.0.3, 2.3.5) activeresource (3.0.3, 2.3.5) activesupport (3.0.3, 2.3.5) 

Ruby is at 1.9.2p0 and Rails is at 3.0.3.

An example dump of my test directory is as follows:

 /fixtures /functional /integration /performance /unit -- /helpers -- user_helper_test.rb -- user_test.rb test_helper.rb 

I have never seen this problem before - I run typical rake tasks to prepare a test environment. I have nothing unusual in my application or environment configuration files, and I have not installed any unusual stones that would interfere with the test environment.

Change March 9

Xavier Holt clause explicitly indicating the path to test_helper; however, this revealed a problem with ActiveSupport.

Now, when I try to run the test, I get the following error message (also the above):

 user_test.rb:3:in `<main>': uninitialized constant Object::ActiveSupport (NameError) 

But, as you can see above, the Action Pack is installed and updated to date.

Edit March 13th

When trying to run tests using rake test:units , the following stack trace is reset to the console:

 test/unit/bookmark_test.rb:3:in `<top (required)>': uninitialized constant Objec t::ActiveSupport (NameError) from C:/Ruby192/lib/ruby/gems/1.9.1/gems/rake-0.8.7/lib/rake/rake_test_loader.rb:5:in `load' from C:/Ruby192/lib/ruby/gems/1.9.1/gems/rake-0.8.7/lib/rake/rake_test_loader.rb:5:in `block in <main>' from C:/Ruby192/lib/ruby/gems/1.9.1/gems/rake-0.8.7/lib/rake/rake_test_loader.rb:5:in `each' from C:/Ruby192/lib/ruby/gems/1.9.1/gems/rake-0.8.7/lib/rake/rake_test_loader.rb:5:in `<main>' rake aborted! 

So, looking at the file above, I see the following:

 #!/usr/bin/env ruby # Load the test files from the command line. ARGV.each { |f| load f unless f =~ /^-/ } 

As far as I know, everything looks as expected.

+7
source share
3 answers

Your test/test_helper must be created when creating the application. It contains this valuable content:

 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|csv) for all tests in alphabetical order. # # Note: You'll currently still have to declare fixtures explicitly in integration tests # -- they do not yet inherit this setting fixtures :all # Add more helper methods to be used by all tests here... end 

The second line is the most important here: it requires the config/environment.rb file in the root of your application, which, in turn, requires a lot of other things, including valuable (do I like this word today, is it normal?) ActiveSupport constant.

When you create a controller, model or scaffold, it will also generate tests for them. I just ran rails g scaffold ticket in my application and generated test/unit/ticket_test.rb , which contains the following:

 require 'test_helper' class TicketTest < ActiveSupport::TestCase # Replace this with your real tests. test "the truth" do assert true end end 

The first line of this file will require the file test/test_helper.rb , which we saw. This will load the ActiveSupport class and TestCase inside it, thereby making this test executable. Everything else just follows from there.

With all these explanations (even if this is what you already know), I place a large bet on it that moves your LOAD_PATH , causing the test directory to be deleted from it.

What is really unusual is that when you specify the full path to test/test_helper.rb , you say that it loads it, but ActiveSupport is still undefined. Well, this should be loaded as described above. Is this really loading config/environment.rb ? Can you add something like:

 puts "LOADING CONFIG/ENVIRONMENT.RB" 

At the top of your config/environment.rb file, and then run the tests again? It must be bred. Very unusual.

Continuing the theme of LOAD_PATH ... Get a dirty little secret that you are not telling us about?

In fact, Dan Cheil is great. You can run the tests with ruby test/unit/ticket_test.rb , in which case test_helper will not be available, but it still doesn’t explain why, when you specify the full path, you still get the undefined ActiveSupport constant.

If you want to run one test, you must do this:

 ruby -Itest test/unit/ticket_test.rb 

This -I option adds the test directory to the boot path, that is, the test_helper file will be available through the direct require 'test_helper' . If errors still occur after this, I think your test/test_helper.rb either empty or broken.

+10
source

Sorry for posting here, but I still can not comment on the questions.

what environment do you use, Win (has a problem with the .gemspec file) Linux, Mac?

Do you use RVM?

Test-Unit is installed by default with Rails, if you installed the gem test block, you will encounter a conflict between attempts to remove gem and your tests should start working.

If you are working on Windows, I would remove gem "autotest" then go to the following directory

  drive:\Ruby192\lib\ruby\gems\1.9.1\specifications 

Here you will find .gemspec files. Make sure you do not have two autotest.gemspec files, or for that matter. if so delete (delete) them, then download and gem set the self-test again. Take the latest version using the version switcher.

You should be able to run autotests. I had come across this before, so to fix it, I simply deleted the view and auxiliary test files and wrote everything in the standard test file. In addition to the fact that I know that autotest windows works, there were problems due to the ruby ​​installer and accountant who did not clean everything correctly and forgot files.

I will find a link for you to better explain.

0
source

The problem you are facing is how you run the tests. Just calling ruby test/unit/user_test.rb does not set the boot path, which explains the problems you had.

rake test:units is what you want and should work right away.

0
source

All Articles