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

devlove2012-let-yor-test-drive-dev-more

 devlove2012-let-yor-test-drive-dev-more

MOROHASHI Kyosuke

December 16, 2012
Tweet

More Decks by MOROHASHI Kyosuke

Other Decks in Programming

Transcript

  1. ಈ͔͢ if __FILE__ == $0 require 'pp' myobj = MyClass.new(:arg)

    p myobj.do_something pp myobj end ςετ࢖Θͣʹ࣮ߦ͢Δ
  2. ಈ͔͢ % irb -r ./my_class > my_obj = MyClass.new >

    myobj.do_something > myobj ର࿩࣮ߦ؀ڥ΋͋Δ
  3. ಈ͔͢ if __FILE__ == $0 require 'pp' alice = Person.create(name:

    'alice', age: 24) alisia = Person.create(name: 'alisia', age: 30) bob = Person.create(name: 'bob', age: 18) chris = Person.create(name: 'chris', age: 42) pp Person.name_like('ali') pp Person.younger_than(24) end ΋͏ͪΐͬͱෳࡶͳྫ
  4. ಈ͔͢ $ rails console > alice = Person.create(name: 'alice') >

    alissa = Person.create(name: 'alissa') > bob = Person.create(name: 'bob') > chris = Person.create(name: 'chris') > > Person.name_like('ali') > Person.younger_than(24) ର࿩࣮ߦ؀ڥͰ΋ Ͱ͖Δͱ͍͑͹Ͱ͖Δ
  5. ಈ͔͢ if __FILE__ == $0 require 'pp' alice = Person.create(name:

    'alice', age: 24) alisia = Person.create(name: 'alisia', age: 30) bob = Person.create(name: 'bob', age: 18) chris = Person.create(name: 'chris', age: 42) pp Person.name_like('ali') pp Person.younger_than(24) end σʔλͳͲͷ४උ લͷσʔλ͕࢒ͬͯͳ͍͔΋֬ೝ͠ͳ͍ͱΠΧϯ
  6. ಈ͔͢ if __FILE__ == $0 require 'pp' alice = Person.create(name:

    'alice', age: 24) alisia = Person.create(name: 'alisia', age: 30) bob = Person.create(name: 'bob', age: 18) chris = Person.create(name: 'chris', age: 42) pp Person.name_like('ali') pp Person.younger_than(24) end ݁ՌΛ໨ࢹ
  7. ಈ͔͢ if __FILE__ == $0 require 'pp' alice = Person.create(name:

    'alice', age: 24) alisia = Person.create(name: 'alisia', age: 30) bob = Person.create(name: 'bob', age: 18) chris = Person.create(name: 'chris', age: 42) pp Person.name_like('ali') pp Person.younger_than(24) end ݁Ռ͕࣮ߦॱʹґଘ ΠνΠνߟ͑ͳ͍ͱ͍͚ͳ͍
  8. ಈ͔͢ if __FILE__ == $0 require 'pp' alice = Person.create(name:

    'alice', age: 24) alisia = Person.create(name: 'alisia', age: 30) bob = Person.create(name: 'bob', age: 18) chris = Person.create(name: 'chris', age: 42) pp Person.name_like('ali') pp Person.younger_than(24) end ݁Ռ͕࣮ߦॱʹґଘ σʔλΛมߋͨ͠Γ͢ΔͱπϥΠ ༨ܭͩͱࢥͬͨ σʔλΛফͨ͠Β ผͷςετͰ ࢖͓ͬͯͨ...
  9. ಈ͔͢ ςετͱ࣮ͯ͠ߦ͢Δ describe Person do context '4 people with below'

    do let!(:alice) { Person.create(name: 'alice', age: 24) } let!(:alissa){ Person.create(name: 'alissa', age: 30) } let!(:bob) { Person.create(name: 'bob', age: 18) } let!(:chris) { Person.create(name: 'chris', age: 42) } describe '.name_like' do subject { Person.name_like('ali').map(&:name) } it { should =~ %w[alice alissa] } end describe '.younger_than' do subject { Person.younger_than(24).map(&:name) } it { should =~ %w[bob] } end end end
  10. ribe Person do ntext '4 people with below' do let!(:alice)

    { Person.create(name: 'alice', age: let!(:alissa){ Person.create(name: 'alissa', age: let!(:bob) { Person.create(name: 'bob', age: 18 let!(:chris) { Person.create(name: 'chris', age: describe '.name_like' do subject { Person.name_like('ali').map(&:name) } it { should =~ %w[alice alissa] } end describe '.younger_than' do subject { Person.younger_than(24).map(&:name) } it { should =~ %w[bob] } end
  11. ibe Person do text '4 people with below' do et!(:alice)

    { Person.create(name: 'alice', age: et!(:alissa){ Person.create(name: 'alissa', age: et!(:bob) { Person.create(name: 'bob', age: 18 et!(:chris) { Person.create(name: 'chris', age: escribe '.name_like' do subject { Person.name_like('ali').map(&:name) } it { should =~ %w[alice alissa] } nd escribe '.younger_than' do subject { Person.younger_than(24).map(&:name) } it { should =~ %w[bob] } nd
  12. let!(:alissa){ Person.create(name: 'alissa', age let!(:bob) { Person.create(name: 'bob', age: 1

    let!(:chris) { Person.create(name: 'chris', age: describe '.name_like' do subject { Person.name_like('ali').map(&:name) it { should =~ %w[alice alissa] } end describe '.younger_than' do subject { Person.younger_than(24).map(&:name) it { should =~ %w[bob] } end d
  13. ͜ ͜ · Ͱ ͷ · ͱ Ί ‣ ͪΐͬͱେ͖͍ίʔυΛɺ

    ςετ࢖Θͣʹಈ͔͢ͱେม ‣ ςετΛ࢖͏ͱɺॻ͍ͨίʔυ Λ͙͢ʹɺ҆৺ͯ͠ಈ͔ͤΔ
  14. ͜ ͜ · Ͱ ͷ · ͱ Ί ‣ ࢲͨͪ͸ߟ͍͑ͯΔ

    ‣ ʮͳʹΛʯ͢Δ͔ͱʮͲ͏ʯ ࡞Δ͔Λ෼͚ͯߟ͑Δ ‣ ʮͳʹΛʯ͢Δ͔Λςετͱ ͯ͠ॻ͘
  15. wΑ͋͘ΔݕࡧϑΥʔϜ w໊લͷ෦෼Ұக w೥ྸͰͷൺֱ wझຯͰͷݕࡧ Person - name (str) - age

    (int) Hobby - name (str) Interest - person_id(fk) - hobby_id(fk) 1 * * 1
  16. describe Person do context '4 people with below' do let!(:alice)

    { Person.create(name: 'alice', age: 24) let!(:alissa){ Person.create(name: 'alissa', age: 30) let!(:bob) { Person.create(name: 'bob', age: 18) } let!(:chris) { Person.create(name: 'chris', age: 42) describe '.name_like' do subject { Person.name_like('ali').map(&:name) } it { should =~ %w[alice alissa] } end describe '.younger_than' do subject { Person.younger_than(24).map(&:name) } it { should =~ %w[bob] } end end end
  17. class Person < ActiveRecord::Base scope :name_like, ->(name) { where("#{table_name}.name LIKE

    ?", "#{name} } scope :younger_than, ->(age) { where("#{table_name}.age < ?", age.to_i) } end
  18. ribe Person do ntext '4 people with below' do #

    snip describe '.has_at_least_one_hobby(hobbies)' do let(:hobbies) { %w[programming BBQ travel] } subject(:people) do Person.has_at_least_one_hobby(hobbies) end end d ͏ʔΜɺ໊લ͕௕͗͢ΔɺΑ͏ͳ...
  19. describe Person do context '4 people with below' do #

    snip describe '.interest_in_any(hobbies)' do let(:hobbies) { %w[programming BBQ travel subject(:people) do Person.interest_in_any(hobbies) end end end end Αͦ͞͏!
  20. describe '.interest_in_any(hobbies)' do let(:hobbies) { %w[programming BBQ travel] } before

    do hobbies.each do |hobby_name| Hobby.create!(name: hobby_name) end Hobby.create!(name: 'baseball') alice.hobbies << Hobby.where(name: 'travel'). bob.hobbies << Hobby.where(name: 'programmi chris.hobbies << Hobby.where(name: 'baseball' end subject(:people) { Person.interest_in_any(hobbi end
  21. Hobby.create!(name: hobby_name) end Hobby.create!(name: 'baseball') alice.hobbies << Hobby.where(name: 'travel') bob.hobbies

    << Hobby.where(name: 'programm chris.hobbies << Hobby.where(name: 'baseball end subject(:people) { Person.interest_in_any(hobb it { should =~ [alice, bob] } end
  22. describe '.interest_in_any(hobbies)' do let(:hobbies) { %w[programming BBQ travel] } before

    do hobbies.each do |hobby_name| Hobby.create!(name: hobby_name) end Hobby.create!(name: 'baseball') alice.hobbies << Hobby.where(name: 'travel').first bob.hobbies << Hobby.where(name: 'programming').first chris.hobbies << Hobby.where(name: 'baseball').first end subject(:people) { Person.interest_in_any(hobbies) } it { should =~ [alice, bob] } end ͜ͷςετ͕௨ͬͨΒҰ൪؆୯ͳਖ਼ৗܥ͸Ͱ͖ͨ
  23. ͜ ͜ · Ͱ ͷ · ͱ Ί ‣ ߟ͑ͨ͜ͱΛςετʹॻ͘

    ‣ ςετʹॻ͔ΕͨίʔυΛ ݟͯ೰ΜͩΓۛຯͨ͠Γ͢Δ ‣ ςετ͕௨ΔΑ͏ʹίʔυΛॻ͘
  24. require 'spec_helper' describe PeopleQuery do include_context \ '4 people: alice(24),

    aliass(30), bob(18), chris(42)' describe '.new(name_like: "ali", younger_than: 25)' do let(:query) { PeopleQuery.new(name_like: 'ali', younger_than: 25) } subject { query.people } it { should == [alice] } end end
  25. require 'spec_helper' describe PeopleQuery do include_context \ '4 people: alice(24),

    aliass(30), bob(18), describe '.new(name_like: "ali", younger_than let(:query) { PeopleQuery.new(name_like: 'ali', younger_than: 25) } subject { query.people } it { should == [alice] } end end Act
  26. require 'spec_helper' describe PeopleQuery do include_context \ '4 people: alice(24),

    aliass(30), bob(18), describe '.new(name_like: "ali", younger_than let(:query) { PeopleQuery.new(name_like: 'ali', younger_than: 25) } subject { query.people } it { should == [alice] } end end Arrange
  27. describe PeopleQuery do include_context \ '4 people: alice(24), aliass(30), bob(18),

    describe '.new(name_like: "ali", younger_than let(:query) { PeopleQuery.new(name_like: 'ali', younger_than: 25) } subject { query.people } it { should == [alice] } end end Assert ͜Ε͕࣍ͷΰʔϧ
  28. require 'spec_helper' describe PeopleQuery do include_context \ '4 people: alice(24),

    aliass(30), bob(18), chris(42)' describe '.new(name_like: "ali", younger_than: 25)' do let(:query) { PeopleQuery.new(name_like: 'ali', younger_than: 25) } subject { query.people } it { should == [alice] } end end FAQ: ςετ͸ ͲΕ͘Β͍ॻ͘΂͖? A; ΰʔϧͩͱࢥ͏ͱ͜Ζ·Ͱॻ͘ͱΑ͍
  29. ͜ ͜ · Ͱ ͷ · ͱ Ί ‣ ೉͍͠໰୊΋ɺ·ͣʮͳʹΛʯͯ͠

    ཉ͍͔͠Λߟ͑Δ ‣ ςετΛ࢖ͬͯɺΰʔϧΛઃఆ͢Δ
  30. describe '.new(name_like: "ali", hobbies: "baseball")' d include_context \ 'there are

    4 hobbies: programming, travel, BBQ and b let(:query) { PeopleQuery.new(name_like: 'ali', hobbies: 'baseball travel') } before do alice.hobbies << Hobby.where(name: 'BBQ') alissa.hobbies << Hobby.where(name: 'baseball') end subject { query.people } it { should == [alissa] } end 3ͭͷ৚݅Λ૊Έ߹Θͤ͸ɺ ͜Μͳ;͏ʹಈ͍ͯʮ΄͍͠ʯ
  31. cribe '.new(name_like: "ali", hobbies: seball")' do nclude_context \ 'there are

    4 hobbies: programming, travel, BBQ baseball' et(:query) { PeopleQuery.new(name_like: 'ali', hobbies: 'baseball travel') efore do alice.hobbies << Hobby.where(name: 'BBQ') alissa.hobbies << Hobby.where(name: 'baseball') nd ubject { query.people }
  32. ͜ ͜ · Ͱ ͷ · ͱ Ί ‣ ࿅Δͱ͖͸ɺ࢖ΘΕํ΋

    ߟ͑Δ Ϣʔβͱ͔ϑϨʔϜϫʔΫͱ͔ ‣ ςετͰͷ"DUͱɺϓϩ μΫτίʔυ͸Α͘ࣅΔ
  33. Thank you for listening. - May it be a light

    for you in dark places, when all other lights go out. - May it be a light for you in dark places, when all other lights go out.