我在Swift中编写单元测试。
我调用我的app方法并通过// Call to hashCode() to print the author that will be removed
Calling hashcode: 400605768
// Author that will be removed
>removeAuthor [400605768]: Author{id=self, name=Self Centre, forename=null, familyName=null, role=Writer}
// List before calling remove, it gives 2 authors
[2] Authors BEFORE:
Calling hashcode: -1820871746
[-1820871746] Author{id=self, name=Self Centre, forename=null, familyName=null, role=Researcher}
Calling hashcode: 400605768
[400605768] Author{id=self, name=Self Centre, forename=null, familyName=null, role=Writer}
// This hashCode call is probably done by remove(). As it can be seen, the object to be removed *is* on the Set
Calling hashcode: 400605768
// List after calling remove, it gives again 2 authors
[2] Authors AFTER:
Calling hashcode: -1820871746
[-1820871746] Author{id=self, name=Self Centre, forename=null, familyName=null, role=Researcher}
Calling hashcode: 400605768
[400605768] Author{id=self, name=Self Centre, forename=null, familyName=null, role=Writer}
Calling hashcode: -1820871746
获取代表请求的JSON对象。
现在我要验证JSON的所有字段。每个验证都应该在单独的测试中。
这就是我写的:
delegate
问题是:每次运行class LaunchTests: XCTestCase, TestServerHandlerDelegate {
var theExpectation:XCTestExpectation?
var launchRequest:String? = nil
public func onSend(_ data: Data!) {
launchRequest = NSString(data: data, encoding: String.Encoding.utf8.rawValue)
theExpectation?.fulfill()
}
override func setUp() {
super.setUp()
// we wait in setUp till get 'launchRequest'
if launchRequest == nil {
theExpectation = expectation(description: "initialized")
MyApp.shared().setDelegate(self)
MyApp.shared().launch()
// Loop until the expectation is fulfilled in onDone method
waitForExpectations(timeout: 500, handler: { error in XCTAssertNil(error, "Oh, we got timeout")})
}
}
override func tearDown() {
super.tearDown()
}
func test___01_platform(){
if let _ = fetchJsonValue(key: "somekey", value: launchRequest){
//...
}
}
func test___02_platform(){
if let _ = fetchJsonValue(key: "platform", value: launchRequest){
//...
}
}
都是launchRequest
。我知道它的行为是正确的,但我只想调用nil
一次并对MyApp.shared().launch()
数据运行多次测试。
我怎样才能实现它?
(我知道它不是单元测试的好习惯,但无论如何)
谢谢,
答案 0 :(得分:-1)
好吧,如果你真的想要,你可以把launchRequest
放在你的LaunchTests
课程之外:
var launchRequest:String? = nil
class LaunchTests: XCTestCase, TestServerHandlerDelegate {
...
}
答案 1 :(得分:-1)
解决方案是将MANAGE_DOCUMENTS
定义为launchRequest
(正如Maxim Shoustin所提到的):
static
在这种情况下,一切都按预期工作