Upgrade to Pro — share decks privately, control downloads, hide ads and more …

tdd on rails

kotohata
December 20, 2012

tdd on rails

rspec,cucumberで今日から始めるTDD生活。

kotohata

December 20, 2012
Tweet

More Decks by kotohata

Other Decks in Technology

Transcript

  1. ࣗݾ঺հ • twitter @tii_kot • blog http://tkot.hatenablog.com/ • github https://github.com/mhersmh

    • Railsྺ1.5೥ • ࠷ۙ͸Node.jsͱ͔Erlangʹ஫໨ͯ͠·͢ 12೥12݄25೔Ր༵೔
  2. RailsͰΑ͘࢖ΘΕΔςετϑϨʔϜϫʔΫ • Test::Unit Rubyඪ४ϥΠϒϥϦɻ • shoulda Test::UnitΛRSpec෩ʹ֦ுͯ͘͠ΕΔɻ • RSpec Ұ൪ਓؾɻ

    • Cucumber ࣗવݴޠͰγφϦΦΛॻ͍ͯɺରԠ͢ΔϢʔβ΍ΞϓϦͷಈ͖Λ RubyͷίʔυͰॻ͘ड͚ೖΕςετ DHH͸Test::Unit೿ɻͦΕͧΕಘҙෆಘҙ͕͋ΔͷͰϓϩ δΣΫτ͝ͱʹϕετͳ΋ͷΛબ΂͹Α͍͕ɺ໎ͬͨΒ RSpecΛ࢖͑͹ؒҧ͍ͳ͍ 12೥12݄25೔Ր༵೔
  3. Α͋͘Δޡղ • ΞδϟΠϧ։ൃ͸ઃܭΛݫ֨ʹఆٛͤͣʹͱΓ͋͑ͣίʔυΛॻ͍ͯΈΔख ๏ͩͳΜ͍ͯ͏෩ʹࢥͬͯΔ͔΋͠Ε·ͤΜ͕ͦΕ͸શ͘ޡղͰ͢ • Rails͸࠷ॳͷϦιʔεઃܭͰίέΔͱઌ΄Ͳͷ։ൃϑϩʔνϟʔτΛ͢΂ͯ ΍Γ௚͢͜ͱʹͳΔʂ ҰͭͷΠςϨʔγϣϯʹ͓͚Δཁ݅ఆٛͱઃܭɺը໘ ભҠͷΠϝʔδ֬ೝͳͲ͸े෼࣌ؒΛ͔͚Δ΂͖ •

    υΩϡϝϯτΛॻ͔ͳ͍ͳΜ͍ͯ͏ͷ΋શ͘ޡΓɻυΩϡϝϯτओମʹͳΒ ͳ͍ͱ͍͏͚ͩͰɺઃܭʹ͍ͭͯ࿩͋ͬͨ͠Γকདྷతʹ௥Ճ͞Εͦ͏ͳػೳ ͸ੵۃతʹϝϞΛ࢒͢΂͖ͩ͠ɺͳΔ΂͘υΩϡϝϯτ΋ಉҰͷόʔδϣϯ ؅ཧԼʹஔ͍ͨ΄͏͕͍͍ɻ 12೥12݄25೔Ր༵೔
  4. Ϣχοτςετ(ଓ͖) • ϓϩάϥϜΛར༻͢Δ৔߹͸ɺ֤ΦϒδΣΫτʹϝοηʔδΛૹͬͯ΍Γऔ ΓΛ͢ΔϧʔςΟϯͷ΄͏ʹؔ৺͕͋Δ͔ΒɺΦϒδΣΫτ͕͢΂͖࢓ࣄΛ هड़͢ΔϢχοτςετ͕TDDॳ৺ऀʹ͸෼͔Γʹ͍͘ɻ ۩ମྫ class UsersController < ApplicationController

    def create @user = User.new(params[:user]) if @user.save redirect_to user_path(@user) else render :new end end end UsersController#createΛϧʔςΟϯ Ͱߟ͑Δͱɺ@user.saveͰDBʹอ ଘ͞ΕΔ͔Ͳ͏͔ʹؔ৺Λ࣋ͬͯ͠ ·͏͕ɺDBʹอଘ͞ΕΔ͔Ͳ͏͔ ͸Modelͷ࢓ࣄͰ͋Δ͔Βɺ Controller͸୯ʹ@user.save͕trueΛ ฦͨ͠Βuser_pathʹϦμΠϨΫτ ͠ɺͦ͏Ͱͳ͍ͳΒnewΛrender͢ Δ͜ͱʹ஫ྗ͢Ε͹Α͍ 12೥12݄25೔Ր༵೔
  5. cucumber(2) ̋features/users/registration.feature (ϑΟʔνϟʔఆٛ) ΋͠ ϝʔϧΞυϨε͸user@example.com,ύεϫʔυ͸testtest,Ϣʔβ໊͸user1Ͱ Ϣʔβొ࿥Λ͢Δ ̋features/step_definitions/user_steps.rb(εςοϓఆٛ) When /^ϝʔϧΞυϨε͸(.*),ύεϫʔυ͸(.*),Ϣʔβ໊͸(.*)ͰϢʔβొ࿥Λ͢Δ$/ do

    | email, password, name| ɹvisit new_user_registration_path ɹfill_in(“user_email”, with:email) ɹfill_in(“user_password”, with: password) ɹfill_in(“user_password_confirmation”, with: password) ɹfill_in(“user_display_name”, with:name) ɹclick_button(“Sign up”) end 12೥12݄25೔Ր༵೔
  6. webrat_ja_steps.rb #coding: utf-8 When /^”(.*)”ϘλϯΛΫϦοΫ͢Δ$/ do |button| click_button(button) end When

    /^”(.*)”ϦϯΫΛΫϦοΫ͢Δ$/ do |link| click_link(link) end When /࠶ಡΈࠐΈ͢Δ/ do visit request.request_uri end When /^”(.*)”ʹ”(.*)”ͱೖྗ͢Δ$/ do |field, value| fill_in(field, with: value) end ͳͲͳͲ... 12೥12݄25೔Ր༵೔
  7. Cucumber(4) γφϦΦ: ొ࿥͞Ε͍ͯͳ͍mailͱύεϫʔυͰ৽نొ࿥ ɹ΋͠ “Ϣʔβొ࿥ϖʔδ”΁ΞΫηε ɹ͔ͭ “user_email”ʹ”user1@example.com”ͱೖྗ͢Δ ɹ͔ͭ “user_password”ʹ”testtest”ͱೖྗ͢Δ ɹ͔ͭ

    “user_display_name”ʹ”user1”ͱೖྗ͢Δ ɹ͔ͭ “user_password_confirmation”ʹ”testtest”ͱೖྗ͢Δ ɹ͔ͭ “Sign up”ϘλϯΛΫϦοΫ͢Δ ɹͳΒ͹ “You have signed up successfully.”ͱදࣔ͞Ε͍ͯΔ͜ͱ ৑௕ͰΘ͔ΓͮΒ͍͠ɺ࠶ར༻ੑʹ๡͍͠ (3ͭલͷεϥΠυͱൺ΂ͯΈΑ͏) 12೥12݄25೔Ր༵೔
  8. RSpec(2) • RSpec͸Railsʹݶఆ͞Εͨ΋ͷͰ͸ͳ͍ɻRailsͰ࢖͏৔߹ҎԼͷΑ͏ͳςε τσΟϨΫτϦ͕ଘࡏɻ(1)spec/controllers (2)spec/models (3)spec/helpers (4)spec/views (5)spec/libs (6)spec/routing (7)spec/requests

    (8)spec/support • request͸webratͳͲΛ࢖༻ͨ͠ड͚ೖΕςετɻCucumberΛ࢖͏৔߹͸͔ ͿΔͷͰཁΒͳ͍ͱࢥ͏ɻ • routing͸config/routes.rbͱΠϝʔδ͍ͯ͠ΔURL͕߹க͍ͯ͠Δ͔Λςετ ͢Δ͚ͩͳͷͰΧόϨοδΛؾʹ͠ͳ͍ͳΒෆཁ͔΋ɻ • view΋සൟʹॻ͖׵ΘΔͱ͜ΖͳͷͰɺॻ͘ඞཁ͸ͳ͍͔΋͠Εͳ͍ɻ 12೥12݄25೔Ր༵೔
  9. RSpec(3) • ٯʹॻ͘΂͖ͳͷ͸spec/models, spec/controllers, spec/libs, spec/helpersɻ • ControllerͷspecͰ͸ɺ͋ΔϧʔςΟϯάʹHTTPϦΫΤετΛඈ͹ͨ࣌͠ʹ ૝ఆ͍ͯ͠ΔΦϒδΣΫτʹ૝ఆ͍ͯ͠ΔϝοηʔδΛૹ͍ͬͯΔ͔Λݕূ ʹϞσϧͷద੾ͳϝιουΛ(ద੾ͳҾ਺Ͱ)ݺͼग़͍ͯ͠Δ͔ɺresponseʹ

    flash͸ؚ·ΕΔ͔ɺ૝ఆ͍ͯ͠ΔURLʹϦμΠϨΫτ͍ͯ͠Δ͔ɺͳͲΛςε τ͢Δ • ModelͷspecͰ͸ɺఆٛͨ͠ϝιου͕ਖ਼͍͠ڍಈΛ͍ͯ͠Δ͔ɺ࣮ࡍʹ ϝοηʔδΛૹͬͯ໭Γ஋Λݕূ͢Δͷ͕ϝΠϯɻྫ֎ͷൃੜ΍ϑΝΠϧ͕ ࡞੒͞ΕΔ͔ͳͲ΋ݕূͰ͖Δɻ 12೥12݄25೔Ր༵೔
  10. RSpec(4) describe UsersController do describe “POST create” do describe “with

    invalid params” do it “re-render the ‘new’ template” do User.any_instance.stub(:save).and_return(false) post :create, {user: {name: “test”}} response.should render_template(“new”) end end end end ※stub(:save).and_return(false) ࣮ࡍʹsave͞ΕΔ͔͸͜͜Ͱςετ ͠ͳ͍ɻِͷϝιουʹΑͬͯfalse Λฦͤ͞Δɻ ※should ςετͷ֩ͷ෦෼ɻ͜ΕΛ ΤΫεϖΫςʔγϣϯͱ͔Ξαʔ γϣϯͱݴ͏ɻ 12೥12݄25೔Ր༵೔