这个Jasmine单元测试模拟调用是如何工作的?

时间:2017-10-10 17:22:24

标签: javascript jquery jasmine mocking jasmine-maven-plugin

这两者之间有什么区别:

第一版:

  it('Expect foo', function() {
    var response = [{'fizz': 'buzz'}];
    var jsonString = JSON.stringify(response);
    spyOn($, 'ajax').and.callFake(function(e) {
      e.success(jsonString);
    });
    expect($('#data-table tr:eq(1) td:eq(0)').text()).toEqual('buzz');
  });

第二版:

  it('Expect bar', function() {
    var response = [{'fizz': 'buzz'}];
    var jsonString = JSON.stringify(response);
    spyOn($, 'ajax');
    createAjaxCall("foo/bar/url");
    $.ajax.calls.mostRecent().args[0].success(JSON.stringify(response));
    expect($('#data-table tr:eq(1) td:eq(0)').text()).toEqual('buzz');
  });

以上两个测试函数应该测试ajax调用是否也最终更新了标识为#data-table的HTML元素,其中包含文本' buzz'对于表中的第1行和第0个数据元素。第一个导致我的Jasmine maven插件测试失败,出现了很难调试的奇怪错误,因为它导致Jasmine maven插件有时成功,有时会失败。第二个似乎没有失败。

我不熟悉第二个电话如何工作。它似乎是在调用createAjaxCall(),它实际上并没有调用该方法,但是使用$.ajax.calls...的模拟调用实际上是调用它的。究竟发生了什么?

这是错误(我正在使用Jasmine Maven插件版本2.2)。它预期令牌},但我检查过,似乎并非如此,没有任何遗漏}

