dimanche 6 décembre 2015

SwiftyJSON not initializing from file correctly

I'm attempting to read in a file from my test target bundle, turn this file into NSData, and pass said data off to my class which then uses it to instantiate a SwiftyJSON instance. It seems as though my data is being generated correctly, however my SwiftyJSON instance seems to be null, and furthermore the test fails when I am looking for a nonexistent identifier (test should be returning false yet returns true)

My Test Case:

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
}

My Class under test:

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()
}

My 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}

My Log Output:


20:07:33.580 RedditClient[25599:333771] _XCT_testBundleReadyWithProtocolVersion:minimumVersion: reply received 20:07:33.641 RedditClient[25599:333771] _IDE_startExecutingTestPlanWithProtocolVersion:16 Test Suite 'Selected tests' started at 2015-12-06 20:07:33.646 Test Suite 'ParseListingTests' started at 2015-12-06 20:07:33.647 Test Case '-[RedditClientTests.ParseListingTests testInit]' started.

data is correct type

{"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}

SwiftyJSON.rawString(): nil

~/ParseListingTests.swift:38: error: -[RedditClientTests.ParseListingTests testInit] : XCTAssertFalse failed - first parse pass Test Case '-[RedditClientTests.ParseListingTests testInit]' failed (0.004 seconds). Test Suite 'ParseListingTests' failed at 2015-12-06 20:07:33.652. Executed 1 test, with 1 failure (0 unexpected) in 0.004 (0.005) seconds Test Suite 'Selected tests' failed at 2015-12-06 20:07:33.652. Executed 1 test, with 1 failure (0 unexpected) in 0.004 (0.007) seconds


Aucun commentaire:

Enregistrer un commentaire