Objective-C协议在XCTest中的选择器失败,但在应用程序中没有



我遇到了一个奇怪的问题,对协议的调用因选择器无法识别而失败,但同样的代码在应用程序中运行良好。有人知道为什么会发生这种事吗?以下是我正在使用的内容。

MKPolygon+PointInPolygon.h

@import Foundation;
@import MapKit;
@interface MKPolygon (PointInPolygon)
- (BOOL)containsCoordinate:(CLLocationCoordinate2D)coordinate;
@end

MKPolygon+PointInPolygon.m

#import "MKPolygon+PointInPolygon.h"
@implementation MKPolygon (PointInPolygon)
- (BOOL)containsCoordinate:(CLLocationCoordinate2D)coordinate
{
    MKMapPoint mapPoint = MKMapPointForCoordinate(coordinate);
    MKPolygonRenderer *polygonView = [[MKPolygonRenderer alloc] initWithPolygon:self];
    CGPoint polygonViewPoint = [polygonView pointForMapPoint:mapPoint];
    BOOL returnResult = CGPathContainsPoint(polygonView.path, NULL, polygonViewPoint, NO);
    return returnResult;
}
@end

MKPolygon+PointInPolygonTests.m

#import <XCTest/XCTest.h>
#import "MKPolygon+PointInPolygon.h"
@interface MKPolygon_PointInPolygonTests : XCTestCase
@end
@implementation MKPolygon_PointInPolygonTests
- (void)testThatCoordinateIsNotInPolygon {
        //Canton
        CLLocationCoordinate2D outsidePolygonLocation = CLLocationCoordinate2DMake(40.798946, -81.378448);
        //Columbus Bounds
        CLLocationCoordinate2D nw = CLLocationCoordinate2DMake(40.0, -83.0);
        CLLocationCoordinate2D ne = CLLocationCoordinate2DMake(40.0, -82.0);
        CLLocationCoordinate2D se = CLLocationCoordinate2DMake(39.0, -82.0);
        CLLocationCoordinate2D sw = CLLocationCoordinate2DMake(39.0, -83.0);
        CLLocationCoordinate2D coords[] = {nw, ne, se, sw};
        MKPolygon *polygon = [MKPolygon polygonWithCoordinates:coords count:4];
        BOOL result = [polygon  containsCoordinate:outsidePolygonLocation];
        XCTAssertFalse(result);
}

对[poligon containsCordinate:outsidePolygonLocation]的调用导致无法识别的选择器错误。

如果我把上面的测试方法代码复制到应用程序中——没有XCTAssertion——它会很好地工作。

我不知道发生了什么事。我让其他一些人看了这个,他们也没有发现任何错误。

您不应该将生产代码添加到测试目标中。

相反,在Other Linker Flags中声明-ObjC,以便它拾取类别方法实现。有关更多信息,请参阅静态库中的Objective-C类别

如果设置正确,测试目标可以访问生产代码中的所有内容。

最新更新