iOS에서 Mantle 모델을 테스트하는 중에 오류가 발생했습니다.
-
21-12-2019 - |
문제
다음과 같은 간단한 모델 클래스(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
다음 TestCase를 만듭니다.
#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
그리고 다음과 같은 런타임 오류가 발생합니다.
*** Assertion failure in -[MTLJSONAdapter initWithJSONDictionary:modelClass:error:]
Invalid parameter not satisfying: [modelClass isSubclassOfClass:MTLModel.class]
그리고 보시다시피, modelClass
(일명 JAIInterestingPhonesCategory
)는 MTLModel
.
맨틀을 프로젝트에 포드로 추가했습니다.
여기서 무슨 일이 일어나고 있는지 아시나요?감사합니다!!!
해결책
여기서 문제는 맨틀이 테스트와 주요 목표 모두에 주입된다는 것입니다.
여기서 해결책은 그에 따라 Podfile을 변경하는 것입니다.
platform :ios, '7.0'
target :app do
pod 'Mantle', '~> 1.4'
end
target :appTests do
pod 'Expecta', '~> 0.3'
end
확인하다 https://github.com/Mantle/Mantle/issues/217 더 많은 정보를 위해서.행운을 빌어요!
제휴하지 않습니다 StackOverflow