Skip to content

Instantly share code, notes, and snippets.

@JoshCheek
Created July 21, 2014 21:35
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save JoshCheek/989a099594059ebf3015 to your computer and use it in GitHub Desktop.
Save JoshCheek/989a099594059ebf3015 to your computer and use it in GitHub Desktop.
example of how to use rack test with minitest
require 'sinatra/base'
require 'minitest/autorun'
require 'minitest/spec'
require "rack/test"
require 'nokogiri'
class MyApp < Sinatra::Base
enable :inline_templates
get '/users/:id' do
"User number #{params[:id]} is an uppity know-it-all"
end
get '/start' do
redirect '/finish'
end
get '/finish' do
"You are at /finish, you came from #{env['HTTP_REFERER']}"
end
get '/has_html' do
<<-HTML
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-type" content="text/html; charset=utf-8">
<title>Omg, hello!</title>
<faketag>don't find me!</faketag>
</head>
<body>
<h1>A few of my favourite numbers!</h1>
<ul><li>1</li>
<li>2</li>
<li>9</li>
<li>3</li>
</ul>
<faketag>Find me!</faketag>
</body>
</html>
HTML
end
get '/get_request' do
''
end
post '/users' do
params.inspect
end
end
describe 'Examples' do
include Rack::Test::Methods
def app
MyApp.new
end
# list of methods from: https://github.com/brynary/rack-test/blob/d798a3dafd1ae3c5a7156b5c2a8c88a704b07a2d/lib/rack/test/methods.rb#L59-77
it "pulls out params" do
get "/users/456"
assert last_response.ok?
assert_includes last_response.body, "User number 456"
end
it 'redirects' do
get '/start'
assert last_response.redirect?
follow_redirect!
assert_equal "http://example.org/finish", last_request.url
assert_equal "You are at /finish, you came from http://example.org/start", last_response.body
end
it 'parses html' do
get '/has_html'
doc = Nokogiri::HTML(last_response.body)
# find all nodes that match this css selector
values = doc.css('li').map { |li| li.text }
assert_equal %w[1 2 9 3], values
# find first node that matches this css selector
assert_equal "Omg, hello!", doc.at_css('title').text
# get the attribute off a node
assert_equal 'text/html; charset=utf-8', doc.at_css('meta')['content']
# find the li for 9
assert_equal '9', doc.css('li').find { |li| li.text == '9' }.text
# find the faketag in the body with css selectors
assert_equal 'Find me!', doc.at_css('body faketag').text
# find the faketag in the body with a subquery selectors
body = doc.at_css('body')
assert_equal 'Find me!', body.at_css('faketag').text
end
it "knows success (200) and not found (404) can't find endpoint if the method doesn't match" do
get '/get_request'
assert last_response.ok?
assert_equal 200, last_response.status
post '/get_request'
assert last_response.not_found?
assert_equal 404, last_response.status
end
it "posts a form, and everything gets turned into keys when going through http" do
post '/users', user: { name: 'Carla', age: '28' }
assert_equal '{"user"=>{"name"=>"Carla", "age"=>"28"}}', last_response.body
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment