CMFactory 1.4.1

CMFactory 1.4.1

TestsTested
LangLanguage Obj-CObjective C
License MIT
ReleasedLast Release Dec 2014

Maintained by Lucas Medeiros Leite.



CMFactory 1.4.1

  • By
  • Lucas Medeiros

This project brings the idea of FactoryGirl to iOS projects, and it loads fixtures and unmarshall them into a Mantle class or NSDictionary class.

Contact:

Developed by Lucas Medeiros at Codeminer42 in Fortaleza

Follow us on twitter: @Codeminer42

Development requirements

Usage

  • Loading fixtures
#import "CMFixture.h"
#import "Kiwi.h"

SPEC_BEGIN(YourSpec)

    //If you use github's Mantle library                    
    beforeEach(^{
         YourClass *yourInstance = [CMFactory buildUsingMantleClass:[YourClass class] fromFixture:@"YourClass"];
         //Or
         NSArray *aColletion = [CMFactory buildUsingMantleClass:[YourClass class] fromFixture:@"People"];
    });

    //If you don't use github's Mantle library
    beforeEach(^{
         NSDictionary *dictionary = [CMFactory buildUsingFixture:@"YourClass"];
         //Or
         NSArray *aColletion = [CMFactory buildUsingFixture:@"People"];
    });

SPEC_END

After that, in your test project, you need to create a file with your factory name in .json or .plist format (Ex: if your factory name is 'People' and you have a file people.json and other people.plist it will unmarshall the .json file)

  • Creating instances dynamically
#import "CMFactory.h"
#import "Kiwi.h"

SPEC_BEGIN(YourSpec)

    specify(^{

        CMFactory *factory = [CMFactory forClass:[YourClass class]];

        [factory addToField:@"aFieldName" value:^{
             return @"www.codeminer42.com";
        }];

        YourClass *instance = [factory build];
        [[instance.aFieldName should] equal: @"www.codeminer42.com"];

    });

    context(@"when using sequence method", ^{

         __block CMFactory *factory;
         __block NSArray *images;

         beforeEach(^{
              factory = [CMFactory forClass:[CMImage class]];
              [factory addToField:@"url" sequenceValue:^(NSUInteger sequence) {
                  return [NSString stringWithFormat:@"www.github.com%d", sequence];
              }];
              images = [factory buildWithCapacity:3];
         });

         specify(^{
              [[images should] haveCountOf:3];
         });

         specify(^{

             for(NSUInteger i = 0; i < 3; i++) {
                 CMImage *image = [images objectAtIndex:i];
                  [[image.url should] equal:[NSString stringWithFormat:@"www.github.com%d", i]];
             }

         });

    });

SPEC_END
  • See the example code

Requirements

CMFactory requires iOS 5.x or greater.

Next version

Integration with core data to use 'create' like methods

License

Usage is provided under the MIT License. See LICENSE for the full details.