SwiftyJSON 未正确从文件初始化
SwiftyJSON not initializing from file correctly
我正在尝试从我的测试目标包中读取一个文件,将此文件转换为 NSData,然后将所述数据传递给我的 class,然后后者使用它来实例化一个 SwiftyJSON 实例。似乎我的数据生成正确,但我的 SwiftyJSON 实例似乎为空,而且当我寻找不存在的标识符时测试失败(测试应该返回 false 但 returns 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正在测试中:
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
测试套件 'Selected tests' 于 2015-12-06 20:07:33.646 开始
测试套件 'ParseListingTests' 于 2015-12-06 20:07:33.647 开始
测试用例“-[RedditClientTests.ParseListingTests testInit]”已启动。
数据类型正确
{"has_mail":假,"name":"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":真,"is_gold":假,"is_mod":假,"id":"rwuhe","gold_expiration":空,"inbox_count":0,"has_verified_email":假,"is_suspended":假, "suspension_expiration_utc": 空}
SwiftyJSON.rawString(): 无
~/ParseListingTests.swift:38:错误:-[RedditClientTests.ParseListingTests testInit]:XCTAssertFalse 失败 - 第一次解析通过
测试用例“-[RedditClientTests.ParseListingTests testInit]”失败(0.004 秒)。
测试套件 'ParseListingTests' 在 2015-12-06 20:07:33.652 失败。
在 0.004 (0.005) 秒内执行了 1 次测试,有 1 次失败(0 次意外)
测试套件 'Selected tests' 在 2015-12-06 20:07:33.652 失败。
在 0.004 (0.007) 秒内执行了 1 次测试,1 次失败(0 次意外)
init(dataFromNetworking: NSData) {
self.json = JSON(data: dataFromNetworking)
我正在尝试从我的测试目标包中读取一个文件,将此文件转换为 NSData,然后将所述数据传递给我的 class,然后后者使用它来实例化一个 SwiftyJSON 实例。似乎我的数据生成正确,但我的 SwiftyJSON 实例似乎为空,而且当我寻找不存在的标识符时测试失败(测试应该返回 false 但 returns 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正在测试中:
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 测试套件 'Selected tests' 于 2015-12-06 20:07:33.646 开始 测试套件 'ParseListingTests' 于 2015-12-06 20:07:33.647 开始 测试用例“-[RedditClientTests.ParseListingTests testInit]”已启动。
数据类型正确
{"has_mail":假,"name":"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":真,"is_gold":假,"is_mod":假,"id":"rwuhe","gold_expiration":空,"inbox_count":0,"has_verified_email":假,"is_suspended":假, "suspension_expiration_utc": 空}
SwiftyJSON.rawString(): 无
~/ParseListingTests.swift:38:错误:-[RedditClientTests.ParseListingTests testInit]:XCTAssertFalse 失败 - 第一次解析通过 测试用例“-[RedditClientTests.ParseListingTests testInit]”失败(0.004 秒)。 测试套件 'ParseListingTests' 在 2015-12-06 20:07:33.652 失败。 在 0.004 (0.005) 秒内执行了 1 次测试,有 1 次失败(0 次意外) 测试套件 'Selected tests' 在 2015-12-06 20:07:33.652 失败。 在 0.004 (0.007) 秒内执行了 1 次测试,1 次失败(0 次意外)
init(dataFromNetworking: NSData) {
self.json = JSON(data: dataFromNetworking)