Il test delle unità è solo qualcosa che non riesco mai a capire, ma posso capire perché è importante e può essere un enorme risparmio di tempo (se sai cosa sei tu sta facendo). Spero che qualcuno possa indicarmi la giusta direzione.Come testare un UIViewController - TDD/BDD
Ho il seguente UIViewController
QBElectricityBaseVC.h
@interface QBElectricityBaseVC : QBStateVC
@property (nonatomic, strong) QBElectricityUsage *electricityUsage;
@property (nonatomic, assign) CGFloat tabBarHeight;
- (void)updateElectricityUsage;
@end
QBElectricityBaseVC.m
@implementation QBElectricityBaseVC
- (instancetype)init
{
self = [super init];
if (self) {
self.tabBarItem = [[UITabBarItem alloc] initWithTitle:NSLocalizedString(@"electricity_title", nil) image:nil tag:0];
}
return self;
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[self.notificationCenter addObserver:self selector:@selector(updateElectricityUsage)
name:kUpdatedElectricityUsageKey object:nil];
}
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
[self.notificationCenter removeObserver:self];
}
- (void)updateElectricityUsage
{
self.electricityUsage = [self.stateManager electricityUsage];
}
- (CGFloat)tabBarHeight
{
return self.tabBarController.tabBar.frame.size.height;
}
@end
Cosa devo sottoporre a test?
- Un osservatore per
kUpdatedElectricityUsageKey
si aggiunge self.electricityUsage
diventa un'istanza diQBElectricityUsage
- Un
tabBarHeight
viene restituito - Un osservatore per
kUpdatedElectricityUsageKey
viene rimosso
mi sto perdendo qualcosa che dovrei testare o testare qualcosa che davvero non dovrei?
Come si esegue il test?
Quindi sto cercando di farlo utilizzando Specta e Expexta. Se devo prendere in giro qualcosa, utilizzerei OCMockito.
Non so davvero come testare l'osservatore aggiunto/rimosso. Vedo il seguente nella documentazione Expexta ma non è sicuro se il suo rilevante/come usarlo:
expect(^{ /* code */ }).to.notify(@"NotificationName"); passes if a given block of code generates an NSNotification named NotificationName.
expect(^{ /* code */ }).to.notify(notification); passes if a given block of code generates an NSNotification equal to the passed notification.
Per verificare che self.electricityUsage
diventa un'istanza di QBElectricityUsage
ho potuto creare una categoria che ha un metodo che fa solo finta la notifica licenziato e chiama il metodo updateElectricityUsage
ma è questo il modo migliore?
E per quanto riguarda lo tabBarHeight
, dovrei semplicemente provare che restituisce un valore valido CGFloat
e non preoccuparti di quale sia il valore?
UPDATE
ho cambiato il mio metodo viewWillAppear
a guardare come di seguito:
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[self addNotificationObservers];
}
- (void)addNotificationObservers
{
[self.notificationCenter addObserver:self selector:@selector(updateElectricityUsage)
name:kUpdatedElectricityUsageKey object:nil];
}
E poi ho creato il seguente test:
#import "Specs.h"
#import "QBElectricityBaseVC.h"
#import "ElectricityConstants.h"
SpecBegin(QBElectricityBaseVCSpec)
describe(@"QBElectricityBaseVC", ^{
__block QBElectricityBaseVC *electricityBaseVC;
__block NSNotificationCenter *mockNotificationCenter;
beforeEach(^{
electricityBaseVC = [QBElectricityBaseVC new];
mockNotificationCenter = mock([NSNotificationCenter class]);
electricityBaseVC.notificationCenter = mockNotificationCenter;
});
afterEach(^{
electricityBaseVC = nil;
mockNotificationCenter = nil;
});
it(@"should have a notification observer for updated electricity usage", ^{
[electricityBaseVC addNotificationObservers];
[verify(mockNotificationCenter) addObserver:electricityBaseVC selector:@selector(updateElectricityUsage)
name:kUpdatedElectricityUsageKey object:nil];
});
});
SpecEnd
Questo test ora passa ma è questo il modo corretto/migliore per prova questo?
Ho le stesse identiche domande - vorrei che ci fosse più documentazione là fuori. Inoltre, perché non è andato con XCTest? – dccarmo