Expecta
A matcher framework for Objective-C and Cocoa.
FEATURES
The main advantage of using Expecta over other matcher frameworks is that you do not have to specify the data types. Also, the syntax of Expecta matchers is much more readable and does not suffer from parenthesitis.
expect(@"foo").to.equal(@"foo"); // `to` is a syntactic sugar and can be safely omitted.
expect(foo).notTo.equal(1);
expect([bar isBar]).to.equal(YES);
expect(baz).to.equal(3.14159);
Expecta is framework-agnostic: it works well with XCTest and XCTest-compatible test frameworks such as Specta, or Kiwi.
MATCHERS
expect(x).to.equal(y);
compares objects or primitives x and y and passes if they are identical (==) or equivalent isEqual:).
expect(x).to.beIdenticalTo(y);
compares objects x and y and passes if they are identical and have the same memory address.
expect(x).to.beNil();
passes if x is nil.
expect(x).to.beTruthy();
passes if x evaluates to true (non-zero).
expect(x).to.beFalsy();
passes if x evaluates to false (zero).
expect(x).to.contain(y);
passes if an instance of NSArray or NSString x contains y.
expect(x).to.beSupersetOf(y);
passes if an instance of NSArray, NSSet, NSDictionary or NSOrderedSet x contains all elements of y.
expect(x).to.haveCountOf(y);
passes if an instance of NSArray, NSSet, NSDictionary or NSString x has a count or length of y.
expect(x).to.beEmpty();
passes if an instance of NSArray, NSSet, NSDictionary or NSString x has a count or length of .
expect(x).to.beInstanceOf([Foo class]);
passes if x is an instance of a class Foo.
expect(x).to.beKindOf([Foo class]);
passes if x is an instance of a class Foo or if x is an instance of any class that inherits from the class Foo.
expect([Foo class]).to.beSubclassOf([Bar class]);
passes if the class Foo is a subclass of the class Bar or if it is identical to the class Bar. Use beKindOf() for class clusters.
expect(x).to.beLessThan(y);
passes ifx
is less thany
.
expect(x).to.beLessThanOrEqualTo(y);
passes ifx
is less than or equal toy
.
expect(x).to.beGreaterThan(y);
passes ifx
is greater thany
.
expect(x).to.beGreaterThanOrEqualTo(y);
passes ifx
is greater than or equal toy
.
expect(x).to.beInTheRangeOf(y,z);
passes ifx
is in the range ofy
andz
.
expect(x).to.beCloseTo(y);
passes ifx
is close toy
.
expect(x).to.beCloseToWithin(y, z);
passes ifx
is close toy
withinz
.
expect(^{ /* code */ }).to.raise(@"ExceptionName");
passes if a given block of code raises an exception namedExceptionName
.
expect(^{ /* code */ }).to.raiseAny();
passes if a given block of code raises any exception.
expect(x).to.conformTo(y);
passes ifx
conforms to the protocoly
.
expect(x).to.respondTo(y);
passes ifx
responds to the selectory
.
expect(^{ /* code */ }).to.notify(@"NotificationName");
passes if a given block of code generates an NSNotification namedNotificationName
.
expect(^{ /* code */ }).to.notify(notification);
passes if a given block of code generates an NSNotification equal to the passednotification
.
expect(x).to.beginWith(y);
passes if an instance of NSString, NSArray, or NSOrderedSetx
begins withy
. Also liased bystartWith
expect(x).to.endWith(y);
passes if an instance of NSString, NSArray, or NSOrderedSetx
ends withy
.
expect(x).to.match(y);
passes if an instance of NSStringx
matches regular expression (given as NSString)y
one or more times.
Inverting Matchers
Every matcher's criteria can be inverted by prepending .notTo
or .toNot
:
expect(x).notTo.equal(y);
compares objects or primitives x and y and passes if they are not equivalent.
Asynchronous Testing
Every matcher can be made to perform asynchronous testing by prepending .will
, .willNot
or after(...)
:
expect(x).will.beNil();
passes if x becomes nil before the default timeout.
expect(x).willNot.beNil();
passes if x becomes non-nil before the default timeout.
expect(x).after(3).to.beNil();
passes if x becoms nil after 3.0 seconds.
expect(x).after(2.5).notTo.equal(42);
passes if x doesn't equal 42 after 2.5 seconds.
The default timeout is 1.0 second and is used for all matchers if not otherwise specified. This setting can be changed by calling [Expecta setAsynchronousTestTimeout:x]
, where x
is the desired timeout in seconds.
describe(@"Foo", ^{
beforeAll(^{
// All asynchronous matching using `will` and `willNot`
// will have a timeout of 2.0 seconds
[Expecta setAsynchronousTestTimeout:2];
});
it(@"will not be nil", ^{
// Test case where default timeout is used
expect(foo).willNot.beNil();
});
it(@"should equal 42 after 3 seconds", ^{
// Signle case where timeout differs from the default
expect(foo).after(3).to.equal(42);
});
});
Forced Failing
You can fail a test by using the failure
attribute. This can be used to test branching.
failure(@"This should not happen");
outright fails a test.
WRITING NEW MATCHERS
Writing a new matcher is easy with special macros provided by Expecta. Take a look at how .beKindOf()
matcher is defined:
EXPMatchers+beKindOf.h
#import "Expecta.h"
EXPMatcherInterface(beKindOf, (Class expected));
// 1st argument is the name of the matcher function
// 2nd argument is the list of arguments that may be passed in the function
// call.
// Multiple arguments are fine. (e.g. (int foo, float bar))
#define beAKindOf beKindOf
EXPMatchers+beKindOf.m
#import "EXPMatchers+beKindOf.h"
EXPMatcherImplementationBegin(beKindOf, (Class expected)) {
BOOL actualIsNil = (actual == nil);
BOOL expectedIsNil = (expected == nil);
prerequisite(^BOOL {
return !(actualIsNil || expectedIsNil);
// Return `NO` if matcher should fail whether or not the result is inverted
// using `.Not`.
});
match(^BOOL {
return [actual isKindOfClass:expected];
// Return `YES` if the matcher should pass, `NO` if it should not.
// The actual value/object is passed as `actual`.
// Please note that primitive values will be wrapped in NSNumber/NSValue.
});
failureMessageForTo(^NSString * {
if (actualIsNil)
return @"the actual value is nil/null";
if (expectedIsNil)
return @"the expected value is nil/null";
return [NSString
stringWithFormat:@"expected: a kind of %@, "
"got: an instance of %@, which is not a kind of %@",
[expected class], [actual class], [expected class]];
// Return the message to be displayed when the match function returns `YES`.
});
failureMessageForNotTo(^NSString * {
if (actualIsNil)
return @"the actual value is nil/null";
if (expectedIsNil)
return @"the expected value is nil/null";
return [NSString
stringWithFormat:@"expected: not a kind of %@, "
"got: an instance of %@, which is a kind of %@",
[expected class], [actual class], [expected class]];
// Return the message to be displayed when the match function returns `NO`.
});
}
EXPMatcherImplementationEnd
DYNAMIC PREDICATE MATCHERS
It is possible to add predicate matchers by simply defining the matcher interface, with the matcher implementation being handled at runtime by delegating to the predicate method on your object.
For instance, if you have the following class:
@interface LightSwitch : NSObject
@property (nonatomic, assign, getter=isTurnedOn) BOOL turnedOn;
@end
@implementation LightSwitch
@synthesize turnedOn;
@end
The normal way to write an assertion that the switch is turned on would be:
expect([lightSwitch isTurnedOn]).to.beTruthy();
However, if we define a custom predicate matcher:
EXPMatcherInterface(isTurnedOn, (void));
(Note: we haven't defined the matcher implementation, just it's interface)
You can now write your assertion as follows:
expect(lightSwitch).isTurnedOn();
INSTALLATION
You can setup Expecta using CocoaPods, Carthage or completely manually.
CocoaPods
- Add Expecta to your project's
Podfile
:
target :MyApp do
# your app dependencies
target :MyAppTests do
inherit! search_paths
pod 'Expecta', '~> 1.0'
end
end
Carthage
-
Add Expecta to your project's
Cartfile.private
:github "specta/expecta" "master"
-
Run
carthage update
in your project directory. -
Drag the appropriate Expecta.framework for your platform (located in
Carthage/Build/
) into your applicationโs Xcode project, and add it to your test target(s). -
Run
pod update
orpod install
in your project directory.
Setting Up Manually
-
Clone Expecta from Github.
-
Run
rake
in your project directory to build the frameworks and libraries. -
Add a Cocoa or Cocoa Touch Unit Testing Bundle target to your Xcode project if you don't already have one.
-
For OS X projects, copy and add
Expecta.framework
in theProducts/osx
folder to your project's test target.For iOS projects, copy and add
Expecta.framework
in theProducts/ios
folder to your project's test target.You can also use
libExpecta.a
if you prefer to link Expecta as a static library โ iOS 7.x and below require this. -
Add
-ObjC
and-all_load
to the Other Linker Flags build setting for the test target in your Xcode project. -
You can now use Expecta in your test classes by adding the following import:
@import Expecta; // If you're using Expecta.framework // OR #import <Expecta/Expecta.h> // If you're using the static library, or the framework
STATUS
Expecta, and Specta are considered done projects, there are no plans for active development on the project at the moment aside from ensuring future Xcode compatability. Therefore it is a stable dependency, but will not be moving into the Swift world. If you are looking for that, we recommend you consider Quick and Nimble.
Contribution Guidelines
- Please use only spaces and indent 2 spaces at a time.
- Please prefix instance variable names with a single underscore (
_
). - Please prefix custom classes and functions defined in the global scope with
EXP
.
License
Copyright (c) 2012-2016 Specta Team. This software is licensed under the MIT License.