一个单元测试影响另一个

时间:2013-11-04 07:19:40

标签: ios objective-c unit-testing xctest

我正在定义两个函数:

mkgeometry_additions.h

#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>

MKMapRect MKMapRectForCoordinateRegion(MKCoordinateRegion region);

MKCoordinateRegion MKCoordinateRegionForSouthWestAndNorthEast(CLLocationCoordinate2D sw, CLLocationCoordinate2D ne);

mkgeometry_additions.m (.m,即使它们是c函数)

MKMapRect MKMapRectForCoordinateRegion(MKCoordinateRegion region)
{
    CLLocationCoordinate2D center = region.center;
    MKCoordinateSpan span = region.span;
    CLLocationCoordinate2D topLeft =
        CLLocationCoordinate2DMake(center.latitude - span.latitudeDelta / 2.0,
                                   center.longitude - span.longitudeDelta / 2.0);
    CLLocationCoordinate2D bottomRight =
    CLLocationCoordinate2DMake(center.latitude - span.latitudeDelta / 2.0,
                               center.longitude - span.longitudeDelta / 2.0);
    MKMapPoint mapPointTopLeft = MKMapPointForCoordinate(topLeft);
    MKMapPoint mapPointBottomRight = MKMapPointForCoordinate(bottomRight);
    double width = mapPointTopLeft.x - mapPointBottomRight.x;
    double height = mapPointTopLeft.y - mapPointBottomRight.y;

    return MKMapRectMake(mapPointTopLeft.x, mapPointTopLeft.y, width, height);
}

MKCoordinateRegion MKCoordinateRegionForSouthWestAndNorthEast(CLLocationCoordinate2D sw, CLLocationCoordinate2D ne)
{
    MKCoordinateSpan span =
        MKCoordinateSpanMake(ne.latitude - sw.latitude, ne.longitude - sw.longitude);
    CLLocationCoordinate2D topLeft = CLLocationCoordinate2DMake(ne.latitude, sw.longitude);
    CLLocationCoordinate2D center = CLLocationCoordinate2DMake(topLeft.latitude + span.latitudeDelta * 0.5, topLeft.longitude + span.longitudeDelta * 0.5);
    return MKCoordinateRegionMake(center, span);
}

这几乎就是两个文件的全部内容。

在我使用XCTests的单元测试中:

- (void)testMKMapRectForCoordinateRegionAfterReverseConversion
{
    CLLocationCoordinate2D fremont = CLLocationCoordinate2DMake(37.54827, -121.98857);
    MKCoordinateRegion region = MKCoordinateRegionMake(fremont, MKCoordinateSpanMake(0.05, 0.05));
    MKMapRect mapRect = MKMapRectForCoordinateRegion(region);
    MKCoordinateRegion derivedRegion = MKCoordinateRegionForMapRect(mapRect);

    double accuracy = 0.0001;

    XCTAssertEqualWithAccuracy(region.center.latitude, derivedRegion.center.latitude,
                               accuracy, @"Latitude is equal");
    XCTAssertEqualWithAccuracy(region.center.longitude, derivedRegion.center.longitude,
                               accuracy, @"Latitude is equal");
    XCTAssertEqualWithAccuracy(region.span.latitudeDelta, derivedRegion.span.latitudeDelta,
                               accuracy, @"Latitude delta is equal");
    XCTAssertEqualWithAccuracy(region.span.longitudeDelta, derivedRegion.span.longitudeDelta,
                               accuracy, @"Latitude delta is equal");
}

- (void)testMKCoordinateRegionForSouthWestAndNorthEast
{
    CLLocationCoordinate2D taipeiWanHua = CLLocationCoordinate2DMake(25.02946, 121.49652);
    CLLocationCoordinate2D taipeiSongShan = CLLocationCoordinate2DMake(25.06640, 121.56166);

    /* When the following line is called, the first test fails */
    MKCoordinateRegionForSouthWestAndNorthEast(taipeiWanHua, taipeiSongShan);

   /* I have the rest of the lines in this test commented out */
}

现在,我对此测试类进行了空设置和拆卸。当我运行测试。如果第二次测试的第三行在测试中,则第一次测试失败。仔细观察会发现,当它失败时,值仍然是相似但超出给定精度: enter image description here

如果我评论第二次测试的第三行,第一次测试就会通过。那是为什么?

1 个答案:

答案 0 :(得分:0)

问题不在于单元测试 - 对我而言,即使我完全注释掉第二个测试,它仍然失败 - 它与MKMapRectForCoordinateRegion实现有关。 我认为正确的实施是:

MKMapRect MKMapRectForCoordinateRegion(MKCoordinateRegion region)
{
    CLLocationCoordinate2D center = region.center;
    MKCoordinateSpan span = region.span;
    CLLocationCoordinate2D topLeft =
    CLLocationCoordinate2DMake(center.latitude + span.latitudeDelta / 2.0,
                               center.longitude - span.longitudeDelta / 2.0);
    CLLocationCoordinate2D bottomRight =
    CLLocationCoordinate2DMake(center.latitude - span.latitudeDelta / 2.0,
                               center.longitude + span.longitudeDelta / 2.0);
    MKMapPoint mapPointTopLeft = MKMapPointForCoordinate(topLeft);
    MKMapPoint mapPointBottomRight = MKMapPointForCoordinate(bottomRight);
    double width = mapPointBottomRight.x - mapPointTopLeft.x;
    double height = mapPointBottomRight.y - mapPointTopLeft.y;

    MKMapRect ret = MKMapRectMake(mapPointTopLeft.x, mapPointTopLeft.y, width, height);
    return ret;
}

看看这个:Mercator Projection