iOSでマントルモデルをテストするエラー
-
21-12-2019 - |
質問
以下の単純なモデルクラス(マントルに基づく):
// .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
.
次のテストケースを作成します。
#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
(a.k.a.JAIInterestingPhonesCategory
)はMTLModel
のサブクラスです。
PODとしてプロジェクトにマントルを追加しています。
ここで何が起こっているのかという考えは?ありがとうございました!!!
解決
問題は、マントルがあなたのテストとあなたの主なターゲットの両方に注入されるということです。
解決策は、それに応じてあなたのPodfileを変更することです:
platform :ios, '7.0'
target :app do
pod 'Mantle', '~> 1.4'
end
target :appTests do
pod 'Expecta', '~> 0.3'
end
.
chref="https://github.com/mantle/mantle/issues/217" rel="nofollow"> https://github.com/mantle/mantle/issues/217 詳細については。 頑張って!
所属していません StackOverflow