Permalink
Browse files

Fixing a bug that crashed the tests when stubbing and spying on an ob…

…ject at the same time.

Objects now only get restored to their original class once.
  • Loading branch information...
1 parent 67d5de8 commit e5028c00433c7488cc2b32025e9eccf442dd2290 @tibr tibr committed Apr 16, 2012
Showing with 28 additions and 14 deletions.
  1. +1 −2 Kiwi/KWExample.m
  2. +3 −0 Kiwi/KWIntercept.h
  3. +18 −0 Kiwi/KWIntercept.m
  4. +1 −2 Kiwi/KWTestCase.m
  5. +2 −4 Tests/KWMockTest.m
  6. +1 −2 Tests/KWRealObjectSpyTest.m
  7. +1 −2 Tests/KWRealObjectStubTest.m
  8. +1 −2 Tests/KWStubTest.m
View
@@ -241,8 +241,7 @@ - (void)visitItNode:(KWItNode *)aNode {
}
// Always clear stubs and spies at the end of it blocks
- KWClearAllMessageSpies();
- KWClearAllObjectStubs();
+ KWClearStubsAndSpies();
}];
}
View
@@ -32,6 +32,9 @@ Class KWRealClassForClass(Class aClass);
Class KWSetupObjectInterceptSupport(id anObject);
void KWSetupMethodInterceptSupport(Class interceptClass, SEL aSelector);
+#pragma mark - Managing Stubs & Spies
+void KWClearStubsAndSpies(void);
+
#pragma mark -
#pragma mark Managing Objects Stubs
View
@@ -12,6 +12,7 @@
static const char * const KWInterceptClassSuffix = "_KWIntercept";
static NSMutableDictionary *KWObjectStubs = nil;
static NSMutableDictionary *KWMessageSpies = nil;
+static NSMutableArray *KWRestoredObjects = nil;
#pragma mark -
#pragma mark Intercept Enabled Method Implementations
@@ -212,6 +213,15 @@ Class KWInterceptedSuperclass(id anObject, SEL aSelector) {
return originalSuperclass;
}
+#pragma mark - Managing Stubs & Spies
+
+void KWClearStubsAndSpies(void) {
+ KWRestoredObjects = [NSMutableArray array];
+ KWClearAllMessageSpies();
+ KWClearAllObjectStubs();
+ KWRestoredObjects = nil;
+}
+
#pragma mark -
#pragma mark Managing Objects Stubs
@@ -250,7 +260,11 @@ void KWClearObjectStubs(id anObject) {
void KWClearAllObjectStubs(void) {
for (NSValue *objectKey in KWObjectStubs) {
id stubbedObject = [objectKey nonretainedObjectValue];
+ if ([KWRestoredObjects containsObject:stubbedObject]) {
+ continue;
+ }
KWRestoreOriginalClass(stubbedObject);
+ [KWRestoredObjects addObject:stubbedObject];
}
[KWObjectStubs removeAllObjects];
}
@@ -298,7 +312,11 @@ void KWClearObjectSpy(id anObject, id aSpy, KWMessagePattern *aMessagePattern) {
void KWClearAllMessageSpies(void) {
for (NSValue *objectKey in KWMessageSpies) {
id spiedObject = [objectKey nonretainedObjectValue];
+ if ([KWRestoredObjects containsObject:spiedObject]) {
+ continue;
+ }
KWRestoreOriginalClass(spiedObject);
+ [KWRestoredObjects addObject:spiedObject];
}
[KWMessageSpies removeAllObjects];
}
View
@@ -67,8 +67,7 @@ - (void)setUpExampleEnvironment {
}
- (void)tearDownExampleEnvironment {
- KWClearAllMessageSpies();
- KWClearAllObjectStubs();
+ KWClearStubsAndSpies();
}
#pragma mark -
View
@@ -19,8 +19,7 @@ @interface KWMockTest : SenTestCase
@implementation KWMockTest
- (void)tearDown {
- KWClearAllMessageSpies();
- KWClearAllObjectStubs();
+ KWClearStubsAndSpies();
}
- (void)testItShouldInitializeForAClassWithANameAsANullObject {
@@ -62,8 +61,7 @@ - (void)testItShouldBeOkToStubOnSingletons {
KWMessagePattern *firstMessagePattern = [KWMessagePattern messagePatternWithSelector:@selector(notifyEarth)];
[[Galaxy sharedGalaxy] addMessageSpy:firstSpy forMessagePattern:firstMessagePattern];
- KWClearAllMessageSpies();
- KWClearAllObjectStubs();
+ KWClearStubsAndSpies();
TestSpy *secondSpy = [TestSpy testSpy];
KWMessagePattern *secondMessagePattern = [KWMessagePattern messagePatternWithSelector:@selector(notifyPlanet:)];
@@ -18,8 +18,7 @@ @interface KWRealObjectSpyTest : SenTestCase
@implementation KWRealObjectSpyTest
- (void)tearDown {
- KWClearAllMessageSpies();
- KWClearAllObjectStubs();
+ KWClearStubsAndSpies();
}
- (void)testItShouldNotifySpies {
@@ -18,8 +18,7 @@ @interface KWRealObjectStubTest : SenTestCase
@implementation KWRealObjectStubTest
- (void)tearDown {
- KWClearAllMessageSpies();
- KWClearAllObjectStubs();
+ KWClearStubsAndSpies();
}
- (void)testItShouldRaiseWhenStubbingNonExistentMethods {
View
@@ -19,8 +19,7 @@ @interface KWStubTest : SenTestCase
@implementation KWStubTest
- (void)tearDown {
- KWClearAllMessageSpies();
- KWClearAllObjectStubs();
+ KWClearStubsAndSpies();
}
- (void)testItShouldProcessMatchedInvocations {

0 comments on commit e5028c0

Please sign in to comment.