Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
iOS单元测试实例变量_Ios_Unit Testing - Fatal编程技术网

iOS单元测试实例变量

iOS单元测试实例变量,ios,unit-testing,Ios,Unit Testing,我是iOS开发新手,我正在尝试为我的代码编写单元测试。我需要访问实例变量。这样做的唯一方法是创建一个类别,编写getter方法,然后将其导入到我的测试文件中吗 这是我的.m文件 //imports @implementation viewController{ NSArray* a; int b; //other variables } //methods 这是测试文件 #import "ViewController_Tests.h" #import "ViewCon

我是iOS开发新手,我正在尝试为我的代码编写单元测试。我需要访问实例变量。这样做的唯一方法是创建一个类别,编写getter方法,然后将其导入到我的测试文件中吗

这是我的.m文件

//imports
@implementation viewController{
    NSArray* a;
    int b;
    //other variables
}

//methods
这是测试文件

#import "ViewController_Tests.h"
#import "ViewController.h"

@implementation ViewController_Tests{
  ViewController *controller;
}

- (void)setUp {
  [super setUp];
  controller = [[ViewController alloc] initWithNibName:nil bundle:nil];
}

- (void)tearDown {
    [super tearDown];
}

- (void)test1 {
  NSArray* a;
  //I want to access the variables here!

} 

@end

我可以看到您可以使用的两种解决方案:

1:使用UISpec框架测试视图控制器的功能,例如“断言视图控制器有一个包含X个条目的tableview”(这是一个回归测试),并在具有完整GUI的模拟器中运行


2:使用预编译标记(如#ifdef UNIT_TESTS)打开对成员变量的访问或添加访问器方法,使用其他预处理器标记中的生成设置定义UNIT_TESTS,因此它仅在单元测试中编译。

请提供更多详细信息。实例变量属于哪个类,你的应用程序委托?它们是我创建的自定义类中的实例变量。在@implementation块中的.m文件中,我声明了我要访问的实例变量。您能在单元测试中创建该类的实例吗?请提供一个导致您出现问题的示例,我们可以看看。是的,我可以创建该类的实例,但我没有对实例变量的公共访问权限。