2010 10 Behavior Driven Cocoa

download 2010 10 Behavior Driven Cocoa

of 27

Transcript of 2010 10 Behavior Driven Cocoa

  • 7/27/2019 2010 10 Behavior Driven Cocoa

    1/27

    Behavior Driven CocoaBDD with Cedar, OCHamcrest & OCMock

    Friday, October 15, 2010

  • 7/27/2019 2010 10 Behavior Driven Cocoa

    2/27

    Behavior Driven Development

    Seeks a common language for Business andTechnology Getting the words right

    Seeks identifiable, verifiable business value Is the feature worth the effort?

    Seeks to reduce diminishing returns

    resulting from large up-front design Enough is enough

    Friday, October 15, 2010

  • 7/27/2019 2010 10 Behavior Driven Cocoa

    3/27

    Its All Behavior

    Behavior Driven Development (BDD) strives todescribe what a system should do, as

    apposed to testing what the system does.

    Its all about Getting the words right.

    Friday, October 15, 2010

  • 7/27/2019 2010 10 Behavior Driven Cocoa

    4/27

    Getting the Words Right

    BDD strives to avoid the traditionaldisconnect between Business people and

    Technology people through use of a common

    vocabulary for describing the system.

    Friday, October 15, 2010

  • 7/27/2019 2010 10 Behavior Driven Cocoa

    5/27

    Programmers...

    Why do I need to write tests? Thats what

    testers do.

    Writing all these tests slows me down! Its awaste of my time.

    Im a good programmer! I dont need to writetests to prove my code works.

    Friday, October 15, 2010

  • 7/27/2019 2010 10 Behavior Driven Cocoa

    6/27

    Testers...

    Why are programmers writing tests? We allknow they cant thats why you need testers!

    Are you trying to take away our jobs?

    You obviously dont understand testing or you

    wouldnt be asking programmers write tests!

    Friday, October 15, 2010

  • 7/27/2019 2010 10 Behavior Driven Cocoa

    7/27

    Write Specs Not Tests

    Simply eliminating the word testfrom the vocabulary can actually

    relieve much of the tension between

    Business, Development and Quality

    Assurance teams.

    Friday, October 15, 2010

  • 7/27/2019 2010 10 Behavior Driven Cocoa

    8/27

    Specs! Not Functional

    Specification Documents.

    Theres nothing functional about afunctional specification!

    Specs in this context are runnable,verifiable computer code that describes

    how the application should behave.

    Friday, October 15, 2010

  • 7/27/2019 2010 10 Behavior Driven Cocoa

    9/27

    Wheres the Business Value?

    Every feature of a system should be there

    because it adds actual business value tothe product being built.

    So define that value in each spec used to

    describe the behavior of each feature?

    Friday, October 15, 2010

  • 7/27/2019 2010 10 Behavior Driven Cocoa

    10/27

    The User Story

    Feature:Addition In orderto avoid silly mistakes

    As amath idiot I wantto be told the sum of two numbers

    Scenario:Add two numbers GivenI have entered 50 into the calculator

    AndI have entered 70 into the calculator WhenI press add Thenthe result should be 120 on the screen

    Friday, October 15, 2010

  • 7/27/2019 2010 10 Behavior Driven Cocoa

    11/27

    Enough is Enough

    Traditional large up-front design requires asking the

    most important questions at a time when theteam has the least amount of knowledge.

    This often requires making wild guesses about

    the design, and scope, of a system before anyoneeven understands the product being built.

    Friday, October 15, 2010

  • 7/27/2019 2010 10 Behavior Driven Cocoa

    12/27

    Embrace Change

    BDD/TDD strives to describe just enoughof the system to deliver something useful.

    We call this the Red, Green, Refactor cycle.

    Change is not only accepted, but is expected.

    It is integrated directly into an agile planningand development process.

    Friday, October 15, 2010

  • 7/27/2019 2010 10 Behavior Driven Cocoa

    13/27

    CedarBDD Framework by Pivotal Labs

    http://github.com/pivotal/cedar

    Friday, October 15, 2010

    http://github.com/pivotal/cedarhttp://github.com/pivotal/cedarhttp://github.com/pivotal/cedar
  • 7/27/2019 2010 10 Behavior Driven Cocoa

    14/27

    Describe It!

    SPEC_BEGIN(FooSpec)

    describe(@"Foo", ^{ beforeEach(^{ ... });

    it(@"should be awesome", ^{ ... });

    });

    SPEC_END

    Friday, October 15, 2010

  • 7/27/2019 2010 10 Behavior Driven Cocoa

    15/27

    It Should Behave Like

    SPEC_BEGIN(FooSpec)

    sharedExamplesFor(@"a similarly-behaving thing", ^(NSDictionary*context) { it(@"should do something common", ^{

    ... });});

    describe(@"Something that shares behavior", ^{ itShouldBehaveLike(@"a similarly-behaving thing");});

    describe(@"Something else that shares behavior", ^{ itShouldBehaveLike(@"a similarly-behaving thing");});

    SPEC_END

    Friday, October 15, 2010

  • 7/27/2019 2010 10 Behavior Driven Cocoa

    16/27

    OCHamcrestMatcher Library for Objective-C

    http://code.google.com/p/hamcrest/wiki/TutorialObjectiveC

    Friday, October 15, 2010

    http://code.google.com/p/hamcrest/wiki/TutorialObjectiveChttp://code.google.com/p/hamcrest/wiki/TutorialObjectiveChttp://code.google.com/p/hamcrest/wiki/TutorialObjectiveC
  • 7/27/2019 2010 10 Behavior Driven Cocoa

    17/27

    Assert That!

    NSString*myString = @"Foo";

    assertThat(myString, equalTo(@"Foo"));

    ...or with a little syntactic sugar...

    assertThat(myString, is(equalTo(@"Foo")));assertThat(myString, is(@"Foo"));

    Friday, October 15, 2010

  • 7/27/2019 2010 10 Behavior Driven Cocoa

    18/27

    Common Matchers

    Core: anything, describedAs, is

    Logical: allOf, anyOf, isNot

    Object: equalTo, hasDescription, instanceOf,

    isCompatableType, nilValue, notNilValue, sameInstance

    Collections: hasEntry, hasKey, hasValue, hasItem, hasItems

    Number: closeTo, greaterThan, greaterThanOrEqualTo, etc.

    Text: equalToIgnoringCase, equalToIgnoringWhiteSpace,

    containsString, startsWith, endsWith

    Friday, October 15, 2010

  • 7/27/2019 2010 10 Behavior Driven Cocoa

    19/27

    Use It with Anything

    The OCHamcrest matchers can be

    used with any test framework you

    wish to use. It does not require Cedar.

    Friday, October 15, 2010

  • 7/27/2019 2010 10 Behavior Driven Cocoa

    20/27

    OCMockSometimes You Just Need to Fake It!

    http://www.mulle-kybernetik.com/software/OCMock/

    Friday, October 15, 2010

    http://www.mulle-kybernetik.com/software/OCMock/http://www.mulle-kybernetik.com/software/OCMock/http://www.mulle-kybernetik.com/software/OCMock/
  • 7/27/2019 2010 10 Behavior Driven Cocoa

    21/27

    Faking a Web Service Call

    it(@"should authenticate", ^{ gr = [[GReaderalloc] init];

    // Setup a mock to fake, and expect, the post message mock = [OCMockObjectpartialMockForObject:gr]; [[[mock expect] andReturn:@"I'm a fake response"] post:[OCMArgany]];

    // Send a message to the real GRReader instance

    [gr authenticateWithUsername:@"robert" password:@"password"];

    [mock verify];});

    Friday, October 15, 2010

  • 7/27/2019 2010 10 Behavior Driven Cocoa

    22/27

    Use It with Anything

    Like OCHamcrest, OCMock can be

    used with any test framework you like.

    Friday, October 15, 2010

  • 7/27/2019 2010 10 Behavior Driven Cocoa

    23/27

    Demos...

    Friday, October 15, 2010

  • 7/27/2019 2010 10 Behavior Driven Cocoa

    24/27

    One more thing...

    Friday, October 15, 2010

  • 7/27/2019 2010 10 Behavior Driven Cocoa

    25/27

    BDD Your JavaScriptWith Jasmine (also from Pivotal Labs)

    http://github.com/pivotal/jasmine

    Friday, October 15, 2010

    http://github.com/pivotal/jasminehttp://github.com/pivotal/jasminehttp://github.com/pivotal/jasmine
  • 7/27/2019 2010 10 Behavior Driven Cocoa

    26/27

    A Jasmine Spec...

    describe("Player", function() { varplayer; varsong; beforeEach(function() { player =newPlayer(); song =newSong(); }); it("should be able to play a Song", function() { player.play(song); expect(player.currentlyPlayingSong).toEqual(song); });});

    Friday, October 15, 2010

  • 7/27/2019 2010 10 Behavior Driven Cocoa

    27/27

    This work is licensed under the Creative Commons Attribution 3.0 United States License. To viewa copy of this license, visit http://creativecommons.org/licenses/by/3.0/us/ or send a letter to

    Creative Commons, 171 Second Street, Suite 300, San Francisco, California, 94105, USA.

    http://creativecommons.org/licenses/by/3.0/us/http://creativecommons.org/licenses/by/3.0/us/http://creativecommons.org/licenses/by/3.0/us/http://creativecommons.org/licenses/by/3.0/us/http://creativecommons.org/licenses/by/3.0/us/http://creativecommons.org/licenses/by/3.0/us/http://creativecommons.org/licenses/by/3.0/us/http://creativecommons.org/licenses/by/3.0/us/http://creativecommons.org/licenses/by/3.0/us/