سؤال

أحاول اختبار أن خريطتي تعرض معلمات الموقع التي أعطيها. أنا أستخدم NIB لوحدة التحكم الخاصة بي (أنا أكره القصص القصرية).

المشكلة التي واجهتها هي أن خريطتي لا شيء في اختباري. تتم تهيئة الخريطة في NIB ولا يتم تهيئتها في الكود الحالي. حاولت تهيئة القرب المنقول الخاص بي باستخدام ViewController الخاص بي ، لكن هذا لا يبدو أنه يعمل في XCTest.

#import <XCTest/XCTest.h>
#include "HomeVC.h"

@interface homeVCTests : XCTestCase {
    HomeVC* homeVC;
}

@end

@implementation homeVCTests

- (void)setUp
{
    [super setUp];

    homeVC = [[HomeVC alloc] initWithNibName:@"HomeVC" bundle:nil];
    [homeVC viewDidLoad];
}

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

- (void)testHomeHasMap // Fails
{
    XCTAssertNotNil(homeVC.map, @"Map is not nil.");
}

- (void)testHomeHasMapTypeHybrid // Fails
{
    XCTAssertEqual(homeVC.map.mapType, MKMapTypeHybrid, @"Map type is hybrid(2)");
}

- (void)testHomeHasMapWithMyLocation // Fails
{
    [homeVC setMapCurrentLocation:37.785834 lon:-122.406417];
    CLLocationCoordinate2D checkpoint = CLLocationCoordinate2DMake(37.785834, -122.406417);
    XCTAssertEqual(homeVC.map.centerCoordinate.latitude, checkpoint.latitude, @"Map shows my location.");
}

@end

HOMEVC.H

#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
#import <CoreLocation/CoreLocation.h>
#import "AppDelegate.h"

@class User;
@class LoginRegisterVC;

@interface HomeVC : UIViewController <CLLocationManagerDelegate, MKMapViewDelegate> {
IBOutlet MKMapView* map;
CLLocationManager* locationManager;
CLLocationCoordinate2D currentLocation;
LoginRegisterVC* loginRegVC;

User* user;

}
@property(nonatomic, strong)LoginRegisterVC* loginRegVC;
@property(nonatomic, strong)User* user;
@property(nonatomic, strong)IBOutlet MKMapView* map;
@property(nonatomic, strong)CLLocationManager* locationManager;
@property(nonatomic)CLLocationCoordinate2D currentLocation;


-(void)setMapCurrentLocation:(float)lat lon:(float)lon;
@end

HOMEVC.M

#import "HomeVC.h"
#import "User.h"
#import "LoginRegisterVC.h"

#define METERS_PER_MILE 1609.344

@interface HomeVC ()

@end

@implementation HomeVC
@synthesize map, locationManager, loginRegVC, user;
@synthesize currentLocation = _currentLocation;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}

- (void)viewDidLoad
{
[super viewDidLoad];

//self.map = MKMapView.new;
self.map.mapType = MKMapTypeHybrid;
self.map.delegate = self;

//NSLog(@"%lu", self.map.mapType);

[self startStandardUpdates];

[self setMapCurrentLocation:self.locationManager.location.coordinate.latitude
lon:self.locationManager.location.coordinate.longitude];

}

- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

-(void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:true];

if (self.user.loggedIn==false) {
self.loginRegVC = LoginRegisterVC.new;
self.loginRegVC.user = self.user;
[self presentViewController:self.loginRegVC animated:true completion:nil];
}
}

- (void)startStandardUpdates
{
if (nil == self.locationManager)
self.locationManager = [[CLLocationManager alloc] init];

self.locationManager.delegate = self;
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;

self.locationManager.distanceFilter = 5; // meters

[self.locationManager startUpdatingLocation];
}

- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation
{
NSLog(@"%f, %f", newLocation.coordinate.latitude, newLocation.coordinate.longitude);
[self setMapCurrentLocation:newLocation.coordinate.latitude lon:newLocation.coordinate.longitude];
}

- (void)locationManager:(CLLocationManager *)manager monitoringDidFailForRegion:(CLRegion *)region withError:(NSError *)error
{

}

-(void)setMapCurrentLocation:(float)lat lon:(float)lon
{
self.currentLocation = CLLocationCoordinate2DMake(lat, lon);
MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(self.currentLocation,
0.5*METERS_PER_MILE,
0.5*METERS_PER_MILE);
[self.map setRegion:region animated:true];
NSLog(@"map center: %f, %f", self.map.centerCoordinate.latitude, self.map.centerCoordinate.longitude);
}
@end
هل كانت مفيدة؟

المحلول

من المستحيل إجراء اختبارات الوحدة مثل هذه لوحدات التحكم التي تنفذ الطرق غير المتزامنة. يتم تنفيذ طرق الاختبار الخاصة بك على الفور حتى لا تتمكن من رؤية النتائج. يمكنك استخدام Semaphores أو تقنيات أخرى لإيقاف تشغيل Runloop حتى تلبي الولايات متطلباتك.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top