【问题标题】:How can I unit test infoDictionary behavior如何对 infoDictionary 行为进行单元测试
【发布时间】:2014-03-04 12:02:29
【问题描述】:

假设我的 SUT(被测系统)需要在属性 bundleIdentifier 中提供来自 infoDictionary 的 @"CFBundleIdentifier"。

- (void)testAppPackageName
{
    //fixture code is happening in setUp function
    XCTAssertEqualObjects(sut.bundleIdentifier, [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleIdentifier"]);
}

问题是[[NSBundle mainBundle] infoDictionary]在测试环境中返回一个空的NSDictionary,这意味着我在这里测试nil等于nil。 还有,这个测试有味道

你会如何测试它?

【问题讨论】:

    标签: objective-c unit-testing mocking tdd


    【解决方案1】:

    我现在使用的解决方案是在构造函数中将 inforDictionary 注入到我的 sut 中

    - (void)testAppPackageName
    {
       //fixture
       NSDictionary* appInfo = @{  @"CFBundleIdentifier" :  BUNDLE_IDENTIFIER};
       sut = [[SomeClass alloc] initWithInfo:appInfo];
    
       XCTAssertEqualObjects(sut.bundleIdentifier, BUNDLE_IDENTIFIER);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-01-08
      • 2019-11-02
      • 1970-01-01
      • 1970-01-01
      • 2013-09-30
      • 2019-01-05
      • 2011-03-16
      • 2013-01-02
      相关资源
      最近更新 更多