BDD with Cucumber and RSpec. Marcus Ahnve Valtech AB marcus.ahnve@valtech.se @mahnve



Similar documents
Behavior Driven Development

Cucumber: Finishing the Example. CSCI 5828: Foundations of Software Engineering Lecture 23 04/09/2012

Application Testing with Capybara

Most of the security testers I know do not have

Guides.rubyonrails.org

Evaluation. Chapter 1: An Overview Of Ruby Rails. Copy. 6) Static Pages Within a Rails Application

Cucumber and Capybara

Building and Deploying Web Scale Social Networking Applications Using Ruby on Rails and Oracle. Kuassi Mensah Group Product Manager

Rake Task Management Essentials

The Cucumber Book. Extracted from: Behaviour-Driven Development for Testers and Developers. The Pragmatic Bookshelf

1. Was ist das? II. Wie funktioniert das? III. Wo funktioniert das nicht?

The NetBeans TM Ruby IDE: You Thought Rails Development Was Fun Before

SERVICE-ORIENTED DESIGN WITH RUBY AND RAILS

Hudson Continous Integration Server. Stefan Saasen,

Pete Helgren Ruby On Rails on i

RailsApps Project Subscription Site with Recurly

How To Create A Model In Ruby (Orm)

Lecture 3 ActiveRecord Rails persistence layer

Rails 4 Quickly. Bala Paranj.

Content Management System (Dokument- og Sagsstyringssystem)

Installing Rails 2.3 Under CentOS/RHEL 5 and Apache 2.2

This tutorial has been designed for beginners who would like to use the Ruby framework for developing database-backed web applications.

CS169.1x Lecture 5: SaaS Architecture and Introduction to Rails " Fall 2012"

Ruby On Rails. CSCI 5449 Submitted by: Bhaskar Vaish

