点击事件测试失败

Testing of click event fails

我正在关注official tutorial of scala.js,在测试部分有一些这样的代码:

package tutorial.webapp

import utest._

import org.scalajs.jquery.{JQuery, jQuery}

object TutorialTest extends TestSuite {

  // Initialize App
  TutorialApp.setupUI

  def tests = TestSuite {
    'HelloWorld {
      val ps = jQuery("p:contains('Hello World')")
      log("Number of hello-world paragraphs", ps.length)
      assert(ps.length == 1)
    }

    'ButtonClick {
      def messageCount =
        jQuery("p:contains('Bang!')").length
      val button = jQuery("button:contains('Click me!')")
      log("Number of bang paragraphs", messageCount)
      log("Number of button", button.length)
      assert(button.length == 1)
      assert(messageCount == 0)

      for (c <- 1 to 5) {
        log("Number of bang paragraphs", messageCount)
        log("Number of button", button.length)
        button.click()
        assert(messageCount == c)
      }

  }

  def log[A](header: String, msg: A): Unit = {
    println(
      """
        |
        |
        |=========================================
        |%s : %s
        |=========================================
        |
        |
      """.format(header, msg.toString).stripMargin)
  }
}

for 循环中基于事件的测试在 sbt test 中失败:

> test
[info] Compiling 1 Scala source to /Users/kaiyin/personal_config_bin_files/workspace/scalajsLearn/target/scala-2.11/test-classes...



=========================================
Number of hello-world paragraphs : 1
=========================================






=========================================
Number of bang paragraphs : 0
=========================================






=========================================
Number of button : 1
=========================================






=========================================
Number of bang paragraphs : 0
=========================================






=========================================
Number of button : 1
=========================================



