Rspec和sinatra只是不适合我,但我希望他们

时间:2014-10-03 10:25:43

标签: ruby rspec sinatra

我之前从未使用过Sinatra,而且我之前从未手动配置过Rspec(总是使用预先编写的rails脚本),但我想试一试。

但是我遇到了麻烦,我设法让RSpec做了一些工作但是我遇到了错误,只是让它识别来自Sinatra的方法。

我想知道切换到Rack::Test会不会更好。

我目前的问题是atm:

1)rakeDon't know how to build task 'default'

失败

2)当我使用rspec时,undefined method get for #<RSpec::ExampleGroups::MySinatraApplication:0

失败

现在很明显我做错了什么,但我不知道是什么。我发现了一些我发现的但是很好,但是我的进展并不顺利。

Rake文件:

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

spec_helper.rb

require 'rspec'
require 'rack/test'

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

app_spec.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

首先编辑:

myapp.rb

require 'dotenv'
Dotenv.load
require 'yaml'
require 'sinatra'
require 'helpers'
require 'actions'
require 'main'

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

1 个答案:

答案 0 :(得分:4)

您的规范助手应该只是:

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

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