RSpec: Feature specs as checklist

34
FEATURE SPECS AS CHECKLIST EDWARD FOX 2014/11/24

Transcript of RSpec: Feature specs as checklist

FEATURE SPECS AS CHECKLISTEDWARD FOX

2014/11/24

TABLE OF CONTENTS:selfWhy feature test?Tests as checklistExample

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

Usagee IncRepro.io

THE GREAT WALL OF RAILS

TEST

TESTING WAS SO HARD...

WHY?

MODEL TESTSCONTROLLER TESTS

VIEW TESTSAUTHORIZATION TESTS

AUTHENTICATION TESTSFUNCTIONAL TESTSINTEGRATION TESTS

BOUNDRY VALUE TESTSSUCCESS TESTSFAILURE TESTS

...

HARDEST PART

RSPEC - FEATURE SPECS

WHY?

1. DON'T KNOW WHAT TO TEST

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

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

IT'S INTEGRAL TEST,

BUT SOME TESTS HAVE FOCUS POINTS

2. 仕様書 BE LIKE...

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

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

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

SO,

TEST BECOMES...

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

NOT DRY,

DOES NOT TELL WHAT MATTERS.

BUT, WAIT.

IF WE HAVE 仕様書 LIKE THAT,

WRITE TESTS LIKE 仕様書?

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

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

NOW FEATURE SPECS ARE:DRYDeclaritiveFocus on what matters

FEATURE SPEC IS DIFFICULT,

SO PUSH AWAY THE COMPLICATED STUFF

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

THANKS!

Slide URL:

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