RestKit RKMappingTest JSON 数组

2023-12-12

我正在使用 RestKit ~> 0.20.3 和 RestKit/Testing ~> 0.20.3。这是我的映射的一个示例:

RKObjectMapping *mapping = [RKObjectMapping mappingForClass:[PlayerVO class]];
[mapping addAttributeMappingsFromArray:@[@"firstName", @"middeName", @"lastName", @"dob", @"sex"]];

这是我的模拟数据:

NSDictionary *data = @{@"players": @[@{@"firstName": @"Ahmed", @"middleName": @"Ahmed", @"lastName": @"Ahmed", @"dob": @"100", @"sex": @"m"}]};

这是我的mappingTest:

RKMappingTest *mappingTest = [RKMappingTest testForMapping:mapping sourceObject:data destinationObject:nil];

最后我的expectation:

RKPropertyMappingTestExpectation *expectation = [RKPropertyMappingTestExpectation expectationWithSourceKeyPath:@"players.firstName" destinationKeyPath:@"firstName" evaluationBlock:^BOOL(RKPropertyMappingTestExpectation *expectation, RKPropertyMapping *mapping, id mappedValue, NSError *__autoreleasing *error) {
    BOOL expect = [mappedValue length] > 0;

    XCTAssertTrue(expect);

    return expect;
}];

[mappingTest addExpectation:expectation];

XCTAssertTrue([mappingTest evaluate]);
XCTAssertNoThrow([mappingTest verify]);

所以这个测试失败了,因为我似乎没有办法指定要遵循的关键路径data是一个数组。这是我得到的错误:

testPlayerServiceGetPlayers] : (([mappingTest verify]) does not throw) failed: throwing "0x8e7d770: failed with error: (null)
RKMappingTest Expectations: (
    "map 'players.firstName' to 'firstName' satisfying evaluation block"
)
Events: (
) during mapping from {
    players =     (
                {
            dob = 100;
            firstName = Ahmed;
            lastName = Ahmed;
            middleName = Ahmed;
            sex = m;
        }
    );
} to (null) with mapping <RKObjectMapping:0x8e6aad0 objectClass=PlayerVO propertyMappings=(
    "<RKAttributeMapping: 0x8e6b880 firstName => firstName>",
    "<RKAttributeMapping: 0x8e61df0 middeName => middeName>",
    "<RKAttributeMapping: 0x8e2f580 lastName => lastName>",
    "<RKAttributeMapping: 0x8e67a60 dob => dob>",
    "<RKAttributeMapping: 0x8e2c120 sex => sex>"
)>"

如果我将数据更改为:

NSDictionary *data = @{@"firstName": @"Ahmed", @"middleName": @"Naseer", @"lastName": @"Nuaman", @"dob": @"524534400", @"sex": @"m"};

And the expectation's expectationWithSourceKeyPath to @"firstName",测试通过。因此,这让我相信这个问题显然与设定关键路径有关。现在在我的应用程序中使用:

RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:mapping method:RKRequestMethodAny pathPattern:nil keyPath:@"players" statusCodes:statusCodes];

但是我找不到设置的方法keyPath for a RKMappingTest or RKPropertyMappingTestExpectation,关于如何实现这一目标有什么想法吗?

Update

所以我浏览了一下RKMappingTest.h并找到了rootKeyPath。我设置如下:

mappingTest.rootKeyPath = @"players";

并且仍然遇到问题mappedValue in the RKPropertyMappingTestExpectation。所以我也将我的数据更改为:

NSDictionary *data = @{@"players": @{@"firstName": @"Ahmed", @"middleName": @"Ahmed", @"lastName": @"Ahmed", @"dob": @"100", @"sex": @"m"}};

现在我可以看到mappedValue现在设置为Ahmed并更新了RKPropertyMappingTestExpectation至以下内容:

RKPropertyMappingTestExpectation *expectation = [RKPropertyMappingTestExpectation expectationWithSourceKeyPath:@"firstName" destinationKeyPath:@"firstName" evaluationBlock:^BOOL(RKPropertyMappingTestExpectation *expectation, RKPropertyMapping *mapping, id mappedValue, NSError *__autoreleasing *error) {
    return [mappedValue isEqualToString:@"Ahmed"];
}];

