• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: NSObject -poseAs:
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: NSObject -poseAs:


  • Subject: Re: NSObject -poseAs:
  • From: "Mike R. Manzano" <email@hidden>
  • Date: Thu, 31 May 2007 16:36:17 -0700


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:
This email sent to email@hidden


  • Follow-Ups:
    • Re: NSObject -poseAs:
      • From: 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>)

  • Prev by Date: Re: String Contains String
  • Next by Date: Re: Math operations for NSNumber
  • Previous by thread: Re: NSObject -poseAs:
  • Next by thread: Re: NSObject -poseAs:
  • Index(es):
    • Date
    • Thread