代码之家  ›  专栏  ›  技术社区  ›  craig

在Rakefile中包含spec\u helper

  •  0
  • craig  · 技术社区  · 6 年前

    spec/spec_helper.rb 文件是向 .rpsec 文件:

    --require spec_helper
    

    这样就不需要在每个 _spec 文件夹:

    # include 'spec/spec_heler'  <-- removed
    
    describe FooController do
    end
    

    $ rake test
    Traceback (most recent call last):
    /Users/<user>/Projects/sinatra/mod/spec/app/controllers/foo_controller_spec.rb:1:in `<top (required)>': uninitialized constant FooController (NameError)
    rake aborted!
    Command failed with status (1)
    
    Tasks: TOP => test
    (See full trace by running task with --trace)
    

    这表明 .rpsec秒 spec_helper

    文件的内容 Rakefile

    require 'rake/testtask'
    
    Rake::TestTask.new do |t|
      t.pattern = "spec/**/*_spec.rb"
    end
    

    文件的内容 spec/spec\u助手.rb

    require 'rack/test'
    require 'rspec'
    
    require File.expand_path '../../app.rb', __FILE__
    
    RSpec.configure do |config|
    
      # rack
      config.include Rack::Test::Methods
    
      ...
    end
    

    如何更正此问题?

    1 回复  |  直到 6 年前
        1
  •  0
  •   craig    6 年前

    我需要换个房间 Rakefile :

    require 'rspec/core/rake_task'
    
    task :default => :spec
    
    RSpec::Core::RakeTask.new(:spec) do |t|
      t.pattern = "spec/**/*_spec.rb"
    end
    

    我还需要改变 spec/spec_helper.rb

    # added
    require 'rubygems'
    require 'bundler/setup'
    
    # existing
    require 'rack/test'
    require 'rspec'
    
    require File.expand_path '../../app.rb', __FILE__
    
    RSpec.configure do |config|
    
      # rack
      config.include Rack::Test::Methods
    
      ...
    
    end
    

    从中运行测试时 rspec :

    $ rspec
    ...
    Finished in 0.07952 seconds (files took 0.63445 seconds to load)
    15 examples, 0 failures, 1 pending
    

    从中运行测试时 rake :

    $ rake
    /Users/<user>/.rbenv/versions/2.3.3/bin/ruby -I/Users/<user>/.rbenv/versions/2.3.3/lib/ruby/gems/2.3.0/gems/rspec-support-3.8.0/lib:/Users/<user>/.rbenv/versions/2.3.3/lib/ruby/gems/2.3.0/gems/rspec-core-3.8.0/lib /Users/<user>/.rbenv/versions/2.3.3/lib/ruby/gems/2.3.0/gems/rspec-core-3.8.0/exe/rspec --pattern spec/\*\*/\*_spec.rb
    ...
    Finished in 0.08885 seconds (files took 0.70133 seconds to load)
    15 examples, 0 failures, 1 pending
    

    $LOAD_PATH 然而。

    推荐文章