nickcoxdotme
nickcoxdotme

Reputation: 6697

Running rspec in Sinatra app just starts the server

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?

Upvotes: 1

Views: 769

Answers (3)

nickcoxdotme
nickcoxdotme

Reputation: 6697

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.

Upvotes: 1

Sir l33tname
Sir l33tname

Reputation: 4330

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.

Upvotes: 1

Uri Agassi
Uri Agassi

Reputation: 37409

Try removing the new() from your def app:

module RSpecMixin
  def app
    App
  end
end

Upvotes: 1

Related Questions