RubyConfUA - Антипаттерны модульного тестирования Митин Павел

Embed Size (px)

Citation preview

  • 8/7/2019 RubyConfUA -

    1/53

    , RubyConfUa 2010

  • 8/7/2019 RubyConfUA -

    2/53

    Ruby on Rails 4 test-driven development

    http://novembermeeting.blogspot.com

  • 8/7/2019 RubyConfUA -

    3/53

    Indented test code vs

    describePopularityCalculator, '#popular?'do it'should take into account \

    the comment count'do

    subject.popular?(post).should be_false

    end

    # ...

    end

  • 8/7/2019 RubyConfUA -

    4/53

    Indented test code vs

    classPopularityCalculator def popular?(post)

    end

    end

  • 8/7/2019 RubyConfUA -

    5/53

    Indented test code vs

    it'should take into account the comment count'doposts = (0..19).map do |n|

    post_with_comment_count n

    end

    posts.each do |post|

    if 10 < post.comment_count

    subject.popular?(post).should be_true

    else

    subject.popular?(post).should be_false end

    end

    end

  • 8/7/2019 RubyConfUA -

    6/53

    Indented test code vs

    classPopularityCalculatorTHRESHOLD = 10

    def popular?(post)

    THRESHOLD < post.comment_count

    end

    end

  • 8/7/2019 RubyConfUA -

    7/53

    Indented test code vs

    it'should take into account the comment count'doposts = (0..19).map do |n|

    post_with_comment_count n

    end

    posts.each do |post|

    if 10 < post.comment_count

    subject.popular?(post).should be_true

    else

    subject.popular?(post).should be_false end

    end

    end

  • 8/7/2019 RubyConfUA -

    8/53

    Indented test code vs

    :

    (, ..)

  • 8/7/2019 RubyConfUA -

    9/53

    Indented test code vs

    :

    ... ,

  • 8/7/2019 RubyConfUA -

    10/53

    Indented test code vs

    it"should return true if the comment count /is more then the popularity threshold"do

    post = post_with_comment_count THRESHOLD + 1

    subject.popular?(post).should be_true

    post = post_with_comment_count THRESHOLD + 100

    subject.popular?(post).should be_true

    end

  • 8/7/2019 RubyConfUA -

    11/53

    Indented test code vs

    :

  • 8/7/2019 RubyConfUA -

    12/53

    Production Logic in Test

    classPopularityCalculatorTHRESHOLD = 10

    def popular?(post)

    THRESHOLD < post.comment_count

    end

    end

  • 8/7/2019 RubyConfUA -

    13/53

    Production Logic in Test

    it"should take into account the comment count"dopost = post_with_comment_count 11

    expected = THRESHOLD < post.comment_count

    actual = subject.popular? post

    actual.should == expected

    end

  • 8/7/2019 RubyConfUA -

    14/53

    Production Logic in Test

    it"should take into account the comment count"dopost = post_with_comment_count 11

    expected = THRESHOLD < post.comment_count

    actual = subject.popular? post

    actual.should == expected

    end

  • 8/7/2019 RubyConfUA -

    15/53

    Production Logic in Test

    :

  • 8/7/2019 RubyConfUA -

    16/53

    Production Logic in Test

  • 8/7/2019 RubyConfUA -

    17/53

    Production Logic in Test

    it"should take into account the comment count"doactual = subject.popular? post_with_comment_count(11)

    actual.should be_true

    end

  • 8/7/2019 RubyConfUA -

    18/53

    Production Logic in Test

    : , :)

  • 8/7/2019 RubyConfUA -

    19/53

    Too Many Expectations

    describeNotificationService, "#notify_about"doit"should notify the post author by email"do

    notification_service.notify_about @comment

    end

    it"should notify the post author by sms"

    end

  • 8/7/2019 RubyConfUA -

    20/53

    Too Many Expectations

    classNotificationService