我以前从未使用过Sinatra,也从未手动配置过Rspec(总是为rails使用预先编写的脚本),但我想尝试一下。
但我遇到了麻烦,我设法让RSpec工作,但我遇到了错误,只是让它识别来自Sinatra的方法。
我想知道换成Rack::Test是否更好。
我现在的问题是atm:
1)rake失败
2)当我使用Don't know how to build task 'default'时,它会因rspec而失败
很明显我做错了什么,但我不知道我在跟踪一些我发现的图特,但是,我只是不太顺利。
耙子:

require 'rspec/core/rake_task'


RSpec::Core::RakeTask.new do |task|
  task.rspec_opts = ['-c', '-f progress', '-r ./spec/spec_helper.rb']
  task.pattern = './spec/**/*_spec.rb'
end

规范助手.rb
require 'rspec'
require 'rack/test'

RSpec.configure do |conf|
  conf.include Rack::Test::Methods
end

应用程序规范rb
ENV['RACK_ENV'] = 'test'

require '../../myapp'
require 'rspec'
require 'rack/test'

describe 'My Sinatra Application' do

  include Rack::Test::Methods

  def app
    Sinatra::Application
  end

  it "says hello" do
    get '/' do
      expect(last_response).to be_ok
      expect(last_response.body).to eq('Hello World')
    end
  end


  it 'should allow access to main page' do

  end

  it 'should list every site from the links file' do
    # get '/' do
    #   Links.each do |link|
    #
    #   end
    # end
  end

end

第一次编辑:
我的app.rb
require 'dotenv'
Dotenv.load
require 'yaml'
require 'sinatra'
require 'helpers'
require 'actions'
require 'main'

主RB
class Ops_JustGiving < Sinatra::Base
  Links = YAML::Load(File.open('..\\links.yml'))['sites']
  set :root, File.dirname __FILE__

  helpers Sinatra::Ops_JustGiving::Helpers

  register Sinatra::Ops_JustGiving::Actions

end

最佳答案

你的规范助手应该是:

require 'rspec'
require 'rack/test'

RSpec.configure do |conf|
  conf.include Rack::Test::Methods
end

那么在你的个别测试中:
ENV['RACK_ENV'] = 'test'

require 'hello_world'  # <-- your sinatra app name
require 'rspec'
require 'rack/test'

describe 'My Sinatra Application' do
  include Rack::Test::Methods  #<---- you really need this mixin

  def app
    Sinatra::Application
  end

  it "says hello" do
    get '/'
    expect(last_response).to be_ok
    expect(last_response.body).to eq('Hello World')
  end
end

让它工作,然后你可以重构作为你添加更多的测试。

09-12 03:23