我尝试使用以下代码在GitHub中封装 uTest 测试:
object AjaxExp01SOTest extends TestSuite {
val log = JSLog.getLogger(getClass().getSimpleName)
JSLog.setGlobalLevel(Level.DEBUG)
def tests = TestSuite {
"OK: GET slP" - {
val promise = Promise[XMLHttpRequest]
val callback: (dom.Event, XMLHttpRequest ) => Unit = { (e,x) =>
log.debug("callback running...")
log.debug("res.status = " + x.status + " res.responseText = " + x.responseText )
promise.success(x)
}
def send( method: String, url: String, isAsync: Boolean, timeout: Long,
requestHeader: String, requestHeaderValue: String,
data: String, callback: (dom.Event, XMLHttpRequest ) => Unit ): XMLHttpRequest = {
val xhr = new dom.XMLHttpRequest()
xhr.open(method, url, isAsync )
xhr.timeout = timeout
if ( requestHeader != null) {
xhr.setRequestHeader(requestHeader, requestHeaderValue)
}
def cb(e: dom.Event): Unit = { callback(e,xhr) }
xhr.onload = cb _
if ( data != null) {
xhr.send(data)
} else {
xhr.send()
}
xhr
}
log.debug("send begin")
val url = "http://localhost:9000/slG/124"
val res = send("GET", url, true, 0, null, null, null, callback)
log.debug("send end")
promise.future
}
}
override def utestWrap(runBody: => concurrent.Future[Any])
(implicit ec: ExecutionContext): concurrent.Future[Any] = {
val f = runBody
f.onComplete {
case Success(a) if ( a != null )=> { log.debug("SUCCESS " + a.getClass.getSimpleName) } // BoxedUnit,
case Success(a) => { log.debug("SUCCESS a == null") }
case Failure(a) => log.debug("FAILURE")
}
log.debug("utestWrap begin")
f
}
}
它有这样的结果:
21:03:23.164 DEBUG AjaxExp01SOTest - SUCCESS BoxedUnit
21:03:23.168 DEBUG AjaxExp01SOTest - utestWrap begin
21:03:23.172 DEBUG AjaxExp01SOTest - OK: GET slP *****
21:03:23.174 DEBUG AjaxExp01SOTest - send begin
21:03:23.177 DEBUG AjaxExp01SOTest - send end
21:03:23.180 DEBUG AjaxExp01SOTest - utestWrap begin
客户端将 GET 请求发送到服务器,服务器在延迟后回复,但从未调用客户端的回调。测试卡在等待回调被调用。
这段代码有什么问题?很可能utestWrap
有错误或其他误解。
另一个(次要)问题是什么是“SUCCESS BoxedUnit”?TestSuite 中只有一个测试用例“GET slP”在“SUCCESS BoxedUnit”之后开始,并且从未达到“SUCCESS XMLHttpRequest”。