【发布时间】:2015-12-07 01:22:13
【问题描述】:
我正在尝试从我的测试目标包中读取一个文件,将此文件转换为 NSData,并将所述数据传递给我的类,然后使用它来实例化一个 SwiftyJSON 实例。似乎我的数据正在正确生成,但是我的 SwiftyJSON 实例似乎为空,而且当我寻找不存在的标识符时测试失败(测试应该返回 false 但返回 true)
我的测试用例:
class ParseListingTests: XCTestCase {
var testData: NSData!
var testJson: AnyObject?
var mut: ParseListing?
override func setUp() {
super.setUp()
// Put setup code here. This method is called before the invocation of each test method in the class.
let bundle: NSBundle = NSBundle(forClass: self.dynamicType)
let sampleJson: String! = bundle.pathForResource("testsample", ofType: nil)
do { testData = try! NSData(contentsOfFile: sampleJson, options: NSDataReadingOptions.DataReadingMappedIfSafe ) }
if testData.isKindOfClass(NSData) { print("data is correct type") }
print(String.init(data: testData, encoding: NSUTF8StringEncoding)!)
mut = ParseListing(dataFromNetworking: testData!)
}
func testInit() {
XCTAssertNotNil(mut, "Class should initalize properly")
let test: Bool = (mut?.startParse())!
XCTAssertFalse(test, "first parse pass") // This assertion fails
}
我的测试中的班级:
class ParseListing {
//MARK: Class lifecycle
init(dataFromNetworking: NSData) {
self.json = JSON(dataFromNetworking)
}
//MARK: Class variables
let json: JSON!
//MARK: Class parsing func's
func startParse() -> Bool {
print("SwiftyJSON.rawString(): \(json.rawString())")
return json["qzqzqz"].isExists()
}
我的测试样本:
{"has_mail": false, "name": "another_test_acct", "created": 1447203562.0, "hide_from_robots": false, "gold_creddits": 0, "created_utc": 1447174762.0, "has_mod_mail": false, "link_karma": 1, "comment_karma": 0, "over_18": true, "is_gold": false, "is_mod": false, "id": "rwuhe", "gold_expiration": null, "inbox_count": 0, "has_verified_email": false, "is_suspended": false, "suspension_expiration_utc": null}
我的日志输出:
20:07:33.580 RedditClient[25599:333771] _XCT_testBundleReadyWithProtocolVersion:minimumVersion: 收到回复 20:07:33.641 RedditClient [25599:333771] _IDE_startExecutingTestPlanWithProtocolVersion:16 测试套件“选定测试”开始于 2015-12-06 20:07:33.646 测试套件“ParseListingTests”开始于 2015-12-06 20:07:33.647 测试用例“-[RedditClientTests.ParseListingTests testInit]”已启动。
数据类型正确
{“has_mail”:假,“名称”:“another_test_acct”,“created”:1447203562.0,“hide_from_robots”:假,“gold_creddits”:0,“created_utc”:1447174762.0,“has_mod_mail”:假,“link_karma” “:1,“comment_karma”:0,“over_18”:true,“is_gold”:false,“is_mod”:false,“id”:“rwuhe”,“gold_expiration”:null,“inbox_count”:0,“has_verified_email ": false, "is_suspended": false, "suspension_expiration_utc": null}
SwiftyJSON.rawString(): 无
~/ParseListingTests.swift:38: 错误:-[RedditClientTests.ParseListingTests testInit] : XCTAssertFalse 失败 - 第一次解析通过 测试用例“-[RedditClientTests.ParseListingTests testInit]”失败(0.004 秒)。 测试套件“ParseListingTests”在 2015-12-06 20:07:33.652 失败。 执行 1 次测试,在 0.004 (0.005) 秒内有 1 次失败(0 次意外) 测试套件“选定测试”在 2015-12-06 20:07:33.652 失败。 执行 1 次测试,在 0.004 (0.007) 秒内有 1 次失败(0 次意外)
【问题讨论】:
标签: ios swift unit-testing swifty-json