【问题标题】:Class variable in XCTestCase in Objective-c?Objective-c中XCTestCase中的类变量?
【发布时间】:2014-04-04 16:52:45
【问题描述】:

这是我的代码:

#import <XCTest/XCTest.h>
#import "GTN_GameStatistics.h"


@interface GTN_GameStatisticsTest : XCTestCase

@end

@implementation GTN_GameStatisticsTest{
   GTN_GameStatistics * _gameStatistic;
}

+ (void)setUp
{
    [super setUp];
    // Put setup code here; it will be run once, before the first test case.

}

- (void)setUp
{
    [super setUp];
    // Put setup code here; it will be run once, before the first test case.

    _gameStatistic = [GTN_GameStatistics sharedManager]; 
}

- (void)tearDown
{
    // Put teardown code here; it will be run once, after the last test case.
    [super tearDown];

    [_gameStatistic resetStatistics];
}

有没有办法将代码从 - (void)setUp 放到 + (void)setUp
为此,我需要将GTN_GameStatistics * _gameStatistic 设为类变量而不是实例变量,但我不知道如何。
我尝试了几种方法,但没有奏效。
我可以不用它,但想知道它是否可能以及如何?

【问题讨论】:

  • 请仅将xcode 标记用于与IDE 本身相关的问题。

标签: ios objective-c cocoa unit-testing


【解决方案1】:

没有“类变量”之类的东西,但全局变量可以解决问题。在您的#imports 下方:

static GTN_GameStatistics * _gameStatistic;

然后在你的 +setup 方法中:

_gameStatistic = [GTN_GameStatistics sharedManager];

【讨论】:

  • C++有,所以我以为objective-c也有。
猜你喜欢
  • 2012-01-26
  • 1970-01-01
  • 2011-08-27
  • 1970-01-01
  • 2012-02-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多