1

https://stackoverflow.com/a/5030173/111884に従って、古典的なsinatraアプリでモジュール化し、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'

./routes/init.rb

require_relative 'main'

./routes/main.rb

# The main routes for the core of the app
class MyApp < Sinatra::Application
  get '/' do
    erb :main
  end
end

./spec/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

./spec/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

レーキファイル

# 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 テストを常に 404'ingおよびUsing Cucumber With Modular Sinatra Appsも試しましたが、機能しません。

4

1 に答える 1

6

appSinatra::Base クラスではなく、モジュール化されたアプリケーション クラス (MyApp) を返すようにメソッドを変更するだけでよいと思います。したがって、次のように置き換えます。

def app() Sinatra::Base end

web_spec.rb で、次のように指定します。

def app
  MyApp
end

Rack::Test::Methodsは、アプリケーション メソッドに依存して、要求を処理するためにどのクラスを呼び出すかを伝えます。単純な非モジュラー Sinatra アプリケーションでは、そのクラスは Sinatra::Base です。これは、ルートがデフォルトで適用されるクラスであるためです。ルートを定義するクラスであるモジュラー アプリケーション (この場合は MyApp)。

于 2011-12-09T12:38:38.763 に答える