但这使用的是对象而不是数组。还有更多建议吗?


我想我找到了比“你测试错误”更好的答案。您应该能够通过将正在测试的映射包装在另一个映射中来正确测试。

// Data (in your case, you've used a hard coded string here)
id fixtureData = [RKTestFixture parsedObjectWithContentsOfFixture:@"fixture.json"];

// Setup
RKObjectMapping* wrapperMapping = [RKObjectMapping mappingForClass:[NSMutableDictionary class]];
RKObjectMapping *mappingToTest = [RKObjectMapping mappingForClass:[PlayerVO class]];
[mappingToTest addAttributeMappingsFromArray:@[@"firstName", @"middeName", @"lastName", @"dob", @"sex"]];
[wrapperMapping addRelationshipMappingWithSourceKeyPath:@"players" mapping:mappingToTest];
RKMappingTest* test = [RKMappingTest testForMapping:wrapperMapping sourceObject:fixtureData destinationObject:nil];

// If you're dealing with a managed object mapping, you'll need these:
// I usually set up a single store in the "+(void)setUp" method and reset it
// on each test in the "-(void)setUp" method. (note one method is class, one
// is instance).
//test.managedObjectContext = managedObjectStore.persistentStoreManagedObjectContext;
//test.mappingOperationDataSource = [[RKManagedObjectMappingOperationDataSource alloc] initWithManagedObjectContext:managedObjectStore.persistentStoreManagedObjectContext cache:[RKFetchRequestManagedObjectCache new]];

// Expectations
[test addExpectation:[RKPropertyMappingTestExpectation expectationWithSourceKeyPath:@"..." destinationKeyPath:@"..."]];
// Add others as you see fit (these will work for the mappingToTest mapping).

// Evaluate
XCTAssert([test evaluate]);
NSDictionary* result = test.destinationObject;
// You can verify counts on the result and any other objects.
// If you have variables in other structures you want tracked, you can expand
// on the wrapper mapping. i.e. If your data contained a "teamName" at the
// same level as your array of "players", you could add a mapping from
// "teamName" to an arbitrary keyPath for your dictionary then access it in the
// result variable.

这对我有用。我认为说不应该测试集合映射是短视的。您可能有一个设计不佳的 REST API,需要这样做,或者您想要验证在映射发生时是否正确设置的托管对象之间的复杂交互。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

RestKit RKMappingTest JSON 数组 的相关文章

