1

I'm using rspec to test my Sinatra app. The app is super simple, and so are the tests, but when I run rspec from the CLI, it just starts the app server. I've never had this happen.

Here's what my spec_helper.rb looks like:

#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

And my spec is just

require 'spec_helper'

describe "My Sinatra Application" do
  it "should allow accessing the home page" do
    expect(1).to eq(1)
  end
end

I can't get rspec to run.

What I've tried:

I've tried the recommendations in this Sinatra testing guide. Also the rspec Sinatra test recipe. In this blog post, the set :run, false looked promising, but no dice. Then I thought, I'll just define a rake task. Putting the rspec gem in the test group and setting the RACK_ENV to test. All these things just start the app server:

$ 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

Halp?

1
  • What does your app.rb look like?
    – matt
    Mar 9, 2014 at 16:17

3 Answers 3

1

Try removing the new() from your def app:

module RSpecMixin
  def app
    App
  end
end
1
  • This still starts the server. What other information can I provide to help diagnose? Mar 9, 2014 at 15:01
1

An other way to create your app for testing is something like this:

APP = Rack::Builder.parse_file('config.ru').first

module RSpecMixin
  def app
    APP
  end
end

Obvious this works only if you use config.ru.

1

I found the answer. It's embarrassing, but I was switching back and forth between the modular and classic style deciding which I liked best, and I had left an errant App.run! call at the bottom of my app.rb file.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.