2016-05-13 52 views
0

尋找對使用Kiwi只讀屬性沒有多大幫助。 總之,我想測試_myReadOnlyDict是否被初始化。 問題是myReadOnlyDict仍然是空的(沒有內容),儘管在beforeEach塊它被嘲笑和增加一個值。如何使用Kiwi存儲只讀屬性,Objective-C

// All these return 0 
    ad.myReadOnlyDict.count; 
    [[ad myReadOnlyDict] allKeys].count; 
    [ad myReadOnlyDict].count; 

我在這裏失蹤了什麼?

任何幫助表示讚賞!

請參閱下面的代碼:

在AppDelegate.h我有一個porperty。

@property (nonatomic, readonly) NSDictionary * myReadOnlyDict; 

在AppDelegate.m我有一個方法,這是從AppDelegate中的didFinishLaunchingWithOptions方法調用。

- (void)initConfig 
{ 
    _myReadOnlyDict = [NSJSONSerialization JSONObjectWithData:[self jsonData] options:nil error:nil]; 
} 

我有一個獼猴桃試驗建立

describe(@"App Delegate", ^{ 

    __block AppDelegate *ad; 

    beforeEach(^{ 
     ad = [[AppDelegate alloc] init]; 

     NSMutableDictionary * mockedDict = [NSJSONSerialization nullMock]; 

     mockedDict[@"my_data"] = @"my_value"; 

     [ad stub:@selector(myReadOnlyDict) andReturn:mockedDict]; 
    }); 

    afterEach(^{ 
     ad = nil; 
    }); 

    context(@"when smth happens", ^{ 
     it(@"should do smth else", ^{ 
      [[ad should] receive:@selector(initConfig)]; 

      // These three lines fail 
      [[theValue([ad myReadOnlyDict].count) shouldNot] equal:theValue(0)]; 
      [[theValue(ad.myReadOnlyDict.count) shouldNot] equal:theValue(0)]; 
      [[theValue([[ad myReadOnlyDict] allKeys].count) shouldNot] equal:theValue(0)]; 

      [ad initConfig]; 
     }); 
    }); 
}); 

回答

0

您的問題是由事實[NSJSONSerialization nullMock]返回一個空的模擬,這是一個什麼也不做任何調用的方法的對象,並返回nil造成/ 0給任何必須返回的方法。

這應該工作:

NSMutableDictionary * mockedDict = [@{@"my_data": @"my_value"} mutableCopy]; 
[ad stub:@selector(myReadOnlyDict) andReturn:mockedDict];