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

NSBelfast: Kiwi and KIF

NSBelfast: Kiwi and KIF

Maurice Kelly presents two popular test frameworks that serve as alternatives to Apple's XCUnit. As part of his role in ShopKeep, Maurice uses the frameworks extensively and shows how they can enhance your code quality.

Maurice Kelly

December 15, 2014
Tweet

More Decks by Maurice Kelly

Other Decks in Programming

Transcript

  1. Why Use Unit Tests? • Test components in isolation •

    Detect regressions • Drive your Development
  2. BDD and Specs Given a system state When an action

    is taken Then a result occurs Given a system state When a second action is taken Then a second result occurs
  3. BDD and Specs Setup a system state Given the system

    When an action is taken Then a result occurs Given the same system When a second action is taken Then a second result occurs
  4. BDD and Specs Setup a system state Given the system

    When an action is taken Then a result occurs Given the same system When a second action is taken Then a second result occurs Given the same system With a subtle adjustment When a third action is taken Then a third result occurs
  5. BDD and Specs • context - defines the state of

    the system • describe - defines the actions being taken • it - defines the results that should occur
  6. BDD and Specs context - a system state describe -

    an action is taken it - causes a result to occur describe - a second action is taken it - causes a second result to occur context - the system is adjusted describe - a third action is taken it - causes a third result to occur
  7. context, describe and it blocks context(@"When the system has a

    state", ^{ /*your context*/ }); describe(@"an action is taken", ^{ /* your actions*/ }); it(@"causes a result to occur", ^{ /*your expectations */ });
  8. context, describe and it blocks context(@"When the system has a

    state", ^{ describe(@"an action is taken", ^{ it(@"causes a result to occur", ^{ }); }); describe(@"a second action is taken", ^{ it(@"causes a second result", ^{ }); }); context(@"and the system is adjusted", ^{ describe(@"a third action is taken", ^{ it(@"causes a third result", ^{ }); }); }); });
  9. Expectations and Matchers • With this data NSNumber numericalYes =

    @YES; NSNumber numericalNo = @NO; • In XCTest: XCTAssertEqualObjects(numericalYes, numericalYes); • In Kiwi: [numericalYes should] equal:numericalNo];
  10. Matchers • equal: • beNil: • beLessThan:
 beLessThanOrEqualTo: • beGreaterThan:


    beGreaterThanOrEqualTo: • beYes • beNo • containString: • matchPattern: • beEmpty: • contain:
  11. Mocks and Stubs • Mocks • Can mock both classes

    and protocols • Null mocks can receive any message without complaining • Plain mocks throw exceptions when receiving a message that isn't stubbed • Stubs • Adds ability to receive and handle specific messages • Can parse the message parameters • Can return values
  12. before and after context(@"When the numberOne is set to YES",

    ^{ __block NSNumber *numberOne; __block NSNumber *numberTwo; beforeEach(^{ numberOne = @YES; numberTwo = @NO; }); describe(@"and no change is made", ^{ it(@"should not equal the numberTwo", ^{ [[numberOne shouldNot] equal:numberTwo]; }); }); describe(@"and numberTwo is changed to YES", ^{ beforeEach(^{ numberTwo = @YES; }); it(@"should equal the numberTwo", ^{ [[numberOne should] equal:numberTwo]; }); }); });
  13. How To Use Kiwi • Install your pods • Create

    a Podfile platform :ios, ‘8.1' target :KiwiAndKifTests, :exclusive => true do pod 'Kiwi' end $ pod install
  14. How To Use Kiwi #import <Kiwi/Kiwi.h> SPEC_BEGIN(FeltSpec) context(@"When the system

    has a specific state", ^{ describe(@"an action is taken", ^{ it(@"causes a result to occur", ^{ }); }); }); SPEC_END • Create a test class and enter a spec
  15. Inter- acting Long press longPressViewWithAccessibilityLabel:
 duration: Interact with UI elements

    setOn:forSwitchWithAccessibilityLabel: setValue:forSliderWithAccessibilityLabel: selectPickerViewRowWithTitle: selectDatePickerValue:
  16. How To Use KIF • Install your pods • Create

    a Podfile platform :ios, '8.1' target :KiwiAndKifTests, :exclusive => true do pod 'Kiwi' pod 'KIF' pod ‘KIF-Kiwi’ end $ pod install
  17. How To Use KIF #import <Kiwi/Kiwi.h> #import <KIF-Kiwi.h> SPEC_BEGIN(KifSpec) context(@"When

    the app is launched", ^{ it(@"displays a welcome message", ^{ [tester waitForViewWithAccessibilityLabel:@"Welcome to Kiwi and KIF"]; }); it(@"shows a switcheroo button", ^{ [tester waitForViewWithAccessibilityLabel:@"Switcheroo" traits:UIAccessibilityTraitButton]; }); describe(@"the button is pressed", ^{ beforeEach(^{ [tester tapViewWithAccessibilityLabel:@"Switcheroo" traits:UIAccessibilityTraitButton]; }); it(@"updates the welcome label", ^{ [tester waitForViewWithAccessibilityLabel:@"Or is it KIF and Kiwi?"]; }); }); }); SPEC_END