SwiftyJSON未正确初始化文件

时间:2015-12-07 01:22:14

标签: ios swift unit-testing swifty-json

我尝试从我的测试目标包中读取文件,将此文件转换为NSData,然后将所述数据传递给我的类,然后使用它来实例化SwiftyJSON实例。好像我的数据生成正确,但是我的SwiftyJSON实例似乎是null,而且当我查找不存在的标识符时测试失败(测试应返回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()
}

我的testSample:

{"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 Test Suite' ParseListingTests'开始于2015-12-06 20:07:33.647 测试用例' - [RedditClientTests.ParseListingTests testInit]'启动。

数据类型正确

{" has_mail":false," name":" another_test_acct"," created":1447203562.0," hide_from_robots&# 34;:false," gold_creddits":0," created_utc":1447174762.0," has_mod_mail":false," link_karma":1,& #34; comment_karma":0," over_18":true," is_gold":false," is_mod":false," id&#34 ;:" rwuhe"," gold_expiration":null," inbox_count":0," has_verified_email":false," is_suspended&# 34;:false," suspension_expiration_utc":null}

SwiftyJSON.rawString():nil

〜/ ParseListingTests.swift:38:错误: - [RedditClientTests.ParseListingTests testInit]:XCTAssertFalse失败 - 首次解析传递 测试用例' - [RedditClientTests.ParseListingTests testInit]'失败(0.004秒)。 Test Suite' ParseListingTests'在2015-12-06 20:07:33.652失败。      执行1次测试,1次失败(0次意外),0.004(0.005)秒 测试套件'选择的测试'在2015-12-06 20:07:33.652失败。      执行1次测试,1次失败(0次意外),0.004(0.007)秒

1 个答案:

答案 0 :(得分:0)

init(dataFromNetworking: NSData) {
self.json = JSON(data: dataFromNetworking)