1

介绍

我想测试在视图子视图(作为模拟注入)上设置了正确的框架。

但是我似乎无法找到一种方法来验证是否设置了正确的 CGRect

当前尝试

-(void)testThatCorrectFrameIsSet {
  //Arrange
  MKTArgumentCaptor *argumentCaptor = [[MKTargumentCaptor alloc] init];
  self.sut.subView = mock([UIView class]);

  //Act
  [self.sut awakeFromNib];

  //Assert
  [[[MKTVerify(self.sut.subView) withMatcher:[argumentCaptor capture]] setFrame:CGSizeZero];

  expect([argumentCaptor.value CGRectValue].size.x).to.beCloseToWithin(42, 0.001);
}

出了什么问题

我得到:

"NSInvalidArgumentException", "-[_NSInlineData CGRectValue]: unrecognized selector sent to instance 0x7af325c0"

这让我想知道如何正确捕获 CGRect 值

边注

我知道我可以注入一个实际的 UIView 而不是一个模拟的,但是我正在测试的实际代码阻止我这样做,因为这会使这个问题变得比需要的更难。

4

1 回答 1

1

我认为您应该使用“CGRectZero”而不是“CGSizeZero”,但这并不能解决问题。

看起来捕获者值不是 NSValue。

//Arrange
MKTArgumentCaptor *argumentCaptor = [[MKTArgumentCaptor alloc] init];
UIView* subView = mock([UIView class]);

// Act
[subView setFrame:CGRectMake(1, 2, 3, 4)];

//Assert
[[MKTVerify(subView) withMatcher:[argumentCaptor capture]] setFrame:CGRectZero];

NSValue* capturedValue = [argumentCaptor value];
if (![capturedValue isKindOfClass:[NSValue class]])
{
    NSLog(@"Captured value is no NSValue!");
}

希望这有助于追查问题。

于 2015-04-20T10:35:00.897 回答