[info] 1/3     tutorial.webapp.TutorialTest.HelloWorld      Success
[info] 2/3     tutorial.webapp.TutorialTest.ButtonClick
[info]                  utest.AssertionError: assert(messageCount == c)
[info]                  c: Int = 1
[info] 3/3     tutorial.webapp.TutorialTest     Success
[info] -----------------------------------Results-----------------------------------
[info] tutorial.webapp.TutorialTest     Success
[info]     HelloWorld       Success
[info]     ButtonClick      Failure('utest.AssertionError: assert(messageCount == c)
[info]     c: Int = 1')
[info] Failures:
[info] 2/3     tutorial.webapp.TutorialTest.ButtonClick
[info]                  utest.AssertionError: assert(messageCount == c)
[info]                  c: Int = 1
[info] (https://raw.githubusercontent.com/scala-js/scala-js/v0.6.5/library/src/main/scala/scala/scalajs/runtime/StackTrace.scala:39:42)
[info] (https://raw.githubusercontent.com/scala-js/scala-js/v0.6.5/library/src/main/scala/scala/scalajs/runtime/StackTrace.scala:33:17)
[info] (https://raw.githubusercontent.com/scala-js/scala-js/v0.6.5/javalanglib/src/main/scala/java/lang/Throwables.scala:24:50)
[info] (https://raw.githubusercontent.com/scala-js/scala-js/v0.6.5/javalanglib/src/main/scala/java/lang/Throwables.scala:12:19)
[info] (file:/Users/haoyi/Dropbox%20(Personal)/Workspace/utest-new/utest/js/../shared/src/main/scala/utest/framework/Errors.scala:20:45)
[info] (file:/Users/haoyi/Dropbox%20(Personal)/Workspace/utest-new/utest/js/../shared/src/main/scala/utest/asserts/package.scala:19:5)
[info] (file:/Users/haoyi/Dropbox%20(Personal)/Workspace/utest-new/utest/js/../shared/src/main/scala/utest/asserts/package.scala:38:56)
[info] (https://raw.githubusercontent.com/scala-js/scala-js/v0.6.5/library/src/main/scala/scala/scalajs/runtime/AnonFunctions.scala:15:38)
[info] (file:/Users/haoyi/Dropbox%20(Personal)/Workspace/utest-new/utest/js/../shared/src/main/scala/utest/asserts/Asserts.scala:74:22)
[info] (https://raw.githubusercontent.com/scala-js/scala-js/v0.6.5/library/src/main/scala/scala/scalajs/runtime/AnonFunctions.scala:15:38)
[info] (https://raw.githubusercontent.com/scala/scala/v2.11.7/src/library/scala/collection/IndexedSeqOptimized.scala:33:24)
[info] (https://raw.githubusercontent.com/scala-js/scala-js/v0.6.5/library/src/main/scala/scala/scalajs/js/WrappedArray.scala:20:13)
[info] (file:/Users/haoyi/Dropbox%20(Personal)/Workspace/utest-new/utest/js/../shared/src/main/scala/utest/asserts/Asserts.scala:72:16)
[info] (file:/Users/kaiyin/personal_config_bin_files/workspace/scalajsLearn/src/test/scala/tutorial/webapp/TutorialTest.scala:32:15)
[info] (https://raw.githubusercontent.com/scala-js/scala-js/v0.6.5/scalalib/overrides-2.11/scala/collection/immutable/Range.scala:160:8)
[info] (file:/Users/kaiyin/personal_config_bin_files/workspace/scalajsLearn/src/test/scala/tutorial/webapp/TutorialTest.scala:28:14)
[info] (file:/Users/kaiyin/personal_config_bin_files/workspace/scalajsLearn/src/test/scala/tutorial/webapp/TutorialTest.scala:12:25)
[info] (file:/Users/haoyi/Dropbox%20(Personal)/Workspace/utest-new/utest/js/../shared/src/main/scala/utest/framework/Model.scala:166:31)
[info] (file:/Users/haoyi/Dropbox%20(Personal)/Workspace/utest-new/utest/js/../shared/src/main/scala/utest/framework/Model.scala:164:27)
[info] (file:/Users/haoyi/Dropbox%20(Personal)/Workspace/utest-new/utest/js/../shared/src/main/scala/utest/framework/Model.scala:61:78)
[info] (https://raw.githubusercontent.com/scala-js/scala-js/v0.6.5/library/src/main/scala/scala/scalajs/runtime/AnonFunctions.scala:10:30)
[info] (https://raw.githubusercontent.com/scala/scala/v2.11.7/src/library/scala/util/Try.scala:192:16)
[info] (file:/Users/haoyi/Dropbox%20(Personal)/Workspace/utest-new/utest/js/../shared/src/main/scala/utest/framework/Model.scala:61:48)
[info] (file:/Users/haoyi/Dropbox%20(Personal)/Workspace/utest-new/utest/js/../shared/src/main/scala/utest/framework/Model.scala:61:48)
[info] (https://raw.githubusercontent.com/scala/scala/v2.11.7/src/library/scala/Option.scala:158:5)
[info] (file:/Users/haoyi/Dropbox%20(Personal)/Workspace/utest-new/utest/js/../shared/src/main/scala/utest/framework/Model.scala:61:93)
[info] (file:/Users/haoyi/Dropbox%20(Personal)/Workspace/utest-new/utest/js/../shared/src/main/scala/utest/framework/Model.scala:61:93)
[info] (https://raw.githubusercontent.com/scala/scala/v2.11.7/src/library/scala/util/Try.scala:237:43)
[info] (https://raw.githubusercontent.com/scala/scala/v2.11.7/src/library/scala/util/Try.scala:237:43)
[info] (https://raw.githubusercontent.com/scala-js/scala-js/v0.6.5/library/src/main/scala/scala/scalajs/runtime/AnonFunctions.scala:10:30)
[info] (https://raw.githubusercontent.com/scala/scala/v2.11.7/src/library/scala/util/Try.scala:192:16)
[info] (https://raw.githubusercontent.com/scala/scala/v2.11.7/src/library/scala/util/Try.scala:237:41)
[info] (https://raw.githubusercontent.com/scala/scala/v2.11.7/src/library/scala/concurrent/Future.scala:235:25)
[info] (https://raw.githubusercontent.com/scala/scala/v2.11.7/src/library/scala/concurrent/Future.scala:235:20)
[info] (https://raw.githubusercontent.com/scala-js/scala-js/v0.6.5/library/src/main/scala/scala/scalajs/runtime/AnonFunctions.scala:15:38)
[info] (https://raw.githubusercontent.com/scala/scala/v2.11.7/src/library/scala/concurrent/impl/Promise.scala:32:19)
[info] (file:/Users/haoyi/Dropbox%20(Personal)/Workspace/utest-new/utest/js/../shared/src/main/scala/utest/framework/ExecutionContext.scala:16:21)
[info] (https://raw.githubusercontent.com/scala/scala/v2.11.7/src/library/scala/concurrent/impl/Promise.scala:40:25)
[info] (https://raw.githubusercontent.com/scala/scala/v2.11.7/src/library/scala/concurrent/impl/Promise.scala:280:61)
[info] (https://raw.githubusercontent.com/scala/scala/v2.11.7/src/library/scala/concurrent/impl/Promise.scala:270:28)
[info] (https://raw.githubusercontent.com/scala/scala/v2.11.7/src/library/scala/concurrent/Future.scala:235:16)
[info] (https://raw.githubusercontent.com/scala/scala/v2.11.7/src/library/scala/concurrent/impl/Promise.scala:153:9)
[info] (file:/Users/haoyi/Dropbox%20(Personal)/Workspace/utest-new/utest/js/../shared/src/main/scala/utest/framework/Model.scala:61:37)
[info] (file:/Users/haoyi/Dropbox%20(Personal)/Workspace/utest-new/utest/js/../shared/src/main/scala/utest/framework/Model.scala:59:11)
[info] (https://raw.githubusercontent.com/scala/scala/v2.11.7/src/library/scala/concurrent/impl/Future.scala:24:20)
[info] (https://raw.githubusercontent.com/scala/scala/v2.11.7/src/library/scala/concurrent/impl/Future.scala:23:15)
[info] (file:/Users/haoyi/Dropbox%20(Personal)/Workspace/utest-new/utest/js/../shared/src/main/scala/utest/framework/ExecutionContext.scala:16:21)
[info] (https://raw.githubusercontent.com/scala/scala/v2.11.7/src/library/scala/concurrent/impl/Future.scala:31:29)
[info] (https://raw.githubusercontent.com/scala/scala/v2.11.7/src/library/scala/concurrent/Future.scala:492:114)
[info] (file:/Users/haoyi/Dropbox%20(Personal)/Workspace/utest-new/utest/js/../shared/src/main/scala/utest/framework/Model.scala:107:14)
[info] (file:/Users/haoyi/Dropbox%20(Personal)/Workspace/utest-new/utest/js/../shared/src/main/scala/utest/framework/Model.scala:82:100)
[info] (file:/Users/haoyi/Dropbox%20(Personal)/Workspace/utest-new/utest/js/../shared/src/main/scala/utest/framework/Model.scala:81:54)
[info] (https://raw.githubusercontent.com/scala/scala/v2.11.7/src/library/scala/Option.scala:158:5)
[info] (file:/Users/haoyi/Dropbox%20(Personal)/Workspace/utest-new/utest/js/../shared/src/main/scala/utest/framework/Model.scala:81:47)
[info] (file:/Users/haoyi/Dropbox%20(Personal)/Workspace/utest-new/utest/js/../shared/src/main/scala/utest/framework/Model.scala:83:24)
[info] (https://raw.githubusercontent.com/scala-js/scala-js/v0.6.5/library/src/main/scala/scala/scalajs/runtime/AnonFunctions.scala:15:38)
[info] (https://raw.githubusercontent.com/scala/scala/v2.11.7/src/library/scala/concurrent/Future.scala:251:31)
[info] (https://raw.githubusercontent.com/scala/scala/v2.11.7/src/library/scala/concurrent/Future.scala:249:16)
[info] (https://raw.githubusercontent.com/scala-js/scala-js/v0.6.5/library/src/main/scala/scala/scalajs/runtime/AnonFunctions.scala:15:38)
[info] (https://raw.githubusercontent.com/scala/scala/v2.11.7/src/library/scala/concurrent/impl/Promise.scala:32:19)
[info] (file:/Users/haoyi/Dropbox%20(Personal)/Workspace/utest-new/utest/js/../shared/src/main/scala/utest/framework/ExecutionContext.scala:16:21)
[info] (https://raw.githubusercontent.com/scala/scala/v2.11.7/src/library/scala/concurrent/impl/Promise.scala:40:25)
[info] (https://raw.githubusercontent.com/scala/scala/v2.11.7/src/library/scala/concurrent/impl/Promise.scala:280:61)
[info] (https://raw.githubusercontent.com/scala/scala/v2.11.7/src/library/scala/concurrent/impl/Promise.scala:270:28)
[info] (https://raw.githubusercontent.com/scala/scala/v2.11.7/src/library/scala/concurrent/Future.scala:249:16)
[info] (https://raw.githubusercontent.com/scala/scala/v2.11.7/src/library/scala/concurrent/impl/Promise.scala:153:9)
[info] (file:/Users/haoyi/Dropbox%20(Personal)/Workspace/utest-new/utest/js/../shared/src/main/scala/utest/framework/Model.scala:82:100)
[info] (file:/Users/haoyi/Dropbox%20(Personal)/Workspace/utest-new/utest/js/../shared/src/main/scala/utest/framework/Model.scala:81:54)
[info] (https://raw.githubusercontent.com/scala/scala/v2.11.7/src/library/scala/Option.scala:158:5)
[info] (file:/Users/haoyi/Dropbox%20(Personal)/Workspace/utest-new/utest/js/../shared/src/main/scala/utest/framework/Model.scala:81:47)
[info] (file:/Users/haoyi/Dropbox%20(Personal)/Workspace/utest-new/utest/js/../shared/src/main/scala/utest/framework/Model.scala:88:38)
[info] (file:/Users/haoyi/Dropbox%20(Personal)/Workspace/utest-new/utest/js/../shared/src/main/scala/utest/framework/Model.scala:59:11)
[info] (https://raw.githubusercontent.com/scala/scala/v2.11.7/src/library/scala/concurrent/impl/Future.scala:24:20)
[info] (https://raw.githubusercontent.com/scala/scala/v2.11.7/src/library/scala/concurrent/impl/Future.scala:23:15)
[info] (file:/Users/haoyi/Dropbox%20(Personal)/Workspace/utest-new/utest/js/../shared/src/main/scala/utest/framework/ExecutionContext.scala:16:21)
[info] (https://raw.githubusercontent.com/scala/scala/v2.11.7/src/library/scala/concurrent/impl/Future.scala:31:29)
[info] (https://raw.githubusercontent.com/scala/scala/v2.11.7/src/library/scala/concurrent/Future.scala:492:114)
[info] (file:/Users/haoyi/Dropbox%20(Personal)/Workspace/utest-new/utest/js/../shared/src/main/scala/utest/framework/Model.scala:107:14)
[info] (file:/Users/haoyi/Dropbox%20(Personal)/Workspace/utest-new/utest/js/../shared/src/main/scala/utest/framework/Model.scala:133:22)
[info] (file:/Users/haoyi/Dropbox%20(Personal)/Workspace/utest-new/utest/js/../shared/src/main/scala/utest/package.scala:135:6)
[info] (file:/Users/haoyi/Dropbox%20(Personal)/Workspace/utest-new/utest/js/../shared/src/main/scala/utest/runner/BaseRunner.scala:70:21)
[info] (file:/Users/haoyi/Dropbox%20(Personal)/Workspace/utest-new/utest/js/../shared/src/main/scala/utest/runner/BaseRunner.scala:95:35)
[info] (https://raw.githubusercontent.com/scala-js/scala-js/v0.6.5/library/src/main/scala/scala/scalajs/runtime/AnonFunctions.scala:30:68)
[info] (file:/Users/haoyi/Dropbox%20(Personal)/Workspace/utest-new/utest/js/../shared/src/main/scala/utest/runner/Task.scala:48:19)
[info] (file:/Users/haoyi/Dropbox%20(Personal)/Workspace/utest-new/utest/js/../shared/src/main/scala/utest/runner/Task.scala:31:17)
[info] (https://raw.githubusercontent.com/scala-js/scala-js/v0.6.0/test-interface/src/main/scala/org/scalajs/testinterface/internal/Slave.scala:104:36)
[info] (https://raw.githubusercontent.com/scala-js/scala-js/v0.6.0/test-interface/src/main/scala/org/scalajs/testinterface/internal/Slave.scala:104:36)
[info] (https://raw.githubusercontent.com/scala-js/scala-js/v0.6.5/library/src/main/scala/scala/scalajs/runtime/AnonFunctions.scala:10:30)
[info] (https://raw.githubusercontent.com/scala/scala/v2.11.7/src/library/scala/util/Try.scala:192:16)
[info] (https://raw.githubusercontent.com/scala-js/scala-js/v0.6.0/test-interface/src/main/scala/org/scalajs/testinterface/internal/Slave.scala:104:23)
[info] (https://raw.githubusercontent.com/scala-js/scala-js/v0.6.0/test-interface/src/main/scala/org/scalajs/testinterface/internal/Slave.scala:36:18)
[info] (https://raw.githubusercontent.com/scala-js/scala-js/v0.6.0/test-interface/src/main/scala/org/scalajs/testinterface/internal/Slave.scala:31:7)
[info] (https://raw.githubusercontent.com/scala-js/scala-js/v0.6.5/library/src/main/scala/scala/scalajs/runtime/AnonFunctions.scala:10:30)
[info] (https://raw.githubusercontent.com/scala-js/scala-js/v0.6.0/test-interface/src/main/scala/org/scalajs/testinterface/internal/Slave.scala:69:7)
[info] (https://raw.githubusercontent.com/scala-js/scala-js/v0.6.0/test-interface/src/main/scala/org/scalajs/testinterface/internal/Slave.scala:30:28)
[info] (https://raw.githubusercontent.com/scala-js/scala-js/v0.6.0/test-interface/src/main/scala/org/scalajs/testinterface/internal/BridgeBase.scala:34:20)
[info] (https://raw.githubusercontent.com/scala-js/scala-js/v0.6.0/test-interface/src/main/scala/org/scalajs/testinterface/internal/BridgeBase.scala:19:14)
[info] (https://raw.githubusercontent.com/scala-js/scala-js/v0.6.0/test-interface/src/main/scala/org/scalajs/testinterface/internal/BridgeBase.scala:19:14)
[info] Tests: 3
[info] Passed: 2
[info] Failed: 1
[error] Failed tests:
[error]     tutorial.webapp.TutorialTest
[error] (test:test) sbt.TestsFailedException: Tests unsuccessful
[error] Total time: 2 s, completed 27 oct. 2015 21:00:35

整个项目在这里:https://github.com/kindlychung/scalajstutorial

这里定义了点击行为:

package tutorial.webapp



/**
 * Created by IDEA on 27/10/15.
 */
import scala.scalajs.js.JSApp
import org.scalajs.dom
import dom.document
import scala.scalajs.js.annotation.JSExport
import org.scalajs.jquery.jQuery



object TutorialApp extends JSApp {
  def main(): Unit = {
    setupUI
    setupBehavior
  }

  def appendPar(targetNode: dom.Node, text: String): Unit = {
    val parNode = document.createElement("p")
    val textNode = document.createTextNode(text)
    parNode.appendChild(textNode)
    targetNode.appendChild(parNode)
  }

  def addClickedMessage(msg: String): Unit = {
    jQuery("body").append("<p>%s</p>".format(msg))
  }

  def setupUI: Unit = {
    jQuery("body").append("<p>Hello World!</p>")
    jQuery("body").append("""<button id="click-me-button" type="button">Click me!</button>""")
  }

  def setupBehavior: Unit = {
    jQuery("#click-me-button").click(() => addClickedMessage("Bang!"))
  }

}

您的按钮确实没有任何作用。您的测试不会调用 setupBehavior,它会在按钮上注册 click 事件。因此,调用 click() 不会执行任何操作。没有添加消息,测试正确报无消息

请务必致电 setupBehavior 来解决您的问题。