IHaveAProblem
IHaveAProblem

Reputation: 33

How does this Jasmine unit test mock call work?

What is the difference between these two:

First version:

  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');
  });

Second version:

  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');
  });

The above two test functions are supposed to test whether an ajax call also ends up updating the HTML element with id #data-table with text 'buzz' for 1st row and 0th data element in the table. The first one is causing my Jasmine maven plugin tests to fail with weird errors that are hard to debug because it causes Jasmine maven plugin to sometimes succeed and sometimes fail. The second one doesn't seem to be failing.

I am not familiar with how the second call works. It seems to be calling createAjaxCall(), which doesn't actually call the method, but then the mock call using $.ajax.calls... is the one that actually calls it. What exactly is happening there?

Here is the error (I am using Jasmine Maven plugin version 2.2). It expected token }, but I checked and that doesn't seem to be the case, there weren't any missing }:

[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 [email protected]: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

Upvotes: 0

Views: 176

Answers (0)

Related Questions