Autotesting rails app

Post on 16-Jul-2015

101 views 3 download

Tags:

Transcript of Autotesting rails app

Autotesting Rails

Covering Rails MVP with autotests

Contents

● Why?

● Rspec, shoulda-matchers, FactoryGirl

● Cucumber

● Simplecov

● What next?

WHY?

Problem

● MVP is ready

● First customers

● Not stable

FAIL

Solution

● Manual testing = expensive (time + $)

● Autotesting?

● Yes, we can!

TDD - Rspec, shoulda-matchers, FactoryGirl

● All instructions

https://github.com/rspec/rspec-rails

Rspec - testing framework

Rspec - update Gemfile

group :development, :test do

gem 'rspec-rails', '~> 3.0'

end

Rspec - install and generate files

$ bundle install --without production

$ rails generate rspec:install

$ bundle exec rspec

Shoulda-matchers - testing one liners

● All instructions

https://github.com/thoughtbot/shoulda-

matchers

Shoulda - update Gemfile

group :test do

gem 'shoulda-matchers', require: false

end

Run

$ bundle install --without production

Shoulda - update rails_helper

● in spec/rails_helper.rb

● after

require 'rspec/rails'

● add

require 'shoulda/matchers'

FactoryGirl - setting up test objects

● All instructions

https://github.com/thoughtbot/factory_girl

FactoryGirl - update Gemfile

group :test do

gem 'factory_girl'

end

Run

$ bundle install --without production

FactoryGirl - update Gemfile

group :test do

gem 'factory_girl'

end

Run

$ bundle install --without production

Generate test files for existing model

Run (replace Post with your existing model)

$ rails generate model Post -s

See generated spec and factory files

BDD - Cucumber

● All instructions specific to Rails

https://github.com/cucumber/cucumber-rails

● And overall http://cukes.info/

Cucumber - testing framework

Cucumber - update Gemfile

group :test do

gem 'cucumber-rails', :require => false

gem 'database_cleaner'

end

Cucumber - update env.rb

Add to the end of features/support/env.rb

World FactoryGirl::Syntax::Methods

Cucumber - install

$ bundle install --without production

$ rails generate cucumber:install

$ bundle exec cucumber

Capybara-webkit - testing javascript

● All instructionso https://github.com/thoughtbot/capybara-webkit

o https://github.com/leonid-shevtsov/headless

Capybara-webkit - update Gemfile

group :test do

gem 'capybara-webkit', '~> 1.3.0'

gem 'headless'

end

Run

$ sudo apt-get install xvfb

$ bundle install --without production

Cucumber - update env.rb

Add to features/support/env.rbCapybara.app = Rack::ShowExceptions.new(Rails.application)

# use webkit driver for javascript testing

Capybara.javascript_driver = :webkit

if Capybara.javascript_driver == :webkit

require 'headless'

headless = Headless.new

headless.start

end

Code coverage with tests

● All instructions

https://github.com/colszowka/simplecov

Simplecov - code coverage

Simplecov - update Gemfile

group :test do

gem 'simplecov', :require => false

end

Run

$ bundle install --without production

Simplecov - update rails_helper

Add at the very top of spec/rails_helper.rb

require 'simplecov'

SimpleCov.command_name 'RSpec'

Simplecov - update env.rb

Add at the very top of features/support/env.rb

require 'simplecov'

SimpleCov.command_name 'Cucumber'

Add file .simplecov to rails app root

SimpleCov.start 'rails' do

end

Update .gitignore

Append to the file

/coverage

Code coverage

Run

$ bundle exec rspec

$ bundle exec cucumber

Open in the browser

coverage/index.html

Code coverage report

What next?

● https://www.google.com.ua/search?q=rspec

+tutorial

● https://www.google.com.ua/search?q=cucum

ber+tutorial

Get down to work and write tests

● $$$ Jira + Stash + Bamboo

● Free (even private) Bitbucket + Jenkins*

● Free Github + Jenkins*

* you need servers

Setup continuous integration

Anton Naumenko

www.syndicode.co