All was well with my app until I refactored it into multiple controllers/server.
I believe I am using the modular set up with Sinatra-Base. I believe I have defined my Rack ENVs right.
I have a problem. I cannot run rspec without starting up Sinatra in my command line. When I fix this problem by commenting out the 'run!' command at the bottom of my server file, obviously Sinatra will not start up when I try and run my app from the command line.
Here is my file structure:
app
app.rb
server.rb
datamapper_setup.rb
controllers
new_users.rb
sessions.rb
views
various.erb
files.erb
models
user.rb
spec
features
feature_test_spec.rb
other_feature_test_spec.rb
units
unit_test_spec.rb
spec_helper.rb
config.ru
.rspec
Gemfile
Rakefile
Here is my config.ru file
require './app/app.rb'
run MyApp
Here is my app.rb file
ENV['RACK_ENV'] ||= 'development'
require 'rack'
require 'sinatra/base'
require 'sinatra/flash'
require 'sinatra/partial'
require_relative 'data_mapper_setup'
require_relative 'server'
require_relative 'controllers/new_users'
require_relative 'controllers/sessions'
Here is my server file
class App < Sinatra::Base
register Sinatra::Flash
register Sinatra::Partial
enable :sessions
set :session_secret, 'super secret'
set :partial_template_engine, :erb
helpers do
def session_user
@session_user ||= User.get(session[:user_id])
end
end
get '/' do
@new_user = User.new
erb :'user/index'
end
#Here is where I am commenting out run! in order to be able to run rspec without starting Sinatra.
# It doesn't work at all if I have => run! if app_file == $0 now, but used to work before the refactor when this line was in my app file.
run!
end
In my spec helper:
ENV['RACK_ENV'] = 'test'
require File.join(File.dirname(__FILE__), '..', 'app/app.rb')
require './app/app.rb'
require 'capybara'
require 'capybara/rspec'
require 'rspec'
require 'database_cleaner'
require 'coveralls'
require 'simplecov'
require './app/data_mapper_setup.rb'
require './spec/web_helpers.rb'
SimpleCov.formatters = [
SimpleCov::Formatter::HTMLFormatter,
Coveralls::SimpleCov::Formatter
]
Coveralls.wear!
Capybara.app = App
RSpec.configure do |config|
config.include SessionHelpers
config.before(:suite) do
DatabaseCleaner.strategy = :transaction
DatabaseCleaner.clean_with(:truncation)
end
config.before(:each) do
DatabaseCleaner.start
end
config.after(:each) do
DatabaseCleaner.clean
end
config.include Capybara::DSL
config.expect_with :rspec do |expectations|
expectations.include_chain_clauses_in_custom_matcher_descriptions = true
end
config.mock_with :rspec do |mocks|
mocks.verify_partial_doubles = true
end
end
All my rspec tests are passing.
What I have tried...
- Various different config.ru set ups
- Spec_helper with require 'rack' differently
- Checking my rack environments are isolated in my database
- Trying to understand the difference between Sinatra Modular and Sinatra Classical styles (with limited success)