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

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

70e13d9877054026fda46d5a5b53a236?s=128

MOROHASHI Kyosuke

December 16, 2012
Tweet

Transcript

  1. 3.
  2. 4.
  3. 5.
  4. 14.

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

    p myobj.do_something pp myobj end ςετ࢖Θͣʹ࣮ߦ͢Δ
  5. 15.

    ಈ͔͢ % irb -r ./my_class > my_obj = MyClass.new >

    myobj.do_something > myobj ର࿩࣮ߦ؀ڥ΋͋Δ
  6. 17.

    ಈ͔͢ 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. 18.

    ಈ͔͢ $ 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) ର࿩࣮ߦ؀ڥͰ΋ Ͱ͖Δͱ͍͑͹Ͱ͖Δ
  8. 20.

    ಈ͔͢ 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. 21.

    ಈ͔͢ 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 ݁ՌΛ໨ࢹ
  10. 22.

    ಈ͔͢ 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 ݁Ռ͕࣮ߦॱʹґଘ ΠνΠνߟ͑ͳ͍ͱ͍͚ͳ͍
  11. 23.

    ಈ͔͢ 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 ݁Ռ͕࣮ߦॱʹґଘ σʔλΛมߋͨ͠Γ͢ΔͱπϥΠ ༨ܭͩͱࢥͬͨ σʔλΛফͨ͠Β ผͷςετͰ ࢖͓ͬͯͨ...
  12. 26.

    ಈ͔͢ ςετͱ࣮ͯ͠ߦ͢Δ 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
  13. 28.

    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
  14. 30.

    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
  15. 32.

    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
  16. 34.

    ͜ ͜ · Ͱ ͷ · ͱ Ί ‣ ͪΐͬͱେ͖͍ίʔυΛɺ

    ςετ࢖Θͣʹಈ͔͢ͱେม ‣ ςετΛ࢖͏ͱɺॻ͍ͨίʔυ Λ͙͢ʹɺ҆৺ͯ͠ಈ͔ͤΔ
  17. 40.
  18. 51.
  19. 52.
  20. 55.

    ͜ ͜ · Ͱ ͷ · ͱ Ί ‣ ࢲͨͪ͸ߟ͍͑ͯΔ

    ‣ ʮͳʹΛʯ͢Δ͔ͱʮͲ͏ʯ ࡞Δ͔Λ෼͚ͯߟ͑Δ ‣ ʮͳʹΛʯ͢Δ͔Λςετͱ ͯ͠ॻ͘
  21. 57.

    wΑ͋͘ΔݕࡧϑΥʔϜ w໊લͷ෦෼Ұக w೥ྸͰͷൺֱ wझຯͰͷݕࡧ Person - name (str) - age

    (int) Hobby - name (str) Interest - person_id(fk) - hobby_id(fk) 1 * * 1
  22. 59.

    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
  23. 60.

    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
  24. 62.

    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 ͏ʔΜɺ໊લ͕௕͗͢ΔɺΑ͏ͳ...
  25. 64.

    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 Αͦ͞͏!
  26. 66.

    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
  27. 68.

    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
  28. 70.

    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 ͜ͷςετ͕௨ͬͨΒҰ൪؆୯ͳਖ਼ৗܥ͸Ͱ͖ͨ
  29. 71.

    ͜ ͜ · Ͱ ͷ · ͱ Ί ‣ ߟ͑ͨ͜ͱΛςετʹॻ͘

    ‣ ςετʹॻ͔ΕͨίʔυΛ ݟͯ೰ΜͩΓۛຯͨ͠Γ͢Δ ‣ ςετ͕௨ΔΑ͏ʹίʔυΛॻ͘
  30. 72.
  31. 74.

    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
  32. 76.

    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
  33. 77.

    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
  34. 78.

    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 ͜Ε͕࣍ͷΰʔϧ
  35. 80.

    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; ΰʔϧͩͱࢥ͏ͱ͜Ζ·Ͱॻ͘ͱΑ͍
  36. 81.

    ͜ ͜ · Ͱ ͷ · ͱ Ί ‣ ೉͍͠໰୊΋ɺ·ͣʮͳʹΛʯͯ͠

    ཉ͍͔͠Λߟ͑Δ ‣ ςετΛ࢖ͬͯɺΰʔϧΛઃఆ͢Δ
  37. 83.

    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ͭͷ৚݅Λ૊Έ߹Θͤ͸ɺ ͜Μͳ;͏ʹಈ͍ͯʮ΄͍͠ʯ
  38. 84.

    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 }
  39. 86.

    ͜ ͜ · Ͱ ͷ · ͱ Ί ‣ ࿅Δͱ͖͸ɺ࢖ΘΕํ΋

    ߟ͑Δ Ϣʔβͱ͔ϑϨʔϜϫʔΫͱ͔ ‣ ςετͰͷ"DUͱɺϓϩ μΫτίʔυ͸Α͘ࣅΔ
  40. 92.
  41. 93.
  42. 94.
  43. 99.

    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.