使用模块化Sinatra应用程序设置Cucumber,Capybara和RSpec

时间:2011-05-14 11:07:57

标签: rspec cucumber sinatra capybara

这是我的config.ru

$:.unshift File.join(File.expand_path(File.dirname(__FILE__)), 'config')
require 'boot'

Routes = Rack::Mount::RouteSet.new do |set|
  set.add_route App::Users, { :path_info => %r{^/users*} }, {}, :users
  set.add_route App::Dashboard, { :path_info => %r{^/$} }, {}, :dashboard
  set.add_route Rack::File.new(File.dirname(__FILE__) + "/public"), { :path_info => %r{^/*} }, {}, :public
end

run Routes

boot.rb

require 'bundler'
Bundler.setup
Bundler.require(:default)
Mongoid.load!(File.join(File.dirname(__FILE__), '/mongoid.yml'))
%w(sinatra json yaml rack/mount rack/contrib).each {|l| require l }
Dir["./lib/**/*.rb"].each { |f| require f }

我的lib包含应用和模型。

如何让Cucumber看到我的应用程序:

这里是黄瓜env.rb

require 'capybara'
require 'capybara/cucumber'
require 'rspec'

World do
  Capybara.app = App
  include Capybara
  include RSpec::Expectations
  include RSpec::Matchers
end

我想要的是让env.rb读取我的config.ru并准备好我的黄瓜进行测试。

2 个答案:

答案 0 :(得分:12)

在@ amrnt的解决方案中,您可以替换该行

Capybara.app = eval("Rack::Builder.new {( " + File.read(File.dirname(__FILE__) + '/../../config.ru') + "\n )}")

用这个:

Capybara.app, _ = Rack::Builder.parse_file(File.expand_path('../../config.ru', __FILE__))

Capybara.app = Rack::Builder.parse_file(File.expand_path('../../config.ru', __FILE__)).first    

答案 1 :(得分:4)

设置env.rb

ENV['RACK_ENV'] = 'test'
require File.join(File.dirname(__FILE__), '..', '..', 'config/boot.rb')

require 'capybara'
require 'capybara/cucumber'
require 'rspec'

Capybara.app = eval("Rack::Builder.new {( " + File.read(File.dirname(__FILE__) + '/../../config.ru') + "\n )}")

class SomeWorld
  include Capybara
  include RSpec::Expectations
  include RSpec::Matchers
end

World do
  SomeWorld.new
end

它会起作用!