我正在尝试实现一个补丁程序,如文档和这篇 SO 帖子中所示:Typhoon: How to get an instance conforming to a protocol for production, and another for testing? .
我正在使用块组装并得到错误:
[WPAnalyticsClientImplementation key]: unrecognized selector sent to instance 0x9eb01d0
在TyphoonPatcher.m: 46
.
我的类实现不响应此选择器。应该是?密钥与修补过程有何关系?
context(@"when the controller does something", ^{
it(@"should work", ^{
// This is an application test, so the factory has already been set in the app delegate.
TyphoonComponentFactory *factory = [TyphoonComponentFactory defaultFactory];
TyphoonPatcher* patcher = [[TyphoonPatcher alloc] init];
[patcher patchDefinition:[factory componentForType:@protocol(WPAnalyticsClient)] withObject:^id
{
id mockAnalytics = [KWMock mockForProtocol:@protocol(WPAnalyticsClient)];
[[mockAnalytics should] conformToProtocol:@protocol(WPAnalyticsClient)];
[mockAnalytics stub:@selector(getSomeString) andReturn:theValue(@"fake implementation")];
return mockAnalytics;
}];
[factory attachPostProcessor:patcher];
// The default factory should now return the mocked client.
id <WPAnalyticsClient> client = [factory componentForType:@protocol(WPAnalyticsClient)];
NSLog(@"conforms: %i", [client conformsToProtocol:@protocol(WPAnalyticsClient)]);
NSString *actualValue = [client getSomeString];
NSLog(@"someString: %@", actualValue);
[[theValue([actualValue isEqualToString:@"fake implementation"]) should] equal:theValue(YES)];
});
});
AppDelegate.m
TyphoonComponentFactory *factory = ([[TyphoonBlockComponentFactory alloc] initWithAssembly:[WPAssembly assembly]]);
[factory makeDefault];