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?
Try removing the new() from your def app:
module RSpecMixin
def app
App
end
end
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.
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.
Related
This is my web app:
class Front < Sinatra::Base
configure do
set :server, :puma
end
get '/' do
'Hello, world!'
end
end
I start it like this (don't suggest to use Rack, please):
Front.start!
Here is my configuration object for Puma, which I don't know how to pass to it:
require 'puma/configuration'
Puma::Configuration.new({ log_requests: true, debug: true })
Seriously, how?
Configuration is tightly connected to a way in which you run puma server.
The standard way to run puma - puma CLI command. In order to configure puma config file config/puma.rb or config/puma/<environment>.rb should be provided (see example).
But you asked how to pass Puma::Configuration object to puma. I wonder why you need it but AFAIK you need to run puma server programmatically in your application code with Puma::Launcher(see source code)
conf = Puma::Configuration.new do |user_config|
user_config.threads 1, 10
user_config.app do |env|
[200, {}, ["hello world"]]
end
end
Puma::Launcher.new(conf, events: Puma::Events.stdio).run
user_config.app may be any callable object (compatible with Rack interface) like Sinatra application.
Hope it's helpful.
Do you want to pass exactly an object or just a configuration in general? For the last option it's possible, but Puma will not log anything anyway (I'm not sure, but seems like you worry exactly about logging settings for Puma).
#!/usr/bin/env ruby
# frozen_string_literal: true
require 'bundler/inline'
gemfile(true) do
gem 'sinatra'
gem 'puma'
gem 'openssl'
end
require 'sinatra/base'
class Front < Sinatra::Base
configure do
set :server, :puma
set :server_settings, log_requests: true, debug: true, environment: 'foo'
end
get '/' do
'Hello, world!'
end
end
Front.start!
I don't get capybara working with my modular sinatra app. I created a small test app and have the same issue. I get always a 504 Timeout Error and i think that the app isn't really loaded by Capybara regardless the Line Capybara.app = Testapp in the acceptance_helper.
Browser Firefox opens when test is running but don't go to any URL and shows always a blank site.
When i do the same test only with rspec it works (second one).
What i'm doing wrong?
My Setup:
OS: Win 7 Professional 64-bit
Ruby version: ruby 2.2.2p95 (2015-04-13 revision 50295) [x64-mingw32]
Firefox version: 40.0.3
gem versions
- sinatra (1.4.6)
- rspec (3.3.0)
- capybara (2.7.1)
- selenium-webdriver (2.53.4)
- thin (1.5.1)
- rack (1.6.4)
- rack-test (0.6.3)
App structure:
test_sinatra_capybara
|
|- testapp.rb
|- spec
| |- spec_helper.rb
| |- acceptance_helper.rb
| |- testapp_spec.rb
|- views
| |- index.slim
testapp.rb
require 'sinatra'
require 'sinatra/base'
require 'slim'
class Testapp < Sinatra::Base
get '/' do
slim :index
end
end
index.slim
#test-id
| Willkommen
spec_helper.rb
require 'rspec'
require 'rack/test'
require_relative '../testapp'
Testapp.environment = :test
module RSpecMixin
include Rack::Test::Methods
def app() Testapp end
end
RSpec.configure do |config|
config.include RSpecMixin
# Use color in STDOUT
config.color = true
# Use the specified formatter
config.formatter = :documentation
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
acceptance_helper.rb
require File.dirname(__FILE__) + '/spec_helper'
require 'capybara'
require 'capybara/rspec'
Capybara.default_driver = :selenium
Capybara.app = Testapp
RSpec.configure do |config|
config.include Capybara::DSL
config.include Capybara::RSpecMatchers
end
testapp_spec.rb
require File.dirname(__FILE__) + '/acceptance_helper'
describe 'URLs that require login' do
it "start page capybara" do
# I tried both
visit '/'
# visit 'http:localhost:4567'
page.should have_content("Willkommen")
end
it "start page rspec" do
get "/"
expect(last_response.body).to include("Willkommen")
end
end
So as i said, last test is working:
Finished in 12.91 seconds (files took 2.54 seconds to load)
2 examples, 1 failure
Failed examples:
rspec ./spec/testapp_spec.rb:4 # URLs that require login start page capybara
Error on First test:
Selenium::WebDriver::Error::WebDriverError:
unexpected response, code=504, content-type="text/html"
Running the exact code posted works fine for me, which I'm guessing means one of two things. Either the version of Firefox you're using is outdated (make sure you're using 47.0.1) or you're on a machine that has a proxy server or firewall that is interfering with requests to 127.0.0.1:<some random port>. The default Capybara config starts up a server on 127.0.0.1:<random port> to handle requests for the app under test. If those connections are blocked or proxied elsewhere it will obviously prevent things from working properly. If you need to bind to a different interface or set a fixed port to get around corporate firewall/proxy restrictions you can do that by setting Capybara.server_host and Capybara.server_port
when i try to start sinatra, i'm getting following error
/var/lib/gems/1.9.1/gems/sinatra-1.4.4/lib/sinatra/base.rb:1488:in start_server': undefined methodrun' for HTTP:Module (NoMethodError)
require 'sinatra/base'
require_relative "twt.rb"
class SinatraApp < Sinatra::Base
set :static, true
set :public_folder, File.dirname(__FILE__) + '/static'
get '/getuserinfo' do
#user = twit.getuserinfo
erb :userInfo
end
end
SinatraApp.run!
in "twt.rb" i require twitter (5.7.1)
require 'twitter'
class Twit
attr_accessor :client
def initialize(consumer_key,consumer_secret,access_token,access_token_secret)
#client = Twitter::REST::Client.new do |config|
config.consumer_key = consumer_key
config.consumer_secret = consumer_secret
config.access_token = access_token
config.access_token_secret = access_token_secret
end
end
def getUserInfo
return user = {
"name"=> client.current_user.name,
"id" => client.current_user.id
}
end
def showAllFriends
client.friends.each { |item| puts item.name }
end
def showFollowers
client.followers.each { |item| puts item.screen_name }
end
def showAllTweets
client.user_timeline.each {|item| puts item.text}
end
def showAllUserTweets(userScreenName)
client.user_timeline(userScreenName).each {|item| puts item.text}
end
def sendTweet(content)
client.update(content)
end
end
if i remove require_relative "twt.rb" line sinatra works fine.
When you run a Sinatra app using the built-in web server (as you do with SinatraApp.run!), Sinatra tries to determine which server to use by checking a list of servers in turn to see which is available. The actual list depends on the version of Ruby you are using, but one server that it always checks is net-http-server, which is simply named HTTP.
The way Sinatra checks for the availability of a server is by using a rack method that calls const_get to try and find the constant Rack::Handler::<server-name>. However, due to the way const_get works, if that constant is not available, but a top level constant with the same name as server-name is, then that will be returned, whatever class it is. (This is arguably a bug in Rack).
The Twitter gem depends on the http gem, and that in turn defines a HTTP module. (Naming a top-level module with something as generic as HTTP is arguably not a good idea).
So what is happening in this case is Sinatra is checking to see if the HTTP server is available, but Rack is returning the HTTP module from the http gem, which isn’t a server. Not being a Rack server it doesn’t have a run method, so when Sinatra tries to use it as one you get the error start_server': undefined method `run' for HTTP:Module.
One workaround is not to use the built-in server, such as the way you have discovered using a config.ru file and starting the app with rackup.
Another solution is to explicitly specify the server to use in your Sinatra app. For example you could install Thin, and then use:
set :server, 'thin'
In fact simply installing Thin would be sufficient as Thin is searched for before HTTP, but you are probably better explicitly setting the server to use. If you cannot install any other server for any reason you could use Webrick instead:
set :server, 'webrick'
i found the solution.
i launch sinatra with config.ru and it works now.
rack config.ru
I am writing a gem which includes a Sinatra application that a developer can extend. For example:
# gem code:
require 'sinatra'
module Mygem
class Application < Sinatra::Base
get 'auth/login' {}
get 'auth/logout {}
end
end
# developer code:
require 'mygem'
class DeveloperApp < Mygem::Application
# ..
end
I am also getting started using RSpec. How should I configure RSpec for testing this functionality?
The references above are all informative and useful but mostly rails specific. I found it quite hard to find a simple recipe for a basic test of a modular Sinatra app, so I am hoping this will answer the question for others. Here is a completely bare-bones, small as possible test. This is probably not the only way to do it, but it works well for a modular app:
require 'sinatra'
class Foo < Sinatra::Base
get '/' do
"Hello"
end
end
require 'rack/test'
describe Foo do
include Rack::Test::Methods
def app
Foo.new
end
it "should be testable" do
get '/'
last_response.should be_ok
end
end
Note that there is no need to have the server running when you launch the test (some tutorials I saw implied that you do) - it's not an integration test.
It's actually pretty simple -- just add rspec to your gemfile (then bundle install), and make a directory in your gem called spec/. Once you've done that, add a file spec/spec_helper.rb that contains some configuration for rspec (mostly requiring various files from your library) as well as defining some helper methods for your specs. Then, for each model and controller, make a file called my_model_name_spec.rb or my_controller_name_spec.rb, and do the test there.
Here are some useful resources for getting started with rspec:
Railscasts:
http://railscasts.com/episodes/275-how-i-test
http://railscasts.com/episodes/71-testing-controllers-with-rspec
http://railscasts.com/episodes/157-rspec-matchers-macros/
And for some more advanced (but well-explained) stuff:
http://benscheirman.com/2011/05/dry-up-your-rspec-files-with-subject-let-blocks
Be sure to include the rack-test gem.
You spec helper should have:
require 'rack/test'
require 'foo' # or where ever your app is
# This can go in a helper somewhere
module AppHelper
def app
Foo.new
end
end
RSpec.configure do |config|
config.include Rack::Test::Methods
config.include AppHelper
end
Then, your spec can be as follows:
require 'spec_helper'
# Example app. Delete this example.
class Foo < Sinatra::Base
get '/' do
'Jesse Pinkman'
end
end
describe Foo do
it 'is testable' do
get '/' do
expect(last_response).to be_ok
end
end
end
Ok so. I'm wanting to do request specs with RSpec for my Sinatra app.
I have a config.ru
# config.ru
require File.dirname(__FILE__) + '/config/boot.rb'
map 'this_route' do
run ThisApp
end
map 'that_route' do
run ThatApp
end
The boot.rb just uses Bundler and does additional requires for the rest of the app:
ThisApp looks like:
# lib/this_app.rb
class ThisApp < Sinatra::Base
get '/hello' do
'hello'
end
end
So I'm using RSpec and I want to write request specs like:
# spec/requests/this_spec.rb
require_relative '../spec_helper'
describe "This" do
describe "GET /this_route/hello" do
it "should reach a page" do
get "/hello"
last_response.status.should be(200)
end
end
it "should reach a page that says hello" do
get "/hello"
last_response.body.should have_content('hello')
end
end
end
end
This works fine because my spec_helper.rb is setup as follows:
# spec/spec_helper.rb
ENV['RACK_ENV'] = "test"
require File.expand_path(File.dirname(__FILE__) + "/../config/boot")
require 'capybara/rspec'
RSpec.configure do |config|
config.include Rack::Test::Methods
end
def app
ThisApp
end
But my problem is that I want to test "ThatApp" from my rackup file along with any amount more apps I might add later along with "ThisApp". For example if I had a second request spec file:
# spec/requests/that_spec.rb
require_relative '../spec_helper'
describe "That" do
describe "GET /that_route/hello" do
it "should reach a page" do
get "/hello"
last_response.status.should be(200)
end
end
it "should reach a page that says hello" do
get "/hello"
last_response.body.should have_content('hello')
end
end
end
end
RackTest requires the rack app I'm testing be defined in the spec_helper file with that 'app' method, and I think eventually I'm going to have to feed Capybara.app the same thing as well when doing further request specs with it.
I feel like I'm missing something and maybe there's an easy way to setup 'app' for RackTest and Capybara at runtime depending on what route and consequent rack app I'm testing in my request specs. Like a before filter in RSpec.configure maybe, but I can't think of or find how I'd access what rack app is currently loaded and try and set it there before the test suite runs.
Anyone get what I'm trying to do and can think of something? Thanks for any help.
Define a different helper module for each sinatra app you want to test, each of which should define its own app method that returns the corresponding app. Then you can simply include MySinatraAppHelper in the appropriate example groups where you want to test the the given app.
You can also use rspec metadata to have the module included in example groups automatically.
Have a look at my sinatra-rspec-bundler-template. Especially at the spec files. I think that's what you are trying to achieve.
It combines two separate Sinatra apps each with it's own specs.