2016-05-13 57 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];