Why is my object's weak delegate property nil in my unit tests?

前端 未结 4 1088
既然无缘
既然无缘 2020-12-31 07:54

I have a pretty simple setup for this unit test. I have a class that has a delegate property:

@interface MyClass : NSObject
...
@property (nonatomic, weak) i         


        
4条回答
  •  没有蜡笔的小新
    2020-12-31 08:15

    A workaround is to use Partial Mocks.

    @interface TestMyDelegateProtocolDelegate : NSObject 
    @end
    
    @implementation TestMyDelegateProtocolDelegate
    - (void)someMethod {}
    @end
    
    
    @implementation SomeTest {
    - (void)testMyMethod_WithDelegate {
      id delegate = [[TestMyDelegateProtocolDelegate] alloc] init];
      id delegateMock = [OCMockObject partialMockForObject:delegate]
      [[[delegateMock expect] someMethod]
      myClassIvar.connectionDelegate = delegate;
      [myClass someOtherMethod];
      STAssertNoThrow([delegate verify], @"should have called someMethod on delegate.");
    }
    @end
    

提交回复
热议问题