Top Banner
FEATURE SPECS AS CHECKLIST EDWARD FOX 2014/11/24
34
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: RSpec: Feature specs as checklist

FEATURE SPECS AS CHECKLISTEDWARD FOX

2014/11/24

Page 2: RSpec: Feature specs as checklist

TABLE OF CONTENTS:selfWhy feature test?Tests as checklistExample

Page 3: RSpec: Feature specs as checklist

SELFname == Edward Foxlanguage == Ruby, Railscarrier == 1 yearcurrent_work == new_work ==

Usagee IncRepro.io

Page 4: RSpec: Feature specs as checklist

THE GREAT WALL OF RAILS

Page 5: RSpec: Feature specs as checklist

TEST

Page 6: RSpec: Feature specs as checklist

TESTING WAS SO HARD...

Page 7: RSpec: Feature specs as checklist

WHY?

Page 8: RSpec: Feature specs as checklist

MODEL TESTSCONTROLLER TESTS

VIEW TESTSAUTHORIZATION TESTS

AUTHENTICATION TESTSFUNCTIONAL TESTSINTEGRATION TESTS

BOUNDRY VALUE TESTSSUCCESS TESTSFAILURE TESTS

...

Page 9: RSpec: Feature specs as checklist
Page 10: RSpec: Feature specs as checklist

HARDEST PART

Page 11: RSpec: Feature specs as checklist

RSPEC - FEATURE SPECS

Page 12: RSpec: Feature specs as checklist

WHY?

Page 13: RSpec: Feature specs as checklist

1. DON'T KNOW WHAT TO TEST

Page 14: RSpec: Feature specs as checklist

Function?E2E test?Authentication?View?Success?Failure?

Page 15: RSpec: Feature specs as checklist

scenario "User creates, edits and deletes an Invoice" do it "succeeds" do visit new_invoice_path fill_in "invoice_title", with: "Way of the Dragon" select("Today", from: "#invoice_date") fill_in "invoice_sender", with: "Chuck Norris" fill_in "invoice_receiver", with: "Bruce Lee" within(:css, ".invoice_publish_state") do choose("Draft") end find_button("Publish").click

find_link("Edit Invoice").click fill_in "invoice_title", with: "The Game of Death" fill_in "invoice_sender", with: "Kareem Abdul-Jabbar" find_button("Publish").click

find_link("Delete Invoice").click expect(find(".notice")).to have_content "Invoice Deleted!" endend

Page 16: RSpec: Feature specs as checklist

IT'S INTEGRAL TEST,

Page 17: RSpec: Feature specs as checklist

BUT SOME TESTS HAVE FOCUS POINTS

Page 18: RSpec: Feature specs as checklist

2. 仕様書 BE LIKE...

Page 19: RSpec: Feature specs as checklist

新規請求書作成画面においてタイトルを入力せずに保存すると

「タイトルは必ず入力してください」と表示され請求書が保存されないこと

新しい送付先を入力して保存すると「送付先を新規で追加しました」と「請求書を作成しました」と表示され新しい請求書が作成されること

Page 20: RSpec: Feature specs as checklist

SO,

Page 21: RSpec: Feature specs as checklist

TEST BECOMES...

Page 22: RSpec: Feature specs as checklist

scenario "user sign up" do context "when password is less than 8 characters" do it "displays error message as 'Password needs to be more than 8 characters'" do visit new_user_registrations_path fill_in "user_email", with: "[email protected]" fill_in "user_password", with: "suiken" find_button("Sign Up").click

expect(find(".error")).to have_content "Password needs to be more than 8 characters" end end

context "when password contains unusable character" do it "displays error message as 'Password contains unusable character'" do visit new_user_registrations_path fill_in "user_email", with: "[email protected]" fill_in "user_password", with: "香港功夫成龍酔拳" find_button("Sign Up").click

expect(find(".error")).to have_content "Password contains unusable character" end end

context "when email is already registered" do it "displays error message as 'The Email is already registered'" do visit new_user_registrations_path fill_in "user_email", with: "[email protected]" fill_in "user_password", with: "password" find_button("Sign Up").click

expect(find(".error")).to have_content "The Email is already registered" end end

Page 23: RSpec: Feature specs as checklist

NOT DRY,

Page 24: RSpec: Feature specs as checklist

DOES NOT TELL WHAT MATTERS.

Page 25: RSpec: Feature specs as checklist

BUT, WAIT.

Page 26: RSpec: Feature specs as checklist

IF WE HAVE 仕様書 LIKE THAT,

Page 27: RSpec: Feature specs as checklist

WRITE TESTS LIKE 仕様書?

Page 28: RSpec: Feature specs as checklist

SHARED_EXAMPLEspec/support/features/shared_example_helper.rb

shared_example_for "Displays notify message as" do |message| expect(page).to have_content messageend

&

HELPER METHODSspec/support/features/invoice_feature_spec_helper.rb

def create_invoice( title: nil, sender: nil, receiver: nil) visit new_invoice_path fill_in "invoice_title", with: title fill_in "invoice_sender", with: sender fill_in "invoice_receiver", with: reciverend

Page 29: RSpec: Feature specs as checklist

spec/features/invoice_spec.rbscenario "User creates an Invoice" do it "accepts invoice with new receiver as draft" do user_sign_up(email: [email protected], password: "password") create_invoice( title: "Snake in the eagle's shadow, sender: "Jackie Chan" )

it_behaves_like "Displays message as", message: "Successfully created an invoice." it_behaves_like "Displays notify message as", message: "Saved as draft since receiver was blank" endend

Page 30: RSpec: Feature specs as checklist

NOW FEATURE SPECS ARE:DRYDeclaritiveFocus on what matters

Page 31: RSpec: Feature specs as checklist

FEATURE SPEC IS DIFFICULT,

Page 32: RSpec: Feature specs as checklist

SO PUSH AWAY THE COMPLICATED STUFF

Page 33: RSpec: Feature specs as checklist

scenario "User performs create and delete of an Invoice" do it "Does not allow destroy to guests" do include_context "Sign in as guest" create_invoice( title: "Rush Hour", sender: "Jackie chan", receiver: "Chris Tucker" ) find_link("Delete 'Rush Hour'").click

it_behaves_like "Responds with status code", status: 403 it_behaves_like "Displays error message as", message: "Only admin roles can delete invoices" end

context "when user is signed in as admin" do it "Allows destroy to admins" do include_context "Sign in as admin" create_invoice( title: "Rush Hour", sender: "Jackie chan", receiver: "Chris Tucker" ) find_link("Delete 'Rush Hour'").click

it_behaves_like "Responds with status code", status: 200 it_behaves_like "Displays success message as", message: "Invoice was successfully deleted." end endend

Page 34: RSpec: Feature specs as checklist

THANKS!

Slide URL:

http://edwardkenfox.com/slides/feature-specs-as-check-list