随机推荐

  • 如何使vbscript弹出消息始终在最上面?

    任何人都知道如何使 vbscript 弹出消息 框 生成 在所有内容之上而不是在后台 这是我的脚本 Set objShell WScript CreateObject WScript Shell Const wshYes 6 Const w
  • Mac OS GCC 中为 C 定义了什么?

    所以我想知道从哪里可以获得纯 C 的默认 Mac OS GCC 定义列表 任何人都可以在这里发布列表吗 为什么我需要这样的东西 我没有 mac OS X 我尝试在 ubuntu 下用 alchemy GCC 编译 ffmpeg ffmpeg
  • Java中的声音问题

    我有一些关于在 Java 中播放声音的问题 希望你能帮助我 1 如何使用 停止 按钮停止正在播放的声音 2 如何减慢声音 或冷却时间 3 我想创建一个可以调节音量和静音选项的选项框 我该怎么做 这是我的代码 private void BGM
  • Java - 将用户输入分配给变量/更改计数器

    尽管我对 C 有相当基本的了解 但我对 java 还很陌生 在我的作业中 我计算零钱并将其分类为美国货币 即 如果您有 105 美分 它将分为 1 美元和 1 毛钱 从逻辑上讲 我明白如何做到这一点 但我在理解 java 语法时遇到了一些严
  • ASP.NET MVC 框架 4.5 CSS 捆绑包无法在托管上运行

    我正在应用程序港口上运行一个用 MVC 4 编写的应用程序 一堆 css 文件不起作用 在我的本地计算机的调试模式下 我看到了应用程序的代码 并且看到了文件 该应用程序按预期工作 当我将应用程序上传到 Appharbor 时 我在代码中看到
  • 如何向水平 LinearLayout 添加(垂直)分隔线?

    我正在尝试将分隔线添加到水平线性布局中 但一无所获 分隔线只是不显示 我是 Android 新手 这是我的布局 XML
  • 如何将 Firebase Cloud Functions 调度程序与实时数据库/分析触发器结合使用?

    我正在开发 Firebase 云功能 以发送触发的推送通知 现在 只要用户在我的应用程序中触发 IAP 事件 我的函数就会发送推送 use strict const functions require firebase functions
  • 如何从 NSString 中删除十六进制字符

    我面临一个与字符串中的某些十六进制值相关的问题 我需要从字符串中删除十六进制字符 The problem is when i print object it prints as BLANK line And in debug mode it
  • java中如何将十六进制字符串转换为long?

    我想在java中将十六进制字符串转换为长字符串 我尝试过一般转换 String s 4d0d08ada45f9dde1e99cad9 long l Long valueOf s longValue System out println l
  • 极限算子的关系代数

    这两个 SQL 查询的关系代数是什么 Select from film where film rating PG limit 20 我们如何显示极限 Select from actor country where first name BO
  • 通用数据库设计方法

    我面对客户的应用程序如下所示 它允许最终用户输入 材料 对于这些材料 他们可以附加任意数量的 属性 属性可以具有任何类型的值 decimal int dateTime 和 varchar 长度从 5 个字符到大块文本不等 本质上 架构如下所
  • ios,从视图获取指向控制器的指针

    我正在尝试制作一个 ios UI 元素 我可以将屏幕上的项目拖动到 tableView 上 tableView 将添加相应的元素 我使用 hitTest 函数来识别我将元素拖动到哪个视图 并且工作正常 我的问题是 当我需要指向 tableV
  • 如何在 android 2.2 模拟器或 android 2.1 设备中使用地理编码器

    我需要使用地理编码器从 google map am 的地址获取 lang 和 lat am 在 android sdk 2 2 中工作 使用地理编码器无法正常工作 它会抛出异常 如何在我的应用程序中使用地理编码器 或任何其他可用于获取经度和
  • 无法在 Linux 上的 SQL Server 2017 Express 上启用 xp_cmdshell

    我正在尝试启用xp cmdshell在 Linux RedHat 7 4 上运行的 SQL Server 2017 Express 上 我正在跟进本指南 并出现以下错误 此版本不支持指定的选项 xp cmdshell SQL Server
  • 如何将私有 pdb 转换为公共 pdb?

    我有私有 pdb 文件 我必须将其转换为公共文件 有工具吗 Use PDBCopy pdbcopy 是 Windows 调试工具的一部分 可通过视窗软件开发工具包
  • 发生错误时如何不让 Flask 服务器崩溃? [关闭]

    Closed 这个问题需要多问focused 目前不接受答案 我使用flask制作了一个API应用程序 它接受一个数字 十进制 作为输入并返回一些字符串 如果我发送一个字符串 这个应用程序就会中断 并且在重新启动后可以正常工作 我不想每次在
  • Xcode 10 GM 多命令产生... Pods 问题

    刚刚切换到 xcode 10 Golden Master 并开始在我的一个 pod 中出现错误 FDTake Tried this线程 但我在构建阶段没有 plist 所以它不起作用 这个问题与 pod 有关吗 Showing All Me
  • getImageData 导致“未捕获错误:NOT_SUPPORTED_ERR:DOM 异常 9”

    我正在尝试了解 HTML5 中的一些图像处理 我发现当我尝试使用 getImageData 方法时 我会抛出此 JS 异常 我正在使用 Chrome 并在本地主机上运行 Thanks 好吧 找出问题所在了 我只想说 德普 我正在使用 get
  • 在 Python 中使用 BeautifulSoup 查找 html 标签

    我想在html代码中找到一个特定的标签 比如如果有2个标签 那么我怎样才能获取第二个标签的内容而不是第一个标签的内容 soup find id contact1 在这里是示例html代码 table align center th STUD
  • RestKit RKMappingTest JSON 数组

    我正在使用 RestKit gt 0 20 3 和 RestKit Testing gt 0 20 3 这是我的映射的一个示例 RKObjectMapping mapping RKObjectMapping mappingForClass