I am new to test driven development and have a basic question. How do I access private properties for unit testing? There are plenty of answers on how you access methods but I couldn't find much on properties. Here is what I've got now for my class CDTest. Excluding some of the imported headers for readability purposes.
CDTest.h
#interface CDTest : NSObject
#end
CDTest.m
#import "CDTest.h"
#import "CDTest+Protected.h"
#interface CDTest()
#property (strong, nonatomic) NSManagedObjectContext *context;
#end
#implementation CDTest
- (void)setup
{
//Sets up the context
}
#end
CDTest+Protected.h
#interface CDTest()
- (void)setup;
#end
Unit test .m file
#import "CDTest.h"
#import "CDTest+Protected.h"
#implementation CDTestTests
{
CDTest *cdTest;
}
- (void)setUp
{
[super setUp];
cdTest = [CDTest new];
}
- (void)testSetup
{
[cdTest setup];
//Now I need access to the properties in cdTest to validate them
}
#end
In Objective-C properties are methods (each property is a getter and a setter if it is read/write).
Just declare your property in your protected category, which should be declared something like this
#interface CDTest(Protected)
- (void)setup;
#property (strong, nonatomic) NSManagedObjectContext *context;
#end
Related
I want to inherit my base class properties and methods which will be used by my several derived classes. I want these properties and methods to be exactly protected so that they will only be visible in derived class and not to any external class. But it always gives me some errors.
#interface BasePerson : NSObject
#end
#interface BasePerson ()
#property (nonatomic, strong) NSMutableArray<Person*>* savedPersons;
#property (nonatomic) BOOL shouldSavePerson;
#end
#interface DerivedPerson1 : BasePerson
#end
#implementation DerivedPerson1
- (instancetype)init
{
if (self = [super init]) {
self.savedPersons = [NSMutableArray array];
self.shouldSavePerson = NO;
}
return self;
}
It always gives me an error that
Property 'savedPersons' not found on object of type 'DerivedPerson1 *'
Property 'shouldSavePerson' not found on object of type 'DerivedPerson1 *'
How i can make use of inheritance in Objective C, I don't want savedPersons and shouldSavePerson properties to be visible to external classes. I only want them to visible in my base class and all the derived classes.
Any help will be great. Thanks
This is not something that the objectiveC really support. There are some ways though. So lets see.
If you put a property in the source file class extension then it is not exposed and you can not access it in the subclass either.
One way is to put all of the subclasses into the same source file as the base class. This is not a good solution at all as you do want to have separate files for separate classes.
It seems logical to import the BaseClass.m in the SubClass source file but that will produce a linker error saying that you have duplicate symbols.
And the solution:
Separate the extension into a separate header. So you have a MyClass
Header:
#import <Foundation/Foundation.h>
#interface MyClass : NSObject
#end
Source:
#import "MyClass.h"
#import "MyClassProtected.h"
#implementation MyClass
- (void)foo {
self.someProperty = #"Some text from base class";
}
#end
Then you create another header file (only the header) MyClassProtected.h which has the following:
#import "MyClass.h"
#interface MyClass ()
#property (nonatomic, strong) NSString *someProperty;
#end
And the subclass MyClassSubclass
Header:
#import "MyClass.h"
#interface MyClassSubclass : MyClass
#end
And the source:
#import "MyClassSubclass.h"
#import "MyClassProtected.h"
#implementation MyClassSubclass
- (void)foo {
self.someProperty = #"We can set it here as well";
}
#end
So now if the user MyClassSubclass he will not have the access to the protected property which is essentially what you want. But the downside is the user may still import MyClassProtected.h after which he will have the access to the property.
Objective-C doesn't have member access control for methods, but you can emulate it using header files.
BasePerson.h
#interface BasePerson : NSObject
#property (strong,nonatomic) SomeClass *somePublicProperty;
-(void) somePublicMethod;
#end
BasePerson-Private.h
#import "BasePerson.h"
#interface BasePerson ()
#property (nonatomic, strong) NSMutableArray<Person*>* savedPersons;
#property (nonatomic) BOOL shouldSavePerson;
#end
BasePerson.m
#import "BasePerson-Private.h"
...
DerivedPerson1.h
#import "BasePerson-Private.h"
#inteface DerivedPerson1 : BasePerson
...
#end
Now any class that #imports BasePerson.h will only see the public methods. As I said though, this is only emulating access control since if a class #imports *BasePerson-Private.h" they will see the private members; this is just how C/Objective-C is.
We can achieve using #protected access specifier
#interface BasePerson : NSObject {
#protected NSMutableArray *savedPersons;
#protected BOOL shouldSavePerson;
}
DerivedPerson1.m
#implementation DerivedPerson1
- (instancetype)init
{
if (self = [super init]) {
self->savedPersons = [NSMutableArray array];
self->shouldSavePerson = NO;
}
return self;
}
#end
OtherClass.m
#import "OtherClass.h"
#import "BasePerson.h"
#implementation OtherClass
- (void)awakeFromNib {
BasePerson *base = [[BasePerson alloc]init];
base->savedPersons = #[];//Getting Error. Because it is not a subclass.
}
#end
Starting point
I have a class, say A, used by an UI view.
A has a delegate that should notify UI view and this one should be write something on screen.
Question
What is the best approach to achieve this feature?
Seems something like observer-observable pattern
Code
---A.h
#interface A : NSObject
#end
---A.m
#implementation A
-(void)fooDelegate:(FooType *)sender {
/* Here I need to notify UI (that change notificationArea.text) */
}
---UIView.h
#interface UIView : UIViewController
#property(strong, nonatomic, retain) A* a;
#property(strong, nonatomic) IBOutlet UITextField *notificationArea;
#end
Based on the comments, I guess just code is what you're looking for...
Create your delegate protocol:
#protocol ADelegate;
#interface A : NSObject
#property (nonatomic, weak) id <ADelegate> delegate;
#end
#protocol ADelegate <NSObject>
#optional
-(void)fooDelegate:(A *)a;
#end
Notify your delegate:
#implementation A
-(void)fooDelegate:(FooType *)sender {
if ([[self delegate] respondsToSelector:#selector(fooDelegate:)]) {
[[self delegate] fooDelegate:self];
}
}
#end
Conform to the delegate protocol:
#import "A.h"
#import "MyView.h"
#interface MyView <ADelegate>
#end
#implementation MyView
-(void)fooDelegate:(A *)a {
// update text field here
}
#end
Finally, whenever you create an instance of A, set the delegate (where self in this example is an instance of MyView:
A *a = [[A alloc] init];
[a setDelegate:self];
I have four classes MainVC, ParentClient and ChildClient1, ChildClient2(which are subclasses of ParentClient). ParentClient has a delegate to MainVC such that in MainVC
- (void)viewDidLoad
{
[super viewDidLoad];
[ParentClient instance].mainViewDelegate = self;
}
And then the ParentClient looks like this
#interface BaseClient : NSObject
#property (assign) id<MainVCInteraction> mainViewDelegate;
+(instancetype) instance;
#end
Now I want to access mainViewDelegate from ChildClient1, ChildClient2 and it returns me nil while [ParentClient instance].mainViewDelegate returns the correct value
Here is what I did I removed the BaseClient Class so that ChildClient1, ChildClient2 were no longer subclasses of BaseClient. I defined a objective-c protocol file MainVCInteaction.h and made Client1, Client2 look like this:
#import "MainVCInteraction.h"
#interface ChildClient1 : NSObject
#property (assign) id<MainVCInteraction> mainViewDelegate;
+(instancetype) instance;
#end
#import "MainVCInteraction.h"
#interface ChildClient2 : NSObject
#property (assign) id<MainVCInteraction> mainViewDelegate;
+(instancetype) instance;
#end
And then MainVC implements this protocol, I assigned the delegate like this
- (void)viewDidLoad
{
[super viewDidLoad];
[ChildClient1 instance].mainViewDelegate = self;
[ChildClient2 instance].mainViewDelegate = self;
}
I've a main class where I want to define two protocols (1 used by a class A, the other by class B) (ios 6.1, xcode 4.6.3 , ARK mode, storyboard project).
According to official syntax, all my code seems to be correct.
But when I try to use the second delegate , nothing work correctly, my 2nd delegate does not respond
**HEADER myProtocols.h**
#import ...
#class myProtocols;
#protocol myProtocol1 <NSObject>
// list of methods and properties
doStuff:(float) myValue;
#end
#protocol myProtocol2 <NSObject>
// list of methods and properties
doOtherStuff:(float) myValue2 andText:(NSString *)myText andType:(NSString *)myType;
#end
#interface myProtocols:NSObject
{
__unsafe_unretained id <myProtocol1> _myDelegate1;
__unsafe_unretained id <myProtocol2> _myDelegate2;
}
#property (nonatomic, assign) id <myProtocol1> myDelegate1;
#property (nonatomic, assign) id <myProtocol2> myDelegate2;
#end
**MESSAGES myProtocols.m**
#import myProtocols.h
#implementation myProtocols
#synthesize myDelegate1 = _myDelegate1
#synthesize myDelegate2 = _myDelegate2
...
if ([_myDelegate1 respondsToSelector:#selector(doStuff:)])
[_myDelegate1 doStuff:3.5]; **// THIS DELEGATE WORK VERY WELL**
...
if ([_myDelegate2 respondsToSelector:#selector(doOtherStuff:andText:andType:)])
[_myDelegate2 doOtherStuff:4.5 andText:#"YES MAN" andType:#"YES BRO"];
**// THIS DELEGATE DONT WORK, IT'S LIKE IT DOESNT INIT**
...
#end
**HEADER classA.h**
#import "myProtocols.h"
#interface classA: UIViewController <myProtocol1>
#property(strong, nonatomic) myProtocols *myProtoVC;
//-(void) doStuff:(float) myValue; according to comments, nothing to do :(
#end
**MESSAGES classA.m**
#import "classA.h"
#interface classA ()
#end
#implementation classA
- (void)viewDidLoad
{
[super viewDidLoad];
_myProtoVC = [[myProtocols alloc] init];
_myProtoVC.myDelegate1 = self;
}
-(void) doStuff:(float) myValue
{
NSLog(#" YES VALUE IS %f",myValue);
}
**HEADER classB.h**
#import "myProtocols.h"
#interface classB: UIViewController <myProtocol2>
#property(strong, nonatomic) myProtocols *myProtoVC;
//-(void) doOtherStuff:(float) myValue2 andText:(NSString *)myText andType:(NSString *)myType; according to comments, nothing to do :(
#end
**MESSAGES classB.m**
#import "classB.h"
#interface classB ()
#end
#implementation classB
- (void)viewDidLoad
{
[super viewDidLoad];
_myProtoVC = [[myProtocols alloc] init];
_myProtoVC.myDelegate2 = self;
}
-(void) doOtherStuff:(float) myValue2 andText:(NSString *)myText andType:(NSString *)myType;
{
NSLog(#" YES VALUE IS %f and text %# and type %#",myValue2,myText,myType);
}
So, my mistake is to call [_myDelegate2 doOtherStuff..] directly inside a function in myProtocols called only by classA.
Then, if I want to call a function in myProtocols using both delegate I MUST init both these delegates in the class (A or B doesnt matter) I use to call this function:
**MESSAGES myProtocols.m**
#import myProtocols.h
#implementation myProtocols
#synthesize myDelegate1 = _myDelegate1
#synthesize myDelegate2 = _myDelegate2
-(void) pleaseDoIt
{
if ([_myDelegate1 respondsToSelector:#selector(doStuff:)])
[_myDelegate1 doStuff:3.5]; **// THIS DELEGATE WORK VERY WELL**
...
if ([_myDelegate2 respondsToSelector:#selector(doOtherStuff:andText:andType:)])
[_myDelegate2 doOtherStuff:4.5 andText:#"YES MAN" andType:#"YES BRO"];
**// THIS DELEGATE NOW WORK VERY WELL**
}
#end
**HEADER classA.h**
#import "myProtocols.h"
#import "classB.h"
#interface classA: UIViewController <myProtocol1>
#property(strong, nonatomic) myProtocols *myProtoVC;
#property(strong, nonatomic) classB *classBVC;
//-(void) doStuff:(float) myValue;
#end
**MESSAGES classA.m**
#import "classA.h"
#interface classA ()
#end
#implementation classA
- (void)viewDidLoad
{
[super viewDidLoad];
_myProtoVC = [[myProtocols alloc] init];
_classBVC = [[myProtocols alloc] init];
_myProtoVC.myDelegate1 = self;
_myProtoVC.myDelegate2 = _classBVC // THIS IS THE POINT!!!
[_myProtoVC pleaseDoIt];
}
-(void) doStuff:(float) myValue
{
NSLog(#" YES VALUE IS %f",myValue);
}
I have a test case and a helper class. In the helper class I want to use asserts too like here:
MainTests.h
#import <SenTestingKit/SenTestingKit.h>
#interface MainTests : SenTestCase
#end
MainTests.m
#import "MainTests.h"
#import "HelperClass.h"
#implementation MainTests
- (void)testExample {
HelperClass *helperClass = [[HelperClass alloc] init];
[helperClass fail];
}
#end
HelperClass.h
#import <SenTestingKit/SenTestingKit.h>
#interface HelperClass : SenTestCase
- (void)fail;
#end
HelperClass.m
#import "HelperClass.h"
#implementation HelperClass
- (void)fail {
STFail(#"This should fail");
}
#end
Sidenote: I had to make the helper class a subclass from SenTestCase to being able to access the assertion macros.
The assertion from the helper class is ignored. Any ideas why? How can I use assertions in helper classes?
I had this same problem today and came up with a hack that worked for my purposes. Poking into the SenTestCase macros, I noticed that they call [self ...] on the helper but didn't trigger the asserts. So, wiring up the source class to the helper got it working for me. Changes to your question classes would look like:
MainTests.h
#import <SenTestingKit/SenTestingKit.h>
#interface MainTests : SenTestCase
#end
MainTests.m
#import "MainTests.h"
#import "HelperClass.h"
#implementation MainTests
- (void)testExample {
// Changed init call to pass self to helper
HelperClass *helperClass = [[HelperClass alloc] initFrom:self];
[helperClass fail];
}
#end
HelperClass.h
#import <SenTestingKit/SenTestingKit.h>
#interface HelperClass : SenTestCase
- (id)initFrom:(SenTestCase *)elsewhere;
- (void)fail;
#property (nonatomic, strong) SenTestCase* from;
#end
HelperClass.m
#import "HelperClass.h"
#implementation HelperClass
#synthesize from;
- (id)initFrom:(SenTestCase *)elsewhere
{
self = [super init];
if (self) {
self.from = elsewhere;
}
return self;
}
- (void)fail {
STFail(#"This should fail");
}
// Override failWithException: to use the source test and not self
- (void) failWithException:(NSException *) anException {
[self.from failWithException:anException];
}
#end
It is entirely possible that additional overrides are needed for more advanced functionality, but this did the trick for me.