我正在使用 rspec 来测试我的 Sinatra 应用程序。该应用程序非常简单,测试也是如此,但是当我从 CLI 运行 rspec 时,它只是启动应用程序服务器。我从来没有发生过这种情况。
这是我的 spec_helper.rb
的样子:
#spec/spec_helper.rb
require File.expand_path '../../app.rb', __FILE__
ENV['RACK_ENV'] = "test"
require 'rspec'
require 'rack/test'
set :environment, :test
set :run, false
set :raise_errors, true
set :logging, false
module RSpecMixin
def app
App.new
end
end
RSpec.configure do |config|
config.color_enabled = true
config.tty = true
config.formatter = :documentation
config.include Rack::Test::Methods
config.include RSpecMixin
end
我的规范是
require 'spec_helper'
describe "My Sinatra Application" do
it "should allow accessing the home page" do
expect(1).to eq(1)
end
end
我无法让 rspec 运行。
我尝试过的:
我已尝试过此 Sinatra testing guide 中的建议。还有 rspec Sinatra test recipe 。在 this blog post 中,set :run, false
看起来很有希望,但没有成功。然后我想,我就 define a rake task 吧。将 rspec
gem 放入 test
组并设置 RACK_ENV
进行测试。所有这些都只是启动应用服务器:
$ rake spec
[2014-03-08 22:06:38] INFO WEBrick 1.3.1
[2014-03-08 22:06:38] INFO ruby 2.0.0 (2013-02-24) [x86_64-darwin11.4.2]
== Sinatra/1.4.4 has taken the stage on 4567 for development with backup from WEBrick
[2014-03-08 22:06:38] INFO WEBrick::HTTPServer#start: pid=21538 port=4567
暂停?
最佳答案
尝试从您的 def 应用程序
中删除 new()
:
module RSpecMixin
def app
App
end
end
关于ruby - 在 Sinatra 应用程序中运行 rspec 只会启动服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22279084/