转换为模块化 sinatra 应用程序会破坏测试

发布于 2024-12-19 03:05:34 字数 2687 浏览 4 评论 0原文

我已经通过经典 sinatra 应用程序进行了模块化,并将 sinatra 应用程序中的路线移动到单独的路线文件中,按照 https://stackoverflow.com/ a/5030173/111884,但是,我似乎无法让我的测试工作。

就是我的文件的样子:

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

Rakefile

# 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将 Cucumber 与模块化 Sinatra 应用程序结合使用,但是他们不工作。

I've modularised by classic sinatra app and moved my routes in my sinatra app into individual routes files as per https://stackoverflow.com/a/5030173/111884, however, I can't seem to get my tests working.

This is what my files look like:

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

Rakefile

# 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

The output of the tests is:

<h1>Not Found</h1>

It doesn't seem to be loading the ./routes/*.rb files.

I'm using Sinatra::Application, instead of Sinatra::Base, but https://stackoverflow.com/a/5030173/111884 uses it. It also references it here http://www.sinatrarb.com/extensions.html. I have tried changing it to use Sinatra::Base, but it didn't fix it.

I've also tried Sinatra tests always 404'ing and Using Cucumber With Modular Sinatra Apps, but they don't work.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

等你爱我 2024-12-26 03:05:34

我认为您只需要更改 app 方法即可返回模块化应用程序类 (MyApp),而不是 Sinatra::Base 类。 : 替换

def app() Sinatra::Base end

因此,将web_spec.rb 中的

def app
  MyApp
end

为: Rack::Test::方法 取决于应用程序方法来告诉它调用哪个类来处理请求。在一个简单的非模块化 Sinatra 应用程序中,该类是 Sinatra::Base 因为这是默认应用路由的类。在模块化应用程序中,这是您定义路由的类(在您的例子中是 MyApp)。

I think you just need to change your app method to return your modularised application class (MyApp) rather than the Sinatra::Base class. So replace:

def app() Sinatra::Base end

in web_spec.rb, with:

def app
  MyApp
end

Rack::Test::Methods depends on the app method to tell it which class to call to process requests. In a simple non-modular Sinatra application that class is Sinatra::Base because that is the class in which the routes are applied by default. In a modular application that is the class in which you define your routes (MyApp in your case).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文