How To Write A Book Purchase On An Ipad With A Bookshop On A Pcode On A Linux (Windows) On A Macbook On A Microsoft Powerbook 2 (Windows 2.

The IBM i on Rails + + Anthony Avison anthony@powerruby.com. Copyright 2014 PowerRuby, Inc.

Writing Software not code With. Ben Mabey

Web Framework Performance Examples from Django and Rails

Enterprise Recipes with Ruby and Rails

Comparison of modern web frameworks. Nikita Klyukin

Unit and Functional Testing for the ios Platform. Christopher M. Judd

Ruby on Rails on Minitest

Ruby on Rails Web Mashup Projects

Installing Rails 2.3 Under Windows XP and Apache 2.2

Tools and Integration

Certified Redmine Project Management Professional Sample Material

Capybara. Exemplos de configuração. Com cucumber-rails. Com cucumber sem Rails. Tags para uso de JS. Nos steps do cucumber. Utilizando com RSpec

Comparing Dynamic and Static Language Approaches to Web Frameworks

Ruby & Ruby on Rails for arkitekter. Kim Harding Christensen khc@kimharding.com

The Other mod_rails: Easy Rails Deployment with JRuby. Nick Sieger Sun Microsystems, Inc

Open Source in Mobile Test Automation. Ru Cindrea - Altom ru@altom.ro

RESTful Rails Development. translated by Florian Görsdorf and Ed Ruder

Brakeman and Jenkins: The Duo Detects Defects in Ruby on Rails Code

Outline. Lecture 18: Ruby on Rails MVC. Introduction to Rails

Why Ruby On Rails? Aaron Bartell Copyright 2014 PowerRuby, Inc.

Ruby On Rails A Cheatsheet. Ruby On Rails Commands

Ruby on Rails. Computerlabor

Ruby on Rails (Ruby 1.9.2, Rails 3.1.1) Installation

A Puppet Approach To Application Deployment And Automation In Nokia. Oliver Hookins Principal Engineer Services & Developer Experience

Installing Ruby on Windows XP

The Learn-Verified Full Stack Web Development Program

Agile Web Development with Rails 4

Testing Frameworks (MiniTest)

Project(Submission(&(Assignment(System( PROJECT(REPORT(

New Relic & JMeter - Perfect Performance Testing

Dasharatham Bitla (Dash)

Installation overview

Ocean Support Tips and Tricks Webinar November 2015

Praise for Michael Hartl s Books and Videos on Ruby on Rails

automated acceptance testing of mobile apps

Apache Thrift and Ruby

Ruby on Rails in GlassFish Sun Microsystems

Ruby on Rails is a web application framework written in Ruby, a dynamically typed programming language The amazing productivity claims of Rails is

Deep in the CRUD LEVEL 1

CompSci 125 Lecture 08. Chapter 5: Conditional Statements Chapter 4: return Statement

Continuous integration with Jenkins CI

Curriculum Vitae. Gastón Ramos -

Infopark CMS Fiona. Rails Connector for CMS Fiona

Overview. What is software testing? What is unit testing? Why/when to test? What makes a good test? What to test?

Scaling Rails with memcached

Rebuilding Rails. Get Your Hands Dirty and Build Your Own Ruby Web Framework. DRM-free. Please copy for yourself and only yourself.

A Beginner's Guide to Security with Ruby on Rails in BSD. Corey Benninger

Makumba and Ruby on Rails

J a v a Quiz (Unit 3, Test 0 Practice)

Transcription:

BDD with Cucumber and RSpec Marcus Ahnve Valtech AB marcus.ahnve@valtech.se @mahnve

Software Developer Polyglot Programmer About me

Q: How many are using Cucumber

Q: How many have heard of Cucumber

Q: How many are using RSpec

Q: How many have heard of RSpec

Q: How many are doing TDD/BDD?

Q: How many think I am asking too many questions?

My BDD history Java and Ruby developer TestDox, Joe Walnes Dan North Dave Astels, Aslak Hellesøy, David Chelimsky

$ rake Finished in 27.79 seconds 285 examples, 0 failures 152 scenarios (152 passed) 1363 steps (1363 passed) 4m4.505s Finished in 8.06 seconds 77 examples, 0 failures

Why automatic testing? Make sure it works now Make sure it works whenever

Why TDD? Make sure it works now Make sure it works whenever Know when you're done

Method Testing class Klazz def method do_something end end class KlazzTest def setup @klazz = Klazz.new end def test_method result = @klazz.do_something assert_equal("expected", result) end end

The Context Problem class KlazzTest def setup @klazz = Klazz.new end def test_method result = @klazz.do_something assert_equal(:expected, result) end def test_another_context klazz = Klazz.new(:constructor_args) assert_equal(:expected_2, klazz.do_something) end end

Why BDD? Make sure it works now Make sure it works whenever Know when you're done Build just what's needed Design from functionality Documentation - why

What is BDD? "BDD is TDD done right" "TDD means 'write the test first'. BDD takes this idea to a more general level: 'write the client first' (the outside)" - Aslak Hellesøy

The BDD Flow Write a failing feature Implement feature 0.. * Implement spec Write a failing spec

$ rails new blog -T create create README create Rakefile create config.ru create.gitignore create Gemfile create app create app/controllers/application_controller.rb... create vendor/plugins/.gitkeep

source 'http://rubygems.org' gem 'rails', '3.0.0' gem 'sqlite3-ruby', :require => 'sqlite3' group :test do gem 'rspec-rails' gem 'cucumber-rails' gem 'capybara' end

$ bundle install master(mon,apr04) Fetching source index for http://rubygems.org/ Using rake (0.8.7)... Installing rack (1.2.2) Installing rack-mount (0.6.14)... Installing gherkin (2.3.5) with native extensions Installing term-ansicolor (1.0.5) Installing cucumber (0.10.2) Installing cucumber-rails (0.4.0)... Installing rspec (2.5.0) Installing rspec-rails (2.5.0) Using sqlite3 (1.3.3) Using sqlite3-ruby (1.3.3)

$ rake db:migrate

$ rails generate cucumber:install create config/cucumber.yml create script/cucumber chmod script/cucumber create features/step_definitions create features/step_definitions/web_steps.rb create features/support create features/support/paths.rb create features/support/selectors.rb create features/support/env.rb exist lib/tasks create lib/tasks/cucumber.rake gsub config/database.yml gsub config/database.yml force config/database.yml

$ rails generate rspec:install master(mon,apr04) create.rspec create spec create spec/spec_helper.rb

Feature: User writes blog post In order for other people to read brilliant thoughts A user can post a blog post Scenario: Given I am on the new blog post page When I write a blog post And it has the title "My great Idea" And it has the body "Body body body" And I press "Save" Then I should see "My great Idea"

$ rake 1 scenario (1 failed) 6 steps (1 failed, 2 skipped, 3 undefined) 0m1.103s You can implement step definitions for undefined steps with these snippets:

When /^I write a blog post$/ do pending # express the regexp above with the code you wish you had end When /^it has the title "([^"]*)"$/ do arg1 pending # express the regexp above with the code you wish you had end When /^it has the body "([^"]*)"$/ do arg1 pending # express the regexp above with the code you wish you had end

$ rake Given I am on the new blog post page # features/step_definitions/web_steps.rb:44 Can't find mapping from "the new blog post page" to a path. Now, go and add a mapping in /home/mahnve/src/presentations/cucumber/src/blog/features/su

module NavigationHelpers def path_to(page_name) case page_name when /the home\s?page/ '/' when /the new blog post page/ '/blog_posts/new'

$ rake Scenario: # features/user_writes_blogpost.feature:6 Given I am on the new blog post page # features/step_definitions/web_steps.rb:44 No route matches "/blogpost/new" (ActionController::RoutingError)

$ rails generate resource BlogPost title:string body:string invoke active_record create db/migrate/20110404155134_create_blog_posts.rb create app/models/blog_post.rb invoke test_unit create test/unit/blog_post_test.rb create test/fixtures/blog_posts.yml invoke controller create app/controllers/blog_posts_controller.rb invoke erb create app/views/blog_posts invoke test_unit create test/functional/blog_posts_controller_test.rb invoke helper create app/helpers/blog_posts_helper.rb invoke test_unit create test/unit/helpers/blog_posts_helper_test.rb route resources :blog_posts

$ rake (in /home/mahnve/src/presentations/cucumber/src/blog) You have 1 pending migrations: 20110404155134 CreateBlogPosts

class CreateBlogPosts < ActiveRecord::Migration def self.up create_table :blog_posts do t t.string :title t.string :body t.timestamps end end def self.down drop_table :blog_posts end end

$ rake db:migrate (in /home/mahnve/src/presentations/cucumber/src/blog) == CreateBlogPosts: migrating ====================================== -- create_table(:blog_posts) -> 0.0010s == CreateBlogPosts: migrated (0.0010s) ================================

$ rake Given I am on the new blog post page # features/step_definitions/web_steps.rb:44 The action 'new' could not be found for BlogPostsController (AbstractController::ActionNotFound

gem 'inherited_resources'

class BlogPostsController < InheritedResources::Base end

$ rake Missing template blog_posts/new with...

gem 'formtastic' gem 'haml'

- semantic_form_for @blog_post do form = form.inputs = form.buttons

$ rake Given I am on the new blog post page # features/step_definitions/web_steps.rb:44 When I write a blog post # features/step_definitions/blogpost_steps.rb:1 TODO (Cucumber::Pending)./features/step_definitions/blogpost_steps.rb:2:in `/^I write a blog post$/'

Oops Steps where not really optimal

When /^I write a blog post with title "([^"]*)" and the body "([^"]*)"$/ do fill_in 'Title', :with => title fill_in 'Body', :with => body end

Given I am on the new blog post page When I write a blog post with title "My great Idea" and body "Body body body" And I press "Create Blog post" Then I should see "My great Idea"

$ rake... Missing template blog_posts/show

= @blog_post.title

Scenario: Create without title Given I am on the new blog post page When I write a blog post without a title And I press "Create Blog post" Then I should see "can't be blank"

$ rake Then I should see "Required" # features/step_definitions/web_steps.rb:105 expected #has_content?("required") to return true, got false (RSpec::Expectations::Expectatio

require 'spec_helper' describe BlogPost do it {should validate_presence_of :title} end

group :test, :development do gem 'rspec-rails' end group :test do gem 'cucumber-rails' gem 'capybara' gem 'database_cleaner' gem 'shoulda-matchers' end

$ rake 1) BlogPost Failure/Error: it {should validate_presence_of :title} Expected errors to include "can't be blank" when title is set to nil, got no errors

class BlogPost < ActiveRecord::Base validates_presence_of :title end

$ rake. Finished in 0.02658 seconds 1 example, 0 failures 2 scenarios (2 passed) 8 steps (8 passed) 0m1.084s

Java package cukes; import cuke4duke.annotation.i18n.en.given; public class BlogPostSteps { @Given("^I write a blog post with title \\"([^\\"]*)\\" and body \\"([^\\"]*)\\ public void writeblogpost (String title, String body) {... } }

Groovy this.metaclass.mixin(cuke4duke.groovydsl) Given(~/^I write a blog post with title "([^"]*)" and body "([^"]*)"$/) { String title, String body... }

Thank You!