Brian DiCasa
Brian DiCasa

Reputation: 9487

Running RSpec Files From ruby code

I'm trying to run RSpec tests straight from ruby code. More specifically, I'm running some mysql scripts, loading the rails test environment and then I want to run my rspec tests (which is what I'm having trouble with)... I'm trying to do this with a rake task. Here is my code so far:

require "spec/autorun"
require"spec"
require "spec/rake/spectask"
RAILS_ENV = 'test'

namespace :run_all_tests do 
  desc "Run all of your tests" 

  puts "Reseting test database..." 
  system "mysql --user=root --password=dev < C:\\Brian\\Work\\Personal\\BrianSite\\database\\BrianSite_test_CreateScript.sql" 
  puts "Filling database tables with test data..." 
  system "mysql --user=root --password=dev < C:\\Brian\\Work\\Personal\\BrianSite\\database\\Fill_Test_Tables.sql" 

  puts "Starting rails test environment..." 
  task :run => :environment do 
    puts "RAILS_ENV is #{RAILS_ENV}"
    # Run rspec test files here...
    require "spec/models/blog_spec.rb" 
  end 
end

I thought the require "spec/models/blog_spec.rb" would do it, but the tests aren't running. Anyone know where I'm going wrong?

UPDATE: I've added the require "spec/autorun" command at the top of the file and now I am running into this error when I do a rake run_all_tests:run :

C:/Ruby/lib/ruby/gems/1.8/gems/rspec-1.3.0/lib/spec/runner/options.rb:283:in fi les_to_load': File or directory not found: run_all_tests:run (RuntimeError) from C:/Ruby/lib/ruby/gems/1.8/gems/rspec-1.3.0/lib/spec/runner/options. rb:275:ineach' from C:/Ruby/lib/ruby/gems/1.8/gems/rspec-1.3.0/lib/spec/runner/options. rb:275:in files_to_load' from C:/Ruby/lib/ruby/gems/1.8/gems/rspec-1.3.0/lib/spec/runner/options. rb:133:inrun_examples' from C:/Ruby/lib/ruby/gems/1.8/gems/rspec-1.3.0/lib/spec/runner.rb:61:in run' from C:/Ruby/lib/ruby/gems/1.8/gems/rspec-1.3.0/lib/spec/runner.rb:45:in autorun' from C:/Ruby/bin/rake:19

It's hitting this error when it gets to the require "spec/models/blog_spec.rb" line. This file does exist because when I try and change the require statement, I just get a file not found error. It seems like rspec is trying to now run the tests, but is running into problems... any thoughts?

Thanks for any help.

Upvotes: 2

Views: 1850

Answers (1)

Dan Fitch
Dan Fitch

Reputation: 2529

Try adding require "spec/autorun to the top of your file.

You don't need to do it that way though, because there are built in Rake tasks (that's what the spec/rake/spectask is including) to do what you're doing: http://rspec.info/documentation/tools/rake.html

Upvotes: 1

Related Questions