Latest 0.1
Homepage https://github.com/luisobo/Defactory
License MIT
Platforms ios 4.0, osx 10.7, requires ARC
Authors

Objective-C object factory for testing.

Features

  • Define factories once, build everywhere.
  • Named factories.
  • Sequences.
  • Associations.
  • Handle primitives
  • Factory inheritance.
  • Tested.

Installation

As a CocoaPod
Just add this to your Podfile

pod 'Defactory'

Other approaches

  • You should be able to add Defactory to you source tree. If you are using git, consider using a git submodule

Usage

Given the following User class

LSUser

@interface LSUser : NSObject
@property (nonatomic, strong) NSString *username;
@property (nonatomic, strong) NSString *password;
@property (nonatomic, strong) NSString *state;
@property (nonatomic, assign) NSUInteger loginCount;
@property (nonatomic, assign) BOOL somethingBool;
@property (nonatomic, strong) NSString *email;
@property (nonatomic, strong) LSUser *dad;
@end
@implementation LSUser
@end

Define the one or multiple factories

FACTORIES(^{
    // Default factory
    [LSUser defineFactory:^(LSFactory *f) {
        f[@"username"] = @"foo"; // Set values.
        f[@"password"] = @"hunter2";

        // Define sequences.
        f[@"email"] = sequence(^(NSUInteger i) { return [NSString stringWithFormat:@"foo%[email protected]", i]; });
    }];

    // Other named factories with inheritance.
    [LSUser define:@"suspended" parent:@"LSUser" factory:^(LSFactory *f) {
        f[@"state"] = @"suspended";

        // User boxed primitives, Defactory will take care of the rest.
        f[@"loginCount"] = @2;
        f[@"somethingBool"] = @YES;
    }];

    [LSUser define:@"son" parent:@"LSUser" factory:^(LSFactory *f) {

        // Associations.
        f[@"dad"] = association([LSUser class]);
    }];
});

Building stuff


// Build an object with the default factory and params
LSUser *user = [LSUser build];

// Build an object with the default factory overriding some params.
user = [LSUser buildWithParams:@{@"password": @"secret", @"state": @"active"}];

// Build an object with a named factory.
user = [LSUser build:@"suspended"];

// Build an object with a named factory overriding some params.
user = [LSUser build:@"suspended" params:@{@"loginCount": @4}];

Contributing

  1. Fork it
  2. Create your feature branch
  3. Commit your changes
  4. Push to the branch
  5. Create new Pull Request

Latest podspec

{
    "name": "Defactory",
    "version": "0.1",
    "summary": "The simplest way to build test objects.",
    "description": "                   * Define factories once, build everywhere.n                   * Named factories.n                   * Sequences.n                   * Associations.n                   * Handles primitivesn                   * Factory inheritance.n                   * Tested.n",
    "homepage": "https://github.com/luisobo/Defactory",
    "license": {
        "type": "MIT",
        "file": "LICENSE"
    },
    "authors": {
        "Luis Solano": "[email protected]"
    },
    "platforms": {
        "ios": "4.0",
        "osx": "10.7"
    },
    "source": {
        "git": "https://github.com/luisobo/Defactory.git",
        "tag": "0.1"
    },
    "source_files": [
        "Defactory",
        "Defactory/**/*.{h,m}"
    ],
    "public_header_files": "Defactory/**/*.h",
    "requires_arc": true
}

Pin It on Pinterest

Share This