【问题标题】:How to stub out property on AppDelegate using ocmock?如何使用 ocmock 存根 AppDelegate 上的属性?
【发布时间】:2012-06-20 11:40:00
【问题描述】:

在我的应用委托类中,我有一个简单的属性

@property (strong, nonatomic) LoginAppDelegate *loginAppDelegate;

然后,我在这里为所有登录视图卸载类似应用程序委托的功能,这样我就可以保持我的主应用程序委托类较小(ish)

然后在登录应用委托上,我有一种方法可以在主导航控制器上推送视图控制器

- (void)launchSomeOtherViewController {} 

痛苦的部分是当我在调用这个“启动”方法的视图控制器中时

- (void)callBackAfterSomeHttpMethodLetsSay
{
    [self.appDelegate.loginAppDelegate launchSomeOtherViewController];
}

当我尝试对此进行模拟时,似乎我在应用程序委托上的存根不正确

- (void)testCallBackWithSignupTokenInvokesLaunchCompleteSignupViewControllerWithToken
{
    id mockLoginAppDelegate = [OCMockObject mockForClass:[LoginAppDelegate class]];
    id mockAppDelegate = [OCMockObject mockForClass:[AppDelegate class]];
    [[[mockAppDelegate stub] andReturn:mockLoginAppDelegate] loginAppDelegate];
    [[mockLoginAppDelegate expect] launchSomeOtherViewController];
    [self.sut callBackAfterSomeHttpMethodLetsSay];
    [mockLoginAppDelegate verify];
}

我通过 ocunit 运行时的错误是通常的“未调用预期方法”

所以我的问题与我存根的方式有关 - 我可以做一个存根来返回登录模拟,还是我需要手动进入 getter?

【问题讨论】:

    标签: objective-c ocmock


    【解决方案1】:

    您似乎没有让您的mockAppDelegate 对被测类可见。尝试添加:

    [self.sut setAppDelegate:mockAppDelegate];
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多