Erreur lors du test d'un modèle Mantle sur iOS
-
21-12-2019 - |
Question
Étant donné la classe de modèle simple suivante (basée sur Mantle) :
// .h
#import <Mantle.h>
@interface JAIInterestingPhonesCategory : MTLModel <MTLJSONSerializing>
@property (copy, nonatomic, readonly) NSString *categoryId;
@property (copy, nonatomic, readonly) NSString *title;
@end
// .m
#import "JAIInterestingPhonesCategory.h"
@implementation JAIInterestingPhonesCategory
+ (NSDictionary *)JSONKeyPathsByPropertyKey
{
return @{
@"categoryId" : @"id",
};
}
@end
Je crée le TestCase suivant :
#import <XCTest/XCTest.h>
#import "JAIInterestingPhonesCategory.h"
@interface JAIInterestinPhoneTestCase : XCTestCase
@end
@implementation JAIInterestinPhoneTestCase
- (void)setUp
{
[super setUp];
// Put setup code here. This method is called before the invocation of each test method in the class.
}
- (void)tearDown
{
// Put teardown code here. This method is called after the invocation of each test method in the class.
[super tearDown];
}
- (void)testCreateModelWithJSONDictionary
{
NSDictionary *JSONModelDictionary = @{
@"id" : @"catId",
@"title" : @"Category title"
};
NSError *error;
JAIInterestingPhonesCategory *category = [MTLJSONAdapter modelOfClass:[JAIInterestingPhonesCategory class] fromJSONDictionary:JSONModelDictionary error:&error];
XCTAssertNotNil(category, @"The instantiated category must not be nil");
}
@end
Et j'obtiens l'erreur d'exécution suivante :
*** Assertion failure in -[MTLJSONAdapter initWithJSONDictionary:modelClass:error:]
Invalid parameter not satisfying: [modelClass isSubclassOfClass:MTLModel.class]
Et comme vous pouvez le constater, modelClass
(alias. JAIInterestingPhonesCategory
) est une sous-classe de MTLModel
.
J'ai ajouté Mantle au projet en tant que Pod.
Une idée de ce qui se passe ici ?Merci!!!
La solution
Le problème ici est que Mantle est injecté à la fois dans vos tests et dans votre cible principale.
Une solution ici consiste à modifier votre fichier pod en conséquence :
platform :ios, '7.0'
target :app do
pod 'Mantle', '~> 1.4'
end
target :appTests do
pod 'Expecta', '~> 0.3'
end
Vérifier https://github.com/Mantle/Mantle/issues/217 pour plus d'informations.Bonne chance!