ARC
아래에 weak
속성 인 parent
이있는 Child
개체가 있습니다. Child
에 대한 몇 가지 테스트를 작성하려고하고 있는데 OCMock
을 사용하여 parent
속성을 조롱하고 있습니다.약한 속성을 사용하여 NSProxy 서브 클래스 집합을 초기화하는 것을 중지하도록 ARC에서 OCMock을 얻으려면 어떻게해야합니까?
ARC에서 합성 된 약한 속성 설정자를 사용하여 NSProxy
하위 클래스를 설정하면 약한 속성을 설정 한 후 속성 ... 행을 설정하지 않고 이미 nil
임을 알 수 있습니다. 여기에 구체적인 예입니다 : 나는 내가 Parent
를 참조 할 Child
에 대한 weak
특성 대신 assign
속성을 사용하여이 문제를 해결받을 수 있다는 사실을 알고
@interface Child : NSObject
@property (nonatomic, weak) id <ParentInterface>parent;
@end
@implementation Child
@synthesize parent = parent_;
@end
// ... later, inside a test class ...
- (void)testParentExists
{
// `mockForProtocol` returns an `NSProxy` subclass
//
OCMockObject *aParent = [OCMockObject mockForProtocol:@protocol(ParentInterface)];
assertThat(aParent, notNilValue());
// `Child` is the class under test
//
Child *child = [[Child alloc] init];
assertThat(child, notNilValue());
assertThat(child.parent, nilValue());
child.parent = (id<ParentInterface>)aParent;
assertThat([child parent], notNilValue()); // <-- This assertion fails
[aParent self]; // <-- Added this reference just to ensure `aParent` was valid until the end of the test.
}
, 그러나 나는 parent
에서 nil
해야 할 것이다 때 (일종의 원시인과 같이) ARC가 제거해야했던 것과 정확히 일치하는 방식으로 수행되었습니다.
내 앱 코드를 변경하지 않고이 테스트를 통과하는 방법에 대한 제안이 있으십니까?
편집 : 그것은 내가 aParent
이 NSObject
의 인스턴스 만들 경우 OCMockObject
이 NSProxy
인으로해야 할 것으로 보인다는 child.parent
약한 참조는 nil이 아닌 값을 "보유". 앱 코드를 변경하지 않고이 테스트를 통과 할 수있는 방법을 찾고 있습니다.
편집 2 : 블레이크의 답변을 수락 한 후 약식 -> 할당에서 조건부로 속성을 변경 한 전처리 매크로의 프로젝트에서 구현을 수행했습니다. 귀하의 마일리지는 다를 수 있습니다 :
#if __has_feature(objc_arc)
#define BBE_WEAK_PROPERTY(type, name) @property (weak, nonatomic) type name
#else
#define BBE_WEAK_PROPERTY(type, name) @property (assign, nonatomic) type name
#endif
다음 커밋 메시지를 확인하십시오. https://github.com/erikdoe/ocmock/commit/dbdb233ae84498077f7e946abb49731968333f0b OCMock 팀이 동일한 문제를 겪고있는 것처럼 보입니다. – Don