2014-10-03 55 views
1

我以前從未使用過Sinatra,之前我從來沒有手動配置過Rspec(總是使用預先寫好的rails腳本),但是我想讓它一試身手。Rspec和sinatra只是不適合我,但我希望他們

但是我遇到了麻煩,我設法讓RSpec進行某種工作,但我遇到了錯誤,只是讓它識別來自Sinatra的方法。

我想知道是否應該改用Rack::Test代替。

我現在的問題是大氣壓:

1)rake失敗Don'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 

回答

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 

把那再工作,就可以重構爲您的添加更多測試。

+0

我不明白它,但它在瀏覽器中正常工作,但測試失敗並顯示'未找到'響應。 – Thermatix 2014-10-03 13:59:01

+0

歡迎來到測試,有時間弄清楚它在做什麼。我回答你的問題,所以請接受我的回答。 – Anthony 2014-10-03 14:03:36

+0

部分答案會打勾嗎? – Thermatix 2014-10-03 14:07:57

相關問題