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

Introduction of CXCKeyValueObserver - KVO and Unit Testing

cockscomb
February 15, 2014

Introduction of CXCKeyValueObserver - KVO and Unit Testing

My presentation @ Cocoa Kansai #54, about CXCKeyValueObserver (https://github.com/cockscomb/CXCKeyValueObserver)

cockscomb

February 15, 2014
Tweet

More Decks by cockscomb

Other Decks in Programming

Transcript

  1. cockscomb

    View Slide

  2. View Slide

  3. ͸ͯͳϒϩά

    View Slide

  4. View Slide

  5. View Slide

  6. CXC

    View Slide

  7. CXCKeyValueObserver

    View Slide

  8. Key Value Observation

    View Slide

  9. Key Value Observation
    • Key Value CodingͰΦϒδΣΫτͷঢ়ଶΛ؂ࢹ
    • @property͸KVCͷنଇʹै͏ͨΊ؂ࢹͰ͖Δ
    • Modelͷঢ়ଶΛ؂ࢹͯ͠ViewΛߋ৽ͳͲͰศར

    View Slide

  10. - (void)viewDidLoad
    {
    [super viewDidLoad];
    [self.someObject addObserver:self
    forKeyPath:@"example"
    options:NSKeyValueObservingOptionNew
    context:NULL];
    }
    !
    - (void)dealloc
    {
    [self.someObject removeObserver:self forKeyPath:@"example"];
    }
    !
    - (void)observeValueForKeyPath:(NSString *)keyPath
    ofObject:(id)object
    change:(NSDictionary *)change
    context:(void *)context
    {
    if (object == self.someObject) {
    if ([keyPath isEqualToString:@"example"]) {
    // do something
    }
    }
    }

    View Slide

  11. - (void)viewDidLoad
    {
    [super viewDidLoad];
    [self.someObject addObserver:self
    forKeyPath:@"example"
    options:NSKeyValueObservingOptionNew
    context:NULL];
    }
    !
    - (void)dealloc
    {
    [self.someObject removeObserver:self forKeyPath:@"example"];
    }
    !
    - (void)observeValueForKeyPath:(NSString *)keyPath
    ofObject:(id)object
    change:(NSDictionary *)change
    context:(void *)context
    {
    if (object == self.someObject) {
    if ([keyPath isEqualToString:@"example"]) {
    // do something
    }
    }
    }
    1

    View Slide

  12. - (void)viewDidLoad
    {
    [super viewDidLoad];
    [self.someObject addObserver:self
    forKeyPath:@"example"
    options:NSKeyValueObservingOptionNew
    context:NULL];
    }
    !
    - (void)dealloc
    {
    [self.someObject removeObserver:self forKeyPath:@"example"];
    }
    !
    - (void)observeValueForKeyPath:(NSString *)keyPath
    ofObject:(id)object
    change:(NSDictionary *)change
    context:(void *)context
    {
    if (object == self.someObject) {
    if ([keyPath isEqualToString:@"example"]) {
    // do something
    }
    }
    }
    1
    2

    View Slide

  13. - (void)viewDidLoad
    {
    [super viewDidLoad];
    [self.someObject addObserver:self
    forKeyPath:@"example"
    options:NSKeyValueObservingOptionNew
    context:NULL];
    }
    !
    - (void)dealloc
    {
    [self.someObject removeObserver:self forKeyPath:@"example"];
    }
    !
    - (void)observeValueForKeyPath:(NSString *)keyPath
    ofObject:(id)object
    change:(NSDictionary *)change
    context:(void *)context
    {
    if (object == self.someObject) {
    if ([keyPath isEqualToString:@"example"]) {
    // do something
    }
    }
    }
    1
    3
    2

    View Slide

  14. - (void)viewDidLoad
    {
    [super viewDidLoad];
    [self.someObject addObserver:self
    forKeyPath:@"example"
    options:NSKeyValueObservingOptionNew
    context:NULL];
    }
    !
    - (void)dealloc
    {
    [self.someObject removeObserver:self forKeyPath:@"example"];
    }
    !
    - (void)observeValueForKeyPath:(NSString *)keyPath
    ofObject:(id)object
    change:(NSDictionary *)change
    context:(void *)context
    {
    if (object == self.someObject) {
    if ([keyPath isEqualToString:@"example"]) {
    // do something
    }
    }
    }

    View Slide

  15. • observeValueForKeyPath:ofObject:change:context:

    ͱ͍͏ϝιουʹશͯهड़͠ͳ͚Ε͹ͳΒͳ͍
    • removeObserver:forKeyPath:

    Λݺͼ๨Εͯ͸͍͚ͳ͍
    • ͩΜͩΜෳࡶʹͳ͍ͬͯ͘
    • ෳ਺ਓ։ൃʹ޲͔ͳ͍આ

    View Slide

  16. CXCKeyValueObserver

    View Slide

  17. - (void)viewDidLoad
    {
    [super viewDidLoad];
    _observer = [[CXCKeyValueObserver alloc]
    initWithObservee:self.someObject
    forKeyPath:@"example"
    options:NSKeyValueObservingOptionNew
    block:^(id observee, NSString *keyPath, NSDictionary *change) {
    // do something
    }];
    }

    View Slide

  18. • BlockΛ༻͍ͯએݴతʹهड़
    • observer΁ͷڧࢀর͕ͳ͘ͳΔͱ͖ࣗಈతʹ

    removeObserver:forKeyPath:
    • ϦʔζφϒϧͳΠϯλʔϑΣʔε

    View Slide

  19. https://github.com/cockscomb/CXCKeyValueObserver

    View Slide

  20. CXCKeyValueObserver
    • MIT License
    • CocoaPodsରԠ
    • ୯ମςετ

    View Slide

  21. ˒14

    View Slide

  22. Unit Test

    View Slide

  23. The Unit Testing of
    Objective-C
    • https://speakerdeck.com/cockscomb/the-
    unit-testing-of-objective-c
    • Kiwi
    • Nocilla

    View Slide

  24. Unit Testing Tools
    • XCTest
    • (BDDϥΠϒϥϦ)
    • ඇಉظςετϥΠϒϥϦ
    • ΧελϜϚονϟ
    • Mock/StubϥΠϒϥϦ
    • ωοτϫʔΫstubϥΠϒϥϦ

    View Slide

  25. XCTest

    View Slide

  26. BDD
    • ϥΠϒϥϦ
    • Kiwi
    • Specta
    • Behavior Driven Development

    View Slide

  27. ඇಉظςετ
    • ϥΠϒϥϦ
    • Kiwi
    • Specta
    • TRVSMonitor
    • TKRGuard
    • ಉظతʹॲཧ͞Εͳ͍ͱ͖ͷςετ

    View Slide

  28. ΧελϜϚονϟ
    • ϥΠϒϥϦ
    • Kiwi
    • OCHamcrest
    • Expecta
    • ศརͳAssert

    View Slide

  29. Mock/Stub
    • ϥΠϒϥϦ
    • Kiwi
    • OCMock
    • ςετ͍ͨ͠ΦϒδΣΫτͷؔ࿈ΦϒδΣΫτ
    Λٖࣅతʹ࡞Γग़ͯ͠ςετΛ؆ུԽ

    View Slide

  30. ωοτϫʔΫStub
    • ϥΠϒϥϦ
    • Nocilla
    • OHHTTPStubs
    • ωοτϫʔΫͷঢ়ଶ΍ϨεϙϯεΛٖࣅతʹ࡞
    Γग़ͯ͠ωοτϫʔΫपΓͷςετΛ؆ུԽ

    View Slide

  31. • BDD
    • Kiwi
    • Specta
    • ඇಉظςετ
    • Kiwi
    • Specta
    • TRVSMonitor
    • TKRGuard
    !
    • ΧελϜϚονϟ
    • Kiwi
    • OCHamcrest
    • Expecta
    • Mock/Stub
    • Kiwi
    • OCMock
    • ωοτϫʔΫstub
    • Nocilla
    • OHHTTPStubs

    View Slide

  32. CXCKeyValueObserver
    • Specta
    • Expecta
    • OCMock

    View Slide

  33. @interface CXCKeyValueObserverTestsObject : NSObject
    @property (nonatomic, strong) id exampleProperty;
    @end
    @implementation CXCKeyValueObserverTestsObject
    @end
    !
    SpecBegin(CXCKeyValueObserver)
    describe(@"KeyValueObserver", ^{
    !
    __block CXCKeyValueObservingBlock block;
    __block id gotObservee;
    __block NSString *gotKeyPath;
    __block NSDictionary *gotChange;
    __block CXCKeyValueObserverTestsObject *observee;
    __block NSString *keyPath;
    !
    beforeEach(^{
    block = ^(id _observee, NSString *_keyPath, NSDictionary *_change) {
    gotObservee = _observee;
    gotKeyPath = _keyPath;
    gotChange = _change;
    };
    gotObservee = nil;
    gotKeyPath = nil;
    gotChange = nil;
    observee = [[CXCKeyValueObserverTestsObject alloc] init];
    keyPath = NSStringFromSelector(@selector(exampleProperty));
    });
    !
    ...
    });
    SpecEnd

    View Slide

  34. it(@"should call its block when the observing property changed", ^{
    __unused CXCKeyValueObserver *observer = [[CXCKeyValueObserver alloc]
    initWithObservee:observee
    forKeyPath:keyPath
    options:0
    block:block];
    !
    observee.exampleProperty = @"exampleNewValue";
    !
    expect(gotObservee).to.equal(observee);
    expect(gotKeyPath).to.beIdenticalTo(keyPath);
    expect(gotChange).notTo.beNil();
    });

    View Slide

  35. it(@"should call removeObserver:forKayPath:context: when deallocated", ^{
    id mock = [OCMockObject niceMockForClass:
    [CXCKeyValueObserverTestsObject class]];
    [[mock expect] removeObserver:[OCMArg any]
    forKeyPath:keyPath
    context:[OCMArg anyPointer]];
    !
    @autoreleasepool {
    CXCKeyValueObserver *observer = [[CXCKeyValueObserver alloc]
    initWithObservee:mock
    forKeyPath:keyPath
    options:0
    block:block];
    !
    observer = nil;
    }
    !
    [mock verify];
    });

    View Slide

  36. CXCKeyValueObserver
    • Specta
    • Expecta
    • OCMock

    View Slide

  37. ࡶஊίʔφʔ

    View Slide

  38. description
    • descriptionॻ͍͓͖͍ͯͨ
    • ඞཁʹԠͯ͡debugDescription
    • AppCodeศར

    View Slide

  39. description
    - (NSString *)description
    {
    NSMutableString *description = [NSMutableString
    stringWithFormat:@"<%@: ", NSStringFromClass([self class])];
    [description appendFormat:@"self.observee=%@", self.observee];
    [description appendFormat:@", self.keyPath=%@", self.keyPath];
    [description appendFormat:@", self.options=%lu", (unsigned long)self.options];
    [description appendFormat:@", self.block=%p", self.block];
    [description appendString:@">"];
    return description;
    }

    View Slide

  40. AppCode

    View Slide

  41. AppCode
    • JetBrains IDE
    • ௒ߴػೳ

    View Slide

  42. View Slide

  43. View Slide

  44. View Slide

  45. AppCode
    • ݡ͍ΦʔτίϯϓϦʔτ
    • ϦϑΥʔϚοτ
    • ίʔυͷੜ੒
    • ϦϑΝΫλϦϯά
    • ίʔυͷղੳ

    View Slide

  46. github.com/cockscomb/CXCKeyValueObserver

    View Slide

  47. DPDLTDPNCIBUFOBCMPHDPN

    View Slide