【问题标题】:not getting the values in xctest没有得到 xctest 中的值
【发布时间】:2023-03-04 13:23:01
【问题描述】:

我已将一个名为按钮状态的方法声明为-(int)buttonstate;

xctest.m 中,当我实例化我的 viewController 并分配给一个 int 值时,我得到了错误的值。

-(int) buttonstate {

    if ([searchButton isEnabled]) {
        j = 1;
    }
    else
        j = 0;

    NSLog(@" j value is %d",j);

    return j;
}

在 xctest .m 文件中

-(void) testwithoutData {
    myapiViewController  *apiViw = [[myapiViewController alloc]init];


    int kn  = [apiViw buttonstate];

    NSLog(@"the value is %ld",(long)kn);        
}

【问题讨论】:

  • 两种方法在同一个视图中?并且您想在其他控制器中传递状态值?
  • 如果 UITextField 包含文本,则 uiButton 将启用并且值为 1,我需要 xctest.m 文件中的 j 值如果我在分配视图之前运行测试,我将值设为 0当我在 (int kn ) 中看到它是 0 之后,该值为 1
  • 您必须在 testwithoutData 中启用 serachButton,然后才能在运行测试用例时调用 buttonstate 以查看值 1。
  • -(void) textdata { long i = search.text.length;在我运行应用程序时该值为 1,但在 testwithoutData 中,如果 (i > 0) { searchButton.enabled = YES; } else { searchButton.enabled = NO; } [自我按钮状态]; } -(int) buttonstate { if ([searchButton isEnabled]) { j = 1; } 否则 j = 0; NSLog(@" j 值为 %d",j);返回 j; } 我有 textfield.text = @"300";

标签: ios iphone ios6 xcode6 xctest


【解决方案1】:

这是我尝试过的-

在 ViewController.h 中

@interface ViewController : UIViewController{
    @public int index;
}

在 ViewController.m 中

-(void) drawCircle{
    index = 10;
    NSLog(@"the value is %ld",(long)index);
}

在 ViewControllerTests.m 中

@interface ViewControllerTests : XCTestCase
@property (nonatomic,strong) ViewController *vcToTest;
@end

@implementation ViewControllerTests

- (void)setUp {
    [super setUp];
    // Put setup code here. This method is called before the invocation of each test method in the class.
    self.vcToTest = [[ViewController alloc] init];
}

- (void)testExample {
    // This is an example of a functional test case.
    XCTAssert(YES, @"Pass");
    [self.vcToTest drawCircle];
    NSLog(@"Test is fine. Value %d",self.vcToTest->index);
}

测试结果 -

【讨论】:

  • 当我在调用 drawCircle 方法之前记录索引值时,我得到的索引值为 0。
  • 请详细说明以及如何获得10
  • 为了显示我在 ViewControllerTests 的 drawCircle 方法中设置的索引值,我在 testExample 中记录索引值之前调用了该方法。我尝试不调用drawCircle,但那时我得到了索引值0。当我调试时,我发现ViewController的实例发生了变化,索引值从10变为0。我在ViewController viewDidLoad中调用了drawCircle方法。所以我建议你先调用 searchbutton enable 方法,然后尝试检查你想要打印的值。希望这可以帮助。检查附加的测试结果截图。
  • 我已经调用了该方法,但值本身仍然是 0
  • 搜索按钮在您调用后启用是或否
猜你喜欢
  • 1970-01-01
  • 2016-06-27
  • 1970-01-01
  • 1970-01-01
  • 2013-11-17
  • 2018-01-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多