rspec presentation
TRANSCRIPT
10/1/2015
Getting Started withRSpec…Capybara and
whatnotLightning 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 RSpechttps://twitter.com/dhh/status/472072157821169664http://brandonhilkert.com/blog/7-reasons-why-im-sticking-with-minitest-and-fixtures-in-rails/
Basic Structure (Syntax)Given some context -> When some event occurs -> Then I expect some outcome
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 endend
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 - 1Gemfile >
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.rbrequire '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 endend
Controller Test
RSpec with Rails - Model Test# spec/models/selfy_spec.rbrequire '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 endend
Model Test
RSpec with RailsFeature Test with Capybara -1# spec/features/user_visits_homepage_spec.rbrequire "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") endend
Capybara
Selenium JS Web Driver
Feature Test
Capybara Cheatsheet: https://learn.thoughtbot.com/test-driven-rails-resources/capybara.pdf
(OPTIONAL)
RSpec with RailsFeature Test with Capybara -2
# spec/features/user_signs_in_spec.rbrequire "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.") endend
Feature Test
Tips and tricksKnow 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 possible messy situations
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