Latest 0.1
Homepage https://github.com/stripe/tracer-objc
License MIT The MIT License

Copyright (c) 2011- Stripe, Inc. (https://stripe.com)

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the “Software”), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

Platforms ios 9.0, requires ARC
Frameworks Foundation, UIKit
Authors

Tracer

Tracer is an experimental testing tool that lets you record & play back the behavior of arbitrary objects in Objective-C.

Let’s say you have a dependency in your code, ThatThing. You call ThatThing, it calls you back, and behavior varies depending on user input or environmental factors.

@protocol ThatInterface <NSObject>
- (void)someCommand:(int)i;
- (void)someOtherCommand:(NSArray *)objects;
// ...
- (void)onError:(NSError *)error;
- (void)onOtherError:(NSError *)error;
@end

@interface ThatThing : NSObject <ThatInterface>
@end

Testing complex async behavior is hard, especially if you don’t control the source of behavior.

Tracer lets you record behavior of ThatThing as a trace:

ThatThing *thing = [ThatThing new];
TRCRecorder *recorder = [TRCRecorder new];
[recorder startRecording:thing protocol:@protocol(ThatInterface)];
NSString *result = [thing someCommand:-100];
[recorder stopRecording:thing protocol:@protocol(ThatInterface) completion:^(TRCTrace *trace, NSError *error) {
    // save trace
}];

After recording completes, Tracer prints the trace to the console as JSON, so you can save behavior to a file.

2019-04-17 23:01:22.689124-0700 xctest[62038:4377601] -----BEGIN TRACE JSON-----
{
  "start_ms" : 1551678464427,
  "id" : "trace",
  "protocol" : "SomeProtocol",
  "calls" : [
    {
      "id" : "call",
      "start_ms" : 203,
      "method" : "someCommand:",
      "arguments" : [
        {
          "id" : "value",
          "type" : "int",
          "object_type" : "not_an_object",
          "object_value" : -100
        }
      ],
      "return_value" : {
        "id" : "value",
        "type" : "void",
      }
    }
  ]
}
-----END TRACE JSON-----

In your tests, instead of mocking the complex behavior of ThatThing, you can simply play recorded behavior:

ThatThing *thing = [ThatThing new];
TRCTrace *trace = [TRCTrace loadFromJSONFile:@"saved_trace"];
[TRCPlayer playTrace:trace onTarget:thing completion:^(NSError * _Nullable error) {

}];

Latest podspec

{
    "name": "tracer-objc",
    "version": "0.1",
    "summary": "Generic record & playback framework for Objective-C",
    "homepage": "https://github.com/stripe/tracer-objc",
    "authors": {
        "Stripe": "[email protected]"
    },
    "frameworks": [
        "Foundation",
        "UIKit"
    ],
    "requires_arc": true,
    "platforms": {
        "ios": "9.0"
    },
    "public_header_files": "Tracer/*.h",
    "source_files": [
        "Tracer/*.{h,m}",
        "Tracer/Private/*.{h,m}"
    ],
    "source": {
        "git": "https://github.com/stripe/tracer-objc.git",
        "tag": "v0.1"
    },
    "license": {
        "type": "MIT",
        "text": "    The MIT Licensenn    Copyright (c) 2011- Stripe, Inc. (https://stripe.com)nn    Permission is hereby granted, free of charge, to any person obtaining a copyn    of this software and associated documentation files (the "Software"), to dealn    in the Software without restriction, including without limitation the rightsn    to use, copy, modify, merge, publish, distribute, sublicense, and/or selln    copies of the Software, and to permit persons to whom the Software isn    furnished to do so, subject to the following conditions:nn    The above copyright notice and this permission notice shall be included inn    all copies or substantial portions of the Software.nn    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS ORn    IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,n    FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THEn    AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHERn    LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,n    OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS INn    THE SOFTWARE.n"
    }
}

Pin It on Pinterest

Share This