[INFO] 
[INFO] --- jacoco-maven-plugin:0.7.5.201505241946:report (post-unit-test) @ scpdashboard-rest-service ---
[INFO] Analyzed bundle 'SCP-Dashboard-REST-Service Maven Webapp' with 40 classes
[INFO] 
[INFO] --- jasmine-maven-plugin:2.2:test (default) @ scpdashboard-rest-service ---
[INFO] jetty-8.2.0.v20160908
[INFO] Started SelectChannelConnector@0.0.0.0:56635
[INFO] Executing Jasmine Specs
[INFO] Resolved artifact C:\Users\WS055397\.m2\repository\com\github\klieber\phantomjs\2.0.0\phantomjs-2.0.0-windows.zip from central (https://repo.maven.apache.org/maven2, default, releases)
[INFO] Extracting C:\Users\WS055397\.m2\repository\com\github\klieber\phantomjs\2.0.0\phantomjs-2.0.0-windows.zip\phantomjs-2.0.0-windows\bin\phantomjs.exe to C:\Users\WS055397\DevCenter\SCP-Dashboard\scp-dashboard-rest-service\target\phantomjs\phantomjs-2.0.0-windows\bin\phantomjs.exe
Oct 10, 2017 11:16:08 AM org.openqa.selenium.phantomjs.PhantomJSDriverService <init>
INFO: executable: C:\Users\WS055397\DevCenter\SCP-Dashboard\scp-dashboard-rest-service\target\phantomjs\phantomjs-2.0.0-windows\bin\phantomjs.exe
Oct 10, 2017 11:16:08 AM org.openqa.selenium.phantomjs.PhantomJSDriverService <init>
INFO: port: 44483
Oct 10, 2017 11:16:08 AM org.openqa.selenium.phantomjs.PhantomJSDriverService <init>
INFO: arguments: [--webdriver=44483, --webdriver-logfile=C:\Users\WS055397\DevCenter\SCP-Dashboard\scp-dashboard-rest-service\phantomjsdriver.log]
Oct 10, 2017 11:16:08 AM org.openqa.selenium.phantomjs.PhantomJSDriverService <init>
INFO: environment: {}
[INFO  - 2017-10-10T16:16:10.187Z] GhostDriver - Main - running on port 44483
[INFO  - 2017-10-10T16:16:10.479Z] Session [542a14a0-add6-11e7-bbc1-f34aa38b1e36] - page.settings - {"XSSAuditingEnabled":false,"javascriptCanCloseWindows":true,"javascriptCanOpenWindows":true,"javascriptEnabled":true,"loadImages":true,"localToRemoteUrlAccessEnabled":false,"userAgent":"Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/538.1 (KHTML, like Gecko) PhantomJS/2.0.0 Safari/538.1","webSecurityEnabled":true}
[INFO  - 2017-10-10T16:16:10.479Z] Session [542a14a0-add6-11e7-bbc1-f34aa38b1e36] - page.customHeaders:  - {}
[INFO  - 2017-10-10T16:16:10.479Z] Session [542a14a0-add6-11e7-bbc1-f34aa38b1e36] - Session.negotiatedCapabilities - {"browserName":"phantomjs","version":"2.0.0","driverName":"ghostdriver","driverVersion":"1.2.0","platform":"windows-8-32bit","javascriptEnabled":true,"takesScreenshot":true,"handlesAlerts":false,"databaseEnabled":false,"locationContextEnabled":false,"applicationCacheEnabled":false,"browserConnectionEnabled":false,"cssSelectorsEnabled":true,"webStorageEnabled":false,"rotatable":false,"acceptSslCerts":false,"nativeEvents":true,"proxy":{"proxyType":"direct"}}
[INFO  - 2017-10-10T16:16:10.479Z] SessionManagerReqHand - _postNewSessionCommand - New Session Created: 542a14a0-add6-11e7-bbc1-f34aa38b1e36
[ERROR - 2017-10-10T16:16:10.962Z] Session [542a14a0-add6-11e7-bbc1-f34aa38b1e36] - page.onError - msg: SyntaxError: Expected token '}'

  :262 in error
[ERROR - 2017-10-10T16:16:10.962Z] Session [542a14a0-add6-11e7-bbc1-f34aa38b1e36] - page.onError - stack:
  eval (http://localhost:56635/src/jquery.i18n.properties.min.js:4)
  (anonymous function) (http://localhost:56635/src/jquery.i18n.properties.min.js:4)
  forEach (:0)
  y (http://localhost:56635/src/jquery.i18n.properties.min.js:4)
  w (http://localhost:56635/src/jquery.i18n.properties.min.js:3)
  success (http://localhost:56635/src/jquery.i18n.properties.min.js:2)
  (anonymous function) (http://localhost:56635/spec/HomeSpec.js:327)
  exec (http://localhost:56635/webjars/jasmine/jasmine.js:2052)
  spy (http://localhost:56635/webjars/jasmine/jasmine.js:194)
  v (http://localhost:56635/src/jquery.i18n.properties.min.js:1)
  p (http://localhost:56635/src/jquery.i18n.properties.min.js:1)
  (anonymous function) (http://localhost:56635/src/jquery.i18n.properties.min.js:1)
  error (http://localhost:56635/src/jquery.i18n.properties.min.js:2)
  i (http://code.jquery.com/jquery-2.2.4.min.js:2)
  fireWith (http://code.jquery.com/jquery-2.2.4.min.js:2)
  z (http://code.jquery.com/jquery-2.2.4.min.js:4)
  (anonymous function) (http://code.jquery.com/jquery-2.2.4.min.js:4)

  :262 in error
[WARNING] JavaScript Console Errors:

  * SyntaxError: Expected token '}'


[INFO  - 2017-10-10T16:16:11.077Z] ShutdownReqHand - _handle - About to shutdown
[INFO] stopped o.e.j.s.h.ContextHandler{/webjars,file:/C:/Users/WS055397/DevCenter/SCP-Dashboard/scp-dashboard-rest-service/}
[INFO] stopped o.e.j.s.h.ContextHandler{/classpath,file:/C:/Users/WS055397/DevCenter/SCP-Dashboard/scp-dashboard-rest-service/}
[INFO] stopped o.e.j.s.h.ContextHandler{/,file:/C:/Users/WS055397/DevCenter/SCP-Dashboard/scp-dashboard-rest-service/}
[INFO] stopped o.e.j.s.h.ContextHandler{/spec,file:/C:/Users/WS055397/DevCenter/SCP-Dashboard/scp-dashboard-rest-service/}
[INFO] stopped o.e.j.s.h.ContextHandler{/src,file:/C:/Users/WS055397/DevCenter/SCP-Dashboard/scp-dashboard-rest-service/}
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 10.720 s
[INFO] Finished at: 2017-10-10T11:16:12-05:00
[INFO] Final Memory: 32M/355M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal com.github.searls:jasmine-maven-plugin:2.2:test (default) on project scpdashboard-rest-service: The jasmine-maven-plugin encountered an exception:
[ERROR] java.lang.RuntimeException: java.lang.RuntimeException: There were javascript console errors.
[ERROR] 
[ERROR] JavaScript Console Errors:
[ERROR] 
[ERROR] * SyntaxError: Expected token '}'
[ERROR] 
[ERROR] 
[ERROR] at com.github.searls.jasmine.runner.SpecRunnerExecutor.execute(SpecRunnerExecutor.java:51)
[ERROR] at com.github.searls.jasmine.mojo.TestMojo.executeSpecs(TestMojo.java:229)
[ERROR] at com.github.searls.jasmine.mojo.TestMojo.run(TestMojo.java:196)
[ERROR] at com.github.searls.jasmine.mojo.AbstractJasmineMojo.execute(AbstractJasmineMojo.java:423)
[ERROR] at com.github.searls.jasmine.mojo.TestMojo.execute(TestMojo.java:183)
[ERROR] at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java:134)
[ERROR] at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:207)
[ERROR] at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:153)
[ERROR] at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:145)
[ERROR] at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:116)
[ERROR] at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:80)
[ERROR] at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build(SingleThreadedBuilder.java:51)
[ERROR] at org.apache.maven.lifecycle.internal.LifecycleStarter.execute(LifecycleStarter.java:128)
[ERROR] at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:307)
[ERROR] at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:193)
[ERROR] at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:106)
[ERROR] at org.apache.maven.cli.MavenCli.execute(MavenCli.java:863)
[ERROR] at org.apache.maven.cli.MavenCli.doMain(MavenCli.java:288)
[ERROR] at org.apache.maven.cli.MavenCli.main(MavenCli.java:199)
[ERROR] at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
[ERROR] at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
[ERROR] at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
[ERROR] at java.lang.reflect.Method.invoke(Method.java:498)
[ERROR] at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced(Launcher.java:289)
[ERROR] at org.codehaus.plexus.classworlds.launcher.Launcher.launch(Launcher.java:229)
[ERROR] at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode(Launcher.java:415)
[ERROR] at org.codehaus.plexus.classworlds.launcher.Launcher.main(Launcher.java:356)
[ERROR] Caused by: java.lang.RuntimeException: There were javascript console errors.
[ERROR] 
[ERROR] JavaScript Console Errors:
[ERROR] 
[ERROR] * SyntaxError: Expected token '}'
[ERROR] 
[ERROR] 
[ERROR] at com.github.searls.jasmine.runner.ConsoleErrorChecker.checkForConsoleErrors(ConsoleErrorChecker.java:17)
[ERROR] at com.github.searls.jasmine.runner.SpecRunnerExecutor.execute(SpecRunnerExecutor.java:42)
[ERROR] ... 26 more
[ERROR] -> [Help 1]
[ERROR] 
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR] 
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException

0 个答案:

没有答案