You are on page 1of 27

Behavior Driven Cocoa

BDD with Cedar, OCHamcrest & OCMock

Friday, October 15, 2010

Behavior Driven Development



Seeks a common language for Business and Technology Getting the words right Seeks identiable, veriable 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

Its All Behavior


Behavior Driven Development (BDD) strives to describe what a system should do, as apposed to testing what the system does. Its all about Getting the words right.

Friday, October 15, 2010

Getting the Words Right

BDD strives to avoid the traditional disconnect between Business people and Technology people through use of a common vocabulary for describing the system.

Friday, October 15, 2010

Programmers...

Why do I need to write tests? Thats what testers do. Writing all these tests slows me down! Its a waste of my time. Im a good programmer! I dont need to write tests to prove my code works.

Friday, October 15, 2010

Testers...

Why are programmers writing tests? We all know 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

Write Specs Not Tests


Simply eliminating the word test from the vocabulary can actually relieve much of the tension between Business, Development and Quality Assurance teams.

Friday, October 15, 2010

Specs! Not Functional Specication Documents.


Theres nothing functional about a functional specication! Specs in this context are runnable, veriable computer code that describes how the application should behave.

Friday, October 15, 2010

Wheres the Business Value?


Every feature of a system should be there because it adds actual business value to the product being built. So dene that value in each spec used to describe the behavior of each feature?

Friday, October 15, 2010

The User Story


Feature: Addition In order to avoid silly mistakes As a math idiot I want to be told the sum of two numbers Scenario: Add two numbers Given I have entered 50 into the calculator And I have entered 70 into the calculator When I press add Then the result should be 120 on the screen

Friday, October 15, 2010

Enough is Enough
Traditional large up-front design requires asking the most important questions at a time when the team has the least amount of knowledge. This often requires making wild guesses about the design, and scope, of a system before anyone even understands the product being built.

Friday, October 15, 2010

Embrace Change
BDD/TDD strives to describe just enough of 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 planning and development process.

Friday, October 15, 2010

Cedar
BDD Framework by Pivotal Labs
http://github.com/pivotal/cedar

Friday, October 15, 2010

Describe It!
SPEC_BEGIN(FooSpec) describe(@"Foo", ^{ beforeEach(^{ ... }); it(@"should be awesome", ^{ ... }); }); SPEC_END

Friday, October 15, 2010

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

OCHamcrest
Matcher Library for Objective-C
http://code.google.com/p/hamcrest/wiki/TutorialObjectiveC

Friday, October 15, 2010

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

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

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

OCMock
Sometimes You Just Need to Fake It!
http://www.mulle-kybernetik.com/software/OCMock/

Friday, October 15, 2010

Faking a Web Service Call


it(@"should authenticate", ^{ gr = [[GReader alloc] init];
// Setup a mock to fake, and expect, the post message

mock = [OCMockObject partialMockForObject:gr]; [[[mock expect] andReturn:@"I'm a fake response"] post:[OCMArg any]];
// Send a message to the real GRReader instance

[gr authenticateWithUsername:@"robert" password:@"password"]; [mock verify]; });

Friday, October 15, 2010

Use It with Anything

Like OCHamcrest, OCMock can be used with any test framework you like.

Friday, October 15, 2010

Demos...

Friday, October 15, 2010

One more thing...

Friday, October 15, 2010

BDD Your JavaScript


With Jasmine (also from Pivotal Labs)
http://github.com/pivotal/jasmine

Friday, October 15, 2010

A Jasmine Spec...
describe("Player", function() { !!var player; !!var song; !!beforeEach(function() { !!!!player = new Player(); !!!!song = new Song(); !!}); !!it("should be able to play a Song", function() { !!!!player.play(song); !!!!expect(player.currentlyPlayingSong).toEqual(song); !!}); });

Friday, October 15, 2010

This work is licensed under the Creative Commons Attribution 3.0 United States License. To view a 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.

Friday, October 15, 2010

You might also like