尝试设置capybara / rspec来测试一个简单的sinatra应用程序,但不断出现机架错误。
hello.rb的
require 'sinatra'
class App < Sinatra::Base
get "/" do
"hello hello!"
end
run! if app_file == $0
end
规格/ hello_spec.rb
require File.join(File.dirname(__FILE__), '..', 'hello.rb')
require 'rspec'
require 'capybara'
require 'capybara/dsl'
require 'capybara/rspec'
set :environment, :test
describe 'The Hello App' do
include Capybara::DSL
def setup
Capybara.app = App
end
it "says hello when browsing /" do
visit '/'
page.should have_content('hello')
end
end
注意,我也试过
def setup
Capybara.app = App.new
end
产生了同样的错误。
错误:
C:\Sites\misc\qrgen>bundle exec rspec
F
Failures:
1) The Hello App says hello when browsing /
Failure/Error: visit '/'
ArgumentError:
rack-test requires a rack application, but none was given
# ./spec/hello_spec.rb:18:in `block (2 levels) in <top (required)>'
Finished in 0.374 seconds
1 example, 1 failure
Failed examples:
rspec ./spec/hello_spec.rb:17 # The Hello App says hello when browsing /
使用和不使用bundle exec运行相同的错误。有什么想法吗?
Gemfile:
gem "sinatra"
gem "sinatra-contrib"
gem "sinatra-flash"
gem "slim"
gem "rspec"
gem "capybara"
gem "rack-test"
答案 0 :(得分:6)
希望这对尝试从Sinatra文档设置测试的其他人有用。
更改
def setup
Capybara.app = App
end
到
Capybara.app = App
解决了这个问题。