Created
May 14, 2014 16:55
-
-
Save DavidColby/ebc3559e5547e275f37f to your computer and use it in GitHub Desktop.
Testing basic Sinatra app with rspec and capybara
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
source 'https://rubygems.org' | |
ruby '2.1.0' | |
gem 'rspec', '~> 2.14.0' | |
gem 'capybara' | |
gem 'pry-debugger', '~> 0.2.2' | |
gem 'sinatra' | |
gem 'sinatra-contrib' # Not required | |
gem 'thin' # Not required | |
gem 'rack-test' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'spec_helper' | |
describe "Sinatra App" do | |
context 'Visiting the home page' do | |
before (:each) do | |
# Visit root before each test | |
visit('/') | |
end | |
context 'Visiting Nav Links' do | |
specify 'Projects link should go to Projects page' do | |
# Find the a tag on the page named Projects. Case sensitive | |
click_link('Projects') | |
# After clicking link we should be on the /projects page | |
# The project page has an h1 Create New Project | |
expect(page).to have_content('Create New Project') | |
end | |
specify 'Tasks link should go to Tasks page' do | |
click_link('Tasks') | |
expect(page).to have_content('Create New Task') | |
end | |
specify 'Employees link should to Employees page' do | |
click_link('Employees') | |
expect(page).to have_content('Create New Employee') | |
end | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# spec_helper.rb | |
require './lib/task-manager.rb' | |
require './lib/server.rb' | |
require 'rspec' | |
require 'rack/test' | |
require 'capybara/rspec' | |
ENV['RACK_ENV'] = 'test' | |
# Mixin for testing sinatra | |
module RSpecMixin | |
include Rack::Test::Methods | |
def app | |
Sinatra::Application | |
end | |
Capybara.app = Sinatra::Application.new | |
end | |
RSpec.configure do |config| | |
# Configure each test to always use a new singleton instance | |
config.before(:each) do | |
TM.instance_variable_set(:@__db_instance, nil) | |
end | |
config.include RSpecMixin | |
config.include Capybara::DSL | |
end | |
Capybara.configure do |config| | |
config.include RSpecMixin | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment