简体   繁体   中英

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: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 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'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.

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

每当我使用rspec库执行rb脚本时,我都会在文件名之前添加“ rspec”

rspec mycode.rb

也许将名称空间的内容更改为字符串...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM