Running RSpec files from ruby ​​code

I am trying to run RSpec tests directly from ruby ​​code. More specifically, I am running some mysql scripts loading the rails test environment and then I want to run my rspec tests (which I am having trouble with) ... I am trying to do it with the rake command, Here is my code:

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 it would require "spec / models / blog_spec.rb", but the tests fail. Does anyone know where I am going wrong?

UPDATE: I added the "spec / autorun" command at the top of the file and now I run this error when I do 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:in

each '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:in

run_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 hits this error when it hits the require / spec / models / blog_spec.rb line. This file exists because when I try to change the require statement, I just get a file that was not found. It seems like rspec is trying to run tests but is running into problems ... any thoughts?

Thanks for any help.

+2


a source to share


3 answers


Try adding require "spec/autorun

to the beginning of the file.



You don't have to do this because there are built-in Rake tasks (which includes spec/rake/spectask

) to do what you do: http://rspec.info/documentation/tools/rake.html

+1


a source


Whenever I execute an rb script using the rspec library, I add "rspec" before the filename



rspec mycode.rb

0


a source


it is possible to change namespace subject to string ...

-1


a source







All Articles