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)
我正在尝试从我的测试目标包中读取一个文件,将此文件转换为NSData,并将所述数据传递给我的类,然后使用它来实例化SwiftyJSON实例。好像我的数据生成正确,但是我的SwiftyJSON实例似乎是null,而且当我在查找不存在的标识符时测试失败(测试应返回false但返回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.
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'开始于2015-12-06 20:07:33.646测试Suite'ParseListingTests'开始于2015-12-06 20:07:33.647测试用例' - [RedditClientTests.ParseListingTests testInit]'开始。
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}
{“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
〜/ 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次意外)测试套件“选定测试”在2015-12-06 20:07:33.652失败。执行1次测试,1次失败(0次意外),0.004(0.007)秒
1 个解决方案
#1
0
init(dataFromNetworking: NSData) {
self.json = JSON(data: dataFromNetworking)
#1
0
init(dataFromNetworking: NSData) {
self.json = JSON(data: dataFromNetworking)