我已经通过经典的sinatra app进行模块化,并按照https://stackoverflow.com/a/5030173/111884将我的sinatra应用程序中的路线移动到单独的路径文件中,但是,我似乎无法让我的测试工作。
这就是我的文件:
./ web.rb
require 'sinatra'
require 'sinatra/flash'
class MyApp < Sinatra::Application
# ...
end
require_relative 'models/init'
require_relative 'helpers/init'
require_relative 'routes/init'
./路由/ init.rb
require_relative 'main'
./路由/ main.rb的
# The main routes for the core of the app
class MyApp < Sinatra::Application
get '/' do
erb :main
end
end
./规格/ spec_helper.rb
ENV['RACK_ENV'] = 'test'
require 'minitest/autorun'
require 'rack/test'
require 'factory_girl'
# Include factories.rb file
begin
require_relative '../test/factories.rb'
rescue NameError
require File.expand_path('../test/factories.rb', __FILE__)
end
# Include web.rb file
begin
require_relative '../web'
rescue NameError
require File.expand_path('../web', __FILE__)
end
./规格/ web_spec.rb
begin
require_relative 'spec_helper'
rescue NameError
require File.expand_path('spec_helper', __FILE__)
end
include Rack::Test::Methods
def app() Sinatra::Base end
describe "Some test" do
# ...
end
Rake文件
# Test rake tasks
require 'rake/testtask'
Rake::TestTask.new do |t|
t.libs << "test"
t.libs << "spec"
t.test_files = FileList['test/factories.rb', 'test/test_*.rb', 'spec/spec_helper.rb', 'spec/**/*_spec.rb']
t.verbose = true
end
测试的输出是:
<h1>Not Found</h1>
似乎没有加载./routes/*.rb
个文件。
我正在使用Sinatra::Application
而非Sinatra::Base
,但https://stackoverflow.com/a/5030173/111884使用它。它也在这里引用http://www.sinatrarb.com/extensions.html。我尝试将其更改为使用Sinatra::Base
,但它没有修复它。
我还尝试了Sinatra tests always 404'ing和Using Cucumber With Modular Sinatra Apps,但它们无效。
答案 0 :(得分:6)
我认为您只需要更改app
方法以返回模块化应用程序类(MyApp)而不是Sinatra :: Base类。所以替换:
def app() Sinatra::Base end
web_spec.rb中的,带:
def app
MyApp
end
Rack::Test::Methods依赖于app方法来告诉它调用哪个类来处理请求。在一个简单的非模块化Sinatra应用程序中,该类是Sinatra :: Base,因为这是默认应用路由的类。在模块化应用程序中,您可以在其中定义路径(在您的情况下为MyApp)。