cleishm / jsmockito

Javascript mocking framework inspired by the awesome mockito
http://jsmockito.org
Other
106 stars 19 forks source link

Invalid page redirection after calling 'mock()' #16

Closed zkhalapyan closed 11 years ago

zkhalapyan commented 11 years ago

I am using QUnit with JsMockito (1.0.4) and JsHamcrest (0.5.2) to test a JavaScript based application. After invoking the statement, var notificationAdapter = mock( LocalNotificationAdapter ); within QUnit module:setup() function, my page is redirected to:

http://localhost/og/test/function%20()%20%7B%20%20%20%20var%20args%20=%20[this];%20%20%20%20args.push.apply(args,%20arguments);%20%20%20%20interactions.push(%7Bargs:%20args%7D);%20%20%20%20var%20stubMatcher%20=%20JsMockito.find(stubMatchers,%20function(stubMatcher)%20%7B%20%20%20%20%20%20return%20JsMockito.matchArray(stubMatcher[0],%20args);%20%20%20%20%7D);%20%20%20%20if%20(stubMatcher%20==%20undefined)%20%20%20%20%20%20return%20delegate.apply(this,%20arguments);%20%20%20%20var%20stubs%20=%20stubMatcher[1];%20%20%20%20if%20(stubs.length%20==%200)%20%20%20%20%20%20return%20undefined;%20%20%20%20var%20stub%20=%20stubs[0];%20%20%20%20if%20(stubs.length%20%3E%201)%20%20%20%20%20%20stubs.shift();%20%20%20%20return%20stub.apply(this,%20arguments);%20%20%7D

from a page at localhost/og/test/index.html. Below is the output of the decoded URL:

function () {
    var args = [this];
    args.push.apply(args, arguments);
    interactions.push({
        args: args
    });
    var stubMatcher = JsMockito.find(stubMatchers, function (stubMatcher) {
        return JsMockito.matchArray(stubMatcher[0], args);
    });
    if (stubMatcher == undefined) return delegate.apply(this, arguments);
    var stubs = stubMatcher[1];
    if (stubs.length == 0) return undefined;
    var stub = stubs[0];
    if (stubs.length > 1) stubs.shift();
    return stub.apply(this, arguments);
}

The following is a console output that I get running within Google Chrome (Version 23.0.1271.95 m):

Uncaught TypeError: Cannot read property '_jsMockitoStubBuilder' of null jsmockito-1.0.4.js:681
addMockMethod jsmockito-1.0.4.js:681
JsMockito.mock jsmockito-1.0.4.js:687
(anonymous function)

Following is the HTML snippet from index.html that includes the libraries:

  <!-- Include QUnit Library -->
  <link rel="stylesheet" href="/og/lib/qunit/css/qunit.css">
  <script src="/og/lib/qunit/js/qunit.js"></script>

  <!-- Include JsHamcrest and JsMockito Library -->
  <script src="/og/lib/jsmockito/jsmockito-1.0.4.js"></script>
  <script src="/og/lib/jshamcrest/jshamcrest-0.5.2.js"></script>
  <script type="text/javascript">
      JsHamcrest.Integration.QUnit();
      JsMockito.Integration.QUnit();
  </script>

  <!-- And here is my actual test file. -->
  <script src="/og/test/models/reminders/ReminderModelTest.js"></script>

Here is the actual test execution:

module( "models.reminders.ReminderModel", {
  setup: function() {
    var notificationAdapter = mock( LocalNotificationAdapter );
  }
});

Have you encountered this issue? I would appreciate any help.

cleishm commented 11 years ago

I haven't encountered this issue. You probably want to work out where the redirection is occurring (inside your application) and figure out why. I'd guess that you'll need to stub a method on the mock.