Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: NSObject -poseAs:




On May 31, 2007, at 4:08 PM, Chris Hanson wrote:

On May 31, 2007, at 3:46 PM, Mike R. Manzano wrote:

One example is the use of factory and abstract factory patterns for decoupling. In a dynamic language, it can be a code smell since the classes themselves can be treated like factories.

[…]

Your regular code can just ask Something for its defaultSomething. For your test, you can write a subclass:

@interface SomethingForTesting : NSObject
+ (id)defaultSomething; // returns a different singleton than its superclass
@end


Then just use that subclass in your test code instead of its parent. Everything else will still use the parent class, but your test code will use the subclass.

Yes, but it's not my test accessing the factory, it's the code that it's testing. I shouldn't be altering that code to use the SomethingForTesting subclass. The idea was to, in the test, have my mock factory poseAs the real factory, so that in the code being tested, the singleton that it ends up using is a mock object. Am I missing something here?


<codebarf>

@interface ThingToTest : NSObject
- (void) methodToTest ;
@end

@implementation ThingToTest
- (void) methodToTest
{
	// It is here, in the method being tested, that a singleton is accessed
	id aSingleton = [SingletonClass defaultSomething] ;
	[aSingleton useThisSingleton] ;
}
@end

@interface MockSingletonClass : SingletonClass
+ (id) defaultSomething ; // override the real singleton's accessor
@end

@implementation MockSingletonClass
[…]
@end

// The test class
@interface TestThingToTest : SenTestCase
@end

@implementation TestThingToTest
- (void) testThingToTest
{
	ThingToTest *thing = [[[ThingToTest alloc]init]autorelease];
	[[MockSingletonClass class] poseAs:[SingletonClass]];
	[thing methodToTest] ; // methodToTest uses the mock singleton ctor
	// Here's where it would be nice to unposeAs.
}
@end

</codebarf>


On a separate note, that's good info regarding using the class itself as a factory (I do that all the time--really convenient, and my sample code above uses this pattern). However, in the real code, I don't have a class, as the "root" of the hierarchy is a protocol, not a class. Can protocols have static methods? That would be neat too.


Mike_______________________________________________

Cocoa-dev mailing list (email@hidden)

Do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/email@hidden

This email sent to email@hidden
References: 
 >NSObject -poseAs: (From: "Mike R. Manzano" <email@hidden>)
 >Re: NSObject -poseAs: (From: "Clark Cox" <email@hidden>)
 >Re: NSObject -poseAs: (From: "Mike R. Manzano" <email@hidden>)
 >Re: NSObject -poseAs: (From: Jim Brownfield <email@hidden>)
 >Re: NSObject -poseAs: (From: "Mike R. Manzano" <email@hidden>)
 >Re: NSObject -poseAs: (From: Chris Hanson <email@hidden>)



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.