Permalink
Browse files

Accept nil responses.

  • Loading branch information...
1 parent add7de6 commit 0b4df39e9368fc2bb4e3ec185ea9438e503e8658 @robrix committed Mar 23, 2010
Showing with 32 additions and 15 deletions.
  1. +2 −1 RXMockObject.m
  2. +30 −14 RXMockObjectTests.m
View
@@ -51,7 +51,7 @@ -(void)setResponseObject:(id)response forSelector:(SEL)selector {
}
-(void)setResponseObject:(id)response forSelector:(SEL)selector withArgument:(id)argument {
- [self setResponseObject: response forSelector: selector withArguments: [NSArray arrayWithObject: argument ?: RXMockObjectNullPlaceholder]];
+ [self setResponseObject: response ?: RXMockObjectNullPlaceholder forSelector: selector withArguments: [NSArray arrayWithObject: argument ?: RXMockObjectNullPlaceholder]];
}
-(void)setResponseObject:(id)response forSelector:(SEL)selector withArguments:(NSArray *)arguments {
@@ -88,6 +88,7 @@ -(void)forwardInvocation:(NSInvocation *)invocation {
[arguments addObject: argument ?: RXMockObjectNullPlaceholder];
}
id response = [self responseForSelector: invocation.selector withArguments: [arguments copy]];
+ if(response == RXMockObjectNullPlaceholder) response = nil;
[invocation setReturnValue: &response];
}
}
View
@@ -17,29 +17,45 @@ @interface RXMockObjectTests : SenTestCase {
@implementation RXMockObjectTests
-(void)setUp {
- mock = [RXMockObject mockObjectForClass: [NSArray class]];
+ mock = [RXMockObject mockObjectForClass: [NSString class]];
}
-(void)testRespondsToNullaryMessagesWithTheGivenObject {
- [mock setResponseObject: @"nullary response" forSelector: @selector(lastObject)];
- RXAssertEquals([mock lastObject], @"nullary response");
+ [mock setResponseObject: @"nullary response" forSelector: @selector(propertyList)];
+ RXAssertEquals([mock propertyList], @"nullary response");
}
-(void)testRespondsToUnaryMessagesWithTheGivenObject {
- [mock setResponseObject: @"result 1" forSelector: @selector(arrayByAddingObject:) withArgument: @"argument 1"];
- [mock setResponseObject: @"result 2" forSelector: @selector(arrayByAddingObject:) withArgument: @"argument 2"];
- RXAssertEquals([mock arrayByAddingObject: @"argument 1"], @"result 1");
- RXAssertEquals([mock arrayByAddingObject: @"argument 2"], @"result 2");
- RXAssertNil([mock arrayByAddingObject: @"not specified"]);
- RXAssertNil([mock arrayByAddingObject: nil]);
+ [mock setResponseObject: @"result 1" forSelector: @selector(componentsSeparatedByString:) withArgument: @"argument 1"];
+ [mock setResponseObject: @"result 2" forSelector: @selector(componentsSeparatedByString:) withArgument: @"argument 2"];
+ RXAssertEquals([mock componentsSeparatedByString: @"argument 1"], @"result 1");
+ RXAssertEquals([mock componentsSeparatedByString: @"argument 2"], @"result 2");
+ RXAssertNil([mock componentsSeparatedByString: @"not specified"]);
+ RXAssertNil([mock componentsSeparatedByString: nil]);
}
-(void)testRespondsToMessagesWithNilArguments {
- [mock setResponseObject: @"result 1" forSelector: @selector(arrayByAddingObject:) withArgument: nil];
- [mock setResponseObject: @"result 2" forSelector: @selector(arrayByAddingObject:) withArgument: @"argument 2"];
- RXAssertEquals([mock arrayByAddingObject: nil], @"result 1");
- RXAssertEquals([mock arrayByAddingObject: @"argument 2"], @"result 2");
- RXAssertNil([mock arrayByAddingObject: @"not specified"]);
+ [mock setResponseObject: @"result 1" forSelector: @selector(componentsSeparatedByString:) withArgument: nil];
+ [mock setResponseObject: @"result 2" forSelector: @selector(componentsSeparatedByString:) withArgument: @"argument 2"];
+ RXAssertEquals([mock componentsSeparatedByString: nil], @"result 1");
+ RXAssertEquals([mock componentsSeparatedByString: @"argument 2"], @"result 2");
+ RXAssertNil([mock componentsSeparatedByString: @"not specified"]);
+}
+
+
+-(void)testAcceptsNilResponses {
+ [mock setResponseObject: nil forSelector: @selector(propertyList)];
+ [mock setResponseObject: nil forSelector: @selector(componentsSeparatedByString:) withArgument: nil];
+ [mock setResponseObject: @"" forSelector: @selector(componentsSeparatedByString:) withArgument: @""];
+ [mock setResponseObject: nil forSelector: @selector(stringByReplacingOccurrencesOfString:withString:) withArguments: [NSArray arrayWithObjects: RXMockObjectNullPlaceholder, RXMockObjectNullPlaceholder, nil]];
+ [mock setResponseObject: nil forSelector: @selector(stringByReplacingOccurrencesOfString:withString:) withArguments: [NSArray arrayWithObjects: @"", RXMockObjectNullPlaceholder, nil]];
+ [mock setResponseObject: nil forSelector: @selector(stringByReplacingOccurrencesOfString:withString:) withArguments: [NSArray arrayWithObjects: RXMockObjectNullPlaceholder, @"", nil]];
+ RXAssertNil([mock propertyList]);
+ RXAssertNil([mock componentsSeparatedByString: nil]);
+ RXAssertNotNil([mock componentsSeparatedByString: @""]);
+ RXAssertNil([mock stringByReplacingOccurrencesOfString: nil withString: nil]);
+ RXAssertNotNil([mock stringByReplacingOccurrencesOfString: @"" withString: nil]);
+ RXAssertNotNil([mock stringByReplacingOccurrencesOfString: nil withString: @""]);
}
@end

0 comments on commit 0b4df39

Please sign in to comment.