I'm having trouble running some rspec specs on a Sinatra app. I'm just learning Ruby, so unfortunately I don't know enough to fix the problem I'm having with code from a book (Service Oriented Design in Ruby on Rails, by Paul Dix).
The problem is these tests aren't really running (I think). From the app directory, I run $ ruby spec/service_spec.rb, and I get no indication of the tests passing or failing. I get a couple of log messages from the file /service.rb, but that's all the output shown. I put in some puts calls to show that the before blocks and it blocks aren't getting executed (even though the describe blocks are).
If you want to see more about the code, check out the repository on Github.
/spec/service_spec.rb:
ENV['SINATRA_ENV'] = 'test'
require_relative '../service'
require 'rspec'
require 'rack/test'
RSpec.configure do |conf|
conf.include Rack::Test::Methods
end
def app
Sinatra::Application
end
describe "service" do
puts 'describe 1'
before(:each) do
puts 'before 1'
User.delete_all
end
describe "GET on /api/v1/users/:id" do
puts 'describe 2'
before(:each) do
puts 'before 2'
User.create(
:name => "paul",
:email => "[email protected]",
:password => "strongpass",
:bio => "rubyist")
end
it "should return a user by name" do
puts 'it 1'
get '/api/v1/users/paul'
last_response.should be_ok
attributes = JSON.parse(last_response.body)["user"]
attributes["name"].should == "paul"
end
it "should return a 404 for a user that doesn't exist" do
puts 'it 2'
get '/api/v1/users/foo'
last_response.status.should == 404
end
end
end
/service.rb:
require 'active_record'
require 'sinatra'
require_relative 'models/user'
require 'logger'
# log levels are DEBUG < INFO < WARN < ERROR < FATAL < UNKNOWN
log = Logger.new(STDOUT)
log.level = Logger::DEBUG
# set up the environment
env_index = ARGV.index("-e")
env_arg = ARGV[env_index + 1] if env_index
env = env_arg || ENV["SINATRA_ENV"] || "development"
log.debug "env: #{env}"
# set up database connection
use ActiveRecord::ConnectionAdapters::ConnectionManagement
databases = YAML.load_file("config/database.yml")
ActiveRecord::Base.establish_connection(databases[env])
log.debug "#{databases[env]['database']} database connection established"
# HTTP entry points
# get a user by id
get '/api/v1/users/:name' do
user = User.find_by_name(params[:name])
if user
user.to_json
else
error 404, { error: "user not found" }.to_json
end
end
$ ruby spec/service_spec.rb output:
D, [2013-03-16T17:05:58.275276 #17685] DEBUG -- : env: test
D, [2013-03-16T17:05:58.290885 #17685] DEBUG -- : db/test.sqlite3 database connection established
describe 1
describe 2
Any ideas? Thanks!
Per comments on question, this was fixed by running rspec --init to bootstrap RSpec with a spec/spec_helper.rb file.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With