SlideShare a Scribd company logo
1 of 14
10/1/2015
Getting Started with
RSpec…Capybara and whatnot
Lightning Talk hosted by The Firehose Project
Myo Thant Kyaw
What exactly is RSpec?
Behavioral Driven Development
(TDD + Domain Driven Design + Acceptance Test-Driven Planning)
RSpec
Given some context -> When some event occurs -> Then I expect some outcome
DHH’s dislike about RSpec
https://twitter.com/dhh/status/472072157821169664
http://brandonhilkert.com/blog/7-reasons-why-im-sticking-with-minitest-and-fixture
Basic Structure (Syntax)
Given some context -> When some event occurs -> Then I expect some o
Structure:
Given
RSpec.describe StaticPagesController, :type => :controller do
describe "GET index" do
it "renders the index template" do
get :index
expect(response).to render_template("index")
end
end
end
When
Then
WhenGiven Then
RSpec with Ruby
#1: $ gem install rspec
#2: Inside project folder: $ rspec —init
and set up like this
#3: spec/spec_helper.rb >
require_relative '../car'
#4: spec/car_spec.rb >
require 'spec_helper'
describe Car do
before(:each) do
@car = Car.new("Make", "Model", :type)
end
describe "#honk" do
it "returns the correct sound" do
expect(@car.honk).to eq("Ponk ponk")
end
end
#5: $ rspec spec/car_spec.rb -fd
Some RSpec Matchers
✤ expect(@car.make).to eq(“Honda”)
✤ expect(@car.make).to match(/H.n.a/)
✤ expect(@visible).to be(true)
✤ expect(@numbers).to match_array([4,5,2,8])
✤ expect(alphabet).to start_with("a")
✤ expect(alphabet).to end_with(“z")
✤ expect(alphabet).to start_with("a").and end_with(“z")
✤ expect(stoplight.color).to eq("red").or eq("green").or eq(“yellow")
✤ http://rspec.info/blog/2014/01/new-in-rspec-3-composable-matchers/
✤ RSpec Cheatsheet: http://www.anchor.com.au/wp-content/uploads/rspec_cheatsheet_attributed.pdf
RSpec with Rails - 1
Gemfile >
group :development, :test do
gem 'rspec-rails', '~> 3.0'
gem 'factory_girl_rails'
end
group :test do
gem 'faker'
gem 'capybara'
gem 'guard-rspec'
gem 'selenium-webdriver', '~> 2.47.1'
end
$ bundle install
RSpec with Rails - 2
$ rails g rspec:install
Delete > test folder
spec/rails_helper.rb >
require 'capybara/rspec'
Install spec files for existing controllers and models >
$ rails g rspec:controller StaticPagesController
$ rails g rspec:model Course
(RSpec generates spec files automatically upon rails g controller/model)
RSpec with Rails - Controller Test
# controllers/selfies_controller.rb
require 'rails_helper'
RSpec.describe StaticPagesController, :type => :controller do
describe "GET index" do
it "renders the index template" do
get :index
expect(response).to render_template("index")
end
end
describe "GET about" do
it "renders the about page" do
get :about
expect(response).to render_template("about")
end
end
end
Controller Test
RSpec with Rails - Model Test
# spec/models/selfy_spec.rb
require 'rails_helper'
RSpec.describe Selfy, :type => :model do
describe "selfy" do
selfy = FactoryGirl.build(:selfy)
it "has a valid factory title" do
expect(selfy.title).to eq("This is test selfy title")
end
it "has a valid factory description" do
expect(selfy.description).to eq("This is test selfy description")
end
end
Model Test
RSpec with Rails
Feature Test with Capybara -1
# spec/features/user_visits_homepage_spec.rb
require "rails_helper"
RSpec.feature "User visits homepage", :type => :feature do
scenario "successfully", :js => true do
visit root_path
expect(page).to have_text("Welcome to Selfie There")
end
end
Capybara
Selenium JS Web Driver
Feature Test
Capybara Cheatsheet:
https://learn.thoughtbot.com/test-driven-rails-resources/capybara.pdf
(OPTIONAL)
RSpec with Rails
Feature Test with Capybara -2
# spec/features/user_signs_in_spec.rb
require "rails_helper"
RSpec.feature "User signs in", :type => :feature do
scenario "successfully", :js => true do
user = FactoryGirl.build(:user)
visit "/users/sign_in"
fill_in "Email", :with => user.email
fill_in "Password", :with => user.password
click_button "Log in"
expect(page).to have_text("Signed in successfully.")
end
end
Feature Test
Tips and tricks
Know the difference between FactoryGirl.create() and FactoryGirl.build()
If your test says something like email exists
=> $ rake db:test:prepare
Know how fixtures work and maybe use database_cleaner gem to avoid pos
Use guard gem to run automated testing
=> $ guard init and leave the tab open like you are running rails server
some .rspec options
.rspec >
—color
—format documentation
Thank You!!!
Myo Thant Kyaw

More Related Content

What's hot

What's hot (20)

Javascript interview questions and answers
Javascript interview questions and answersJavascript interview questions and answers
Javascript interview questions and answers
 
Gitlab CI/CD
Gitlab CI/CDGitlab CI/CD
Gitlab CI/CD
 
Gitops Hands On
Gitops Hands OnGitops Hands On
Gitops Hands On
 
Version control system
Version control systemVersion control system
Version control system
 
GitOps: Git come unica fonte di verità per applicazioni e infrastruttura
GitOps: Git come unica fonte di verità per applicazioni e infrastrutturaGitOps: Git come unica fonte di verità per applicazioni e infrastruttura
GitOps: Git come unica fonte di verità per applicazioni e infrastruttura
 
Documentation in the agile software development process
Documentation in the agile software development processDocumentation in the agile software development process
Documentation in the agile software development process
 
Devops Porto - CI/CD at Gitlab
Devops Porto - CI/CD at GitlabDevops Porto - CI/CD at Gitlab
Devops Porto - CI/CD at Gitlab
 
Introduction to BDD
Introduction to BDDIntroduction to BDD
Introduction to BDD
 
Using runbot to test all your developments automatically
Using runbot to test all your developments automaticallyUsing runbot to test all your developments automatically
Using runbot to test all your developments automatically
 
Git101
Git101Git101
Git101
 
Coding standards
Coding standardsCoding standards
Coding standards
 
GitOps with ArgoCD
GitOps with ArgoCDGitOps with ArgoCD
GitOps with ArgoCD
 
Eclipse Training - SWT & JFace
Eclipse Training - SWT & JFaceEclipse Training - SWT & JFace
Eclipse Training - SWT & JFace
 
Git hub
Git hubGit hub
Git hub
 
Trunk based development and Canary deployment
Trunk based development and Canary deploymentTrunk based development and Canary deployment
Trunk based development and Canary deployment
 
Angular 10 course_content
Angular 10 course_contentAngular 10 course_content
Angular 10 course_content
 
FOSDEM 2017: GitLab CI
FOSDEM 2017:  GitLab CIFOSDEM 2017:  GitLab CI
FOSDEM 2017: GitLab CI
 
Google flutter the easy and practical way
Google flutter the easy and practical wayGoogle flutter the easy and practical way
Google flutter the easy and practical way
 
Meetup React Sanca - 29/11/18 - React Testing
Meetup React Sanca - 29/11/18 - React TestingMeetup React Sanca - 29/11/18 - React Testing
Meetup React Sanca - 29/11/18 - React Testing
 
CD using ArgoCD(KnolX).pdf
CD using ArgoCD(KnolX).pdfCD using ArgoCD(KnolX).pdf
CD using ArgoCD(KnolX).pdf
 

Similar to Rspec presentation

A tour on ruby and friends
A tour on ruby and friendsA tour on ruby and friends
A tour on ruby and friends
旻琦 潘
 
Javascript unit testing, yes we can e big
Javascript unit testing, yes we can   e bigJavascript unit testing, yes we can   e big
Javascript unit testing, yes we can e big
Andy Peterson
 
Elasticsearch And Ruby [RuPy2012]
Elasticsearch And Ruby [RuPy2012]Elasticsearch And Ruby [RuPy2012]
Elasticsearch And Ruby [RuPy2012]
Karel Minarik
 

Similar to Rspec presentation (20)

Rspec and Capybara Intro Tutorial at RailsConf 2013
Rspec and Capybara Intro Tutorial at RailsConf 2013Rspec and Capybara Intro Tutorial at RailsConf 2013
Rspec and Capybara Intro Tutorial at RailsConf 2013
 
RSpec and Rails
RSpec and RailsRSpec and Rails
RSpec and Rails
 
Rspec
RspecRspec
Rspec
 
Spark View Engine (Richmond)
Spark View Engine (Richmond)Spark View Engine (Richmond)
Spark View Engine (Richmond)
 
Ruby on Rails - Introduction
Ruby on Rails - IntroductionRuby on Rails - Introduction
Ruby on Rails - Introduction
 
A tour on ruby and friends
A tour on ruby and friendsA tour on ruby and friends
A tour on ruby and friends
 
Capybara and cucumber with DSL using ruby
Capybara and cucumber with DSL using rubyCapybara and cucumber with DSL using ruby
Capybara and cucumber with DSL using ruby
 
Javascript unit testing, yes we can e big
Javascript unit testing, yes we can   e bigJavascript unit testing, yes we can   e big
Javascript unit testing, yes we can e big
 
Elasticsearch And Ruby [RuPy2012]
Elasticsearch And Ruby [RuPy2012]Elasticsearch And Ruby [RuPy2012]
Elasticsearch And Ruby [RuPy2012]
 
Basic RSpec 2
Basic RSpec 2Basic RSpec 2
Basic RSpec 2
 
Alberto Maria Angelo Paro - Isomorphic programming in Scala and WebDevelopmen...
Alberto Maria Angelo Paro - Isomorphic programming in Scala and WebDevelopmen...Alberto Maria Angelo Paro - Isomorphic programming in Scala and WebDevelopmen...
Alberto Maria Angelo Paro - Isomorphic programming in Scala and WebDevelopmen...
 
TDC 2012 - Patterns e Anti-Patterns em Ruby
TDC 2012 - Patterns e Anti-Patterns em RubyTDC 2012 - Patterns e Anti-Patterns em Ruby
TDC 2012 - Patterns e Anti-Patterns em Ruby
 
Behavioural Testing Ruby/Rails Apps @ Scale - Rspec & Cucumber
       Behavioural Testing Ruby/Rails Apps @ Scale - Rspec & Cucumber       Behavioural Testing Ruby/Rails Apps @ Scale - Rspec & Cucumber
Behavioural Testing Ruby/Rails Apps @ Scale - Rspec & Cucumber
 
RSpec 3.0: Under the Covers
RSpec 3.0: Under the CoversRSpec 3.0: Under the Covers
RSpec 3.0: Under the Covers
 
RSpec. Part 2
RSpec. Part 2RSpec. Part 2
RSpec. Part 2
 
Crossing the Bridge: Connecting Rails and your Front-end Framework
Crossing the Bridge: Connecting Rails and your Front-end FrameworkCrossing the Bridge: Connecting Rails and your Front-end Framework
Crossing the Bridge: Connecting Rails and your Front-end Framework
 
Create a new project in ROR
Create a new project in RORCreate a new project in ROR
Create a new project in ROR
 
Ruby on Rails survival guide of an aged Java developer
Ruby on Rails survival guide of an aged Java developerRuby on Rails survival guide of an aged Java developer
Ruby on Rails survival guide of an aged Java developer
 
SCR Annotations for Fun and Profit
SCR Annotations for Fun and ProfitSCR Annotations for Fun and Profit
SCR Annotations for Fun and Profit
 
BDD in iOS with Cedar
BDD in iOS with CedarBDD in iOS with Cedar
BDD in iOS with Cedar
 

Recently uploaded

Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Victor Rentea
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 

Recently uploaded (20)

Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
 
Cyberprint. Dark Pink Apt Group [EN].pdf
Cyberprint. Dark Pink Apt Group [EN].pdfCyberprint. Dark Pink Apt Group [EN].pdf
Cyberprint. Dark Pink Apt Group [EN].pdf
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
CNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In PakistanCNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In Pakistan
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Spring Boot vs Quarkus the ultimate battle - DevoxxUK
Spring Boot vs Quarkus the ultimate battle - DevoxxUKSpring Boot vs Quarkus the ultimate battle - DevoxxUK
Spring Boot vs Quarkus the ultimate battle - DevoxxUK
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challenges
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
 
Exploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusExploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with Milvus
 

Rspec presentation

  • 1. 10/1/2015 Getting Started with RSpec…Capybara and whatnot Lightning Talk hosted by The Firehose Project Myo Thant Kyaw
  • 2. What exactly is RSpec? Behavioral Driven Development (TDD + Domain Driven Design + Acceptance Test-Driven Planning) RSpec Given some context -> When some event occurs -> Then I expect some outcome DHH’s dislike about RSpec https://twitter.com/dhh/status/472072157821169664 http://brandonhilkert.com/blog/7-reasons-why-im-sticking-with-minitest-and-fixture
  • 3. Basic Structure (Syntax) Given some context -> When some event occurs -> Then I expect some o Structure: Given RSpec.describe StaticPagesController, :type => :controller do describe "GET index" do it "renders the index template" do get :index expect(response).to render_template("index") end end end When Then WhenGiven Then
  • 4. RSpec with Ruby #1: $ gem install rspec #2: Inside project folder: $ rspec —init and set up like this #3: spec/spec_helper.rb > require_relative '../car' #4: spec/car_spec.rb > require 'spec_helper' describe Car do before(:each) do @car = Car.new("Make", "Model", :type) end describe "#honk" do it "returns the correct sound" do expect(@car.honk).to eq("Ponk ponk") end end #5: $ rspec spec/car_spec.rb -fd
  • 5. Some RSpec Matchers ✤ expect(@car.make).to eq(“Honda”) ✤ expect(@car.make).to match(/H.n.a/) ✤ expect(@visible).to be(true) ✤ expect(@numbers).to match_array([4,5,2,8]) ✤ expect(alphabet).to start_with("a") ✤ expect(alphabet).to end_with(“z") ✤ expect(alphabet).to start_with("a").and end_with(“z") ✤ expect(stoplight.color).to eq("red").or eq("green").or eq(“yellow") ✤ http://rspec.info/blog/2014/01/new-in-rspec-3-composable-matchers/ ✤ RSpec Cheatsheet: http://www.anchor.com.au/wp-content/uploads/rspec_cheatsheet_attributed.pdf
  • 6. RSpec with Rails - 1 Gemfile > group :development, :test do gem 'rspec-rails', '~> 3.0' gem 'factory_girl_rails' end group :test do gem 'faker' gem 'capybara' gem 'guard-rspec' gem 'selenium-webdriver', '~> 2.47.1' end $ bundle install
  • 7. RSpec with Rails - 2 $ rails g rspec:install Delete > test folder spec/rails_helper.rb > require 'capybara/rspec' Install spec files for existing controllers and models > $ rails g rspec:controller StaticPagesController $ rails g rspec:model Course (RSpec generates spec files automatically upon rails g controller/model)
  • 8. RSpec with Rails - Controller Test # controllers/selfies_controller.rb require 'rails_helper' RSpec.describe StaticPagesController, :type => :controller do describe "GET index" do it "renders the index template" do get :index expect(response).to render_template("index") end end describe "GET about" do it "renders the about page" do get :about expect(response).to render_template("about") end end end Controller Test
  • 9. RSpec with Rails - Model Test # spec/models/selfy_spec.rb require 'rails_helper' RSpec.describe Selfy, :type => :model do describe "selfy" do selfy = FactoryGirl.build(:selfy) it "has a valid factory title" do expect(selfy.title).to eq("This is test selfy title") end it "has a valid factory description" do expect(selfy.description).to eq("This is test selfy description") end end Model Test
  • 10. RSpec with Rails Feature Test with Capybara -1 # spec/features/user_visits_homepage_spec.rb require "rails_helper" RSpec.feature "User visits homepage", :type => :feature do scenario "successfully", :js => true do visit root_path expect(page).to have_text("Welcome to Selfie There") end end Capybara Selenium JS Web Driver Feature Test Capybara Cheatsheet: https://learn.thoughtbot.com/test-driven-rails-resources/capybara.pdf (OPTIONAL)
  • 11. RSpec with Rails Feature Test with Capybara -2 # spec/features/user_signs_in_spec.rb require "rails_helper" RSpec.feature "User signs in", :type => :feature do scenario "successfully", :js => true do user = FactoryGirl.build(:user) visit "/users/sign_in" fill_in "Email", :with => user.email fill_in "Password", :with => user.password click_button "Log in" expect(page).to have_text("Signed in successfully.") end end Feature Test
  • 12. Tips and tricks Know the difference between FactoryGirl.create() and FactoryGirl.build() If your test says something like email exists => $ rake db:test:prepare Know how fixtures work and maybe use database_cleaner gem to avoid pos Use guard gem to run automated testing => $ guard init and leave the tab open like you are running rails server
  • 13. some .rspec options .rspec > —color —format documentation