I have a few repetitive specs that I would like to DRY up. The common functionality doesn't lend itself to moving into a beforeEach
block. Essentially, it's object creation and is 4 lines for each of 12 objects, I'd like to turn those 4 lines into a single function call.
Where can I put helper functions in a Kiwi spec?
In RSpec I can just put def
between spec blocks, but that doesn't appear to be possible here. I've even tried skipping the SPEC_END
macro and adding that content myself so I could add functions inside the @implementation from SPEC_BEGIN
but that doesn't seem to work, either.
Correction... I can manage something that kind of works with hand-coding the SPEC_END
macro. I had the end } mis-placed. But still, it fails, because the method isn't in the @interface
.
Create your helper function as a block just after the SPEC_BEGIN:
SPEC_BEGIN(MySpec)
NSDate* (^dateFromString) (NSString *) = ^NSDate* (NSString *dateString) {
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setTimeStyle:NSDateFormatterNoStyle];
[dateFormatter setDateFormat:@"MM-dd-yyyy"];
return [dateFormatter dateFromString:dateString];
};
describe(@"something", ^{
NSDate *checkDate = dateFromString(@"12-01-2005");
...
});
SPEC_END
You could also create a straight C function above the SPEC_BEGIN()
scope.
NSString *makeAString () {
return @"A String";
}
Or if you have helper functions that will be used across several Spec files, place these functions in a separate file and import the header. I've found this to be a great way to clean up my Specs.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With