Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
215 views
in Technique[技术] by (71.8m points)

javascript - jasmine: spyOn(obj, 'method').andCallFake or and.callFake?

I want to mock test data in my Jasmine tests. Here are two versions:

// version 1:
spyOn(mBankAccountResource, 'getBankAccountData').and.callFake(fakedFunction);

// version 2:
spyOn(mBankAccountResource, 'getBankAccountData').andCallFake(fakedFunction);

When I execute my tests with a browser (Chrome, Firefox) then the first version works. However, when I run the same test with phantomjs, I have to use the second version. Otherwise, it complains that the function is not defined.

Here are the error messages:

// phantomjs (with version 1)
    TypeError: 'undefined' is not an object (evaluating 'spyOn(mBankAccountResource, 'getBankAccountData').and.callFake')
    at /home/phil/workspaces/world/basket.angular.ui/basket.angular.ui/test/bankaccount/BankAccountCtrlTest.js:65
    at invoke (/home/phil/workspaces/world/basket.angular.ui/bower_components/angular/angular.js:3707)
    at workFn (/home/phil/workspaces/world/basket.angular.ui/bower_components/angular-mocks/angular-mocks.js:2149)
undefined

// Chrome (with version 2)
TypeError: Object function () {
        callTracker.track({
          object: this,
          args: Array.prototype.slice.apply(arguments)
        });
        return spyStrategy.exec.apply(this, arguments);
      } has no method 'andCallFake'
    at Object.<anonymous> (/home/phil/workspaces/world/basket.angular.ui/basket.angular.ui/test/bankaccount/BankAccountCtrlTest.js:65:59)
    at Object.invoke (/home/phil/workspaces/world/basket.angular.ui/bower_components/angular/angular.js:3707:17)
    at Object.workFn (/home/phil/workspaces/world/basket.angular.ui/bower_components/angular-mocks/angular-mocks.js:2149:20)

I searched the Jasmine API but could not find out which version is the correct one. All examples that I found seem to use the second version.

Did the API of Jasmine change recently? How can I write my tests, so it always works?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Yes, the spy API changed from Jasmine 1.3.1 to Jasmine 2.0. There's no "correct" version. If you can find the tool support for Jasmine 2.0, I would recommend upgrading.

Jasmine 1.3.1 syntax (documentation)

spyOn(mBankAccountResource, 'getBankAccountData').andCallFake(fakedFunction);
expect(mBankAccountResource.getBankAccountData.mostRecentCall.args).toEqual(["foo"]);

Jasmine 2.0 syntax (documentation)

// Methods moved to 'and' property
spyOn(mBankAccountResource, 'getBankAccountData').and.callFake(fakedFunction);

// Call data moved to 'calls' property
expect(mBankAccountResource.getBankAccountData.calls.mostRecent().args).toEqual(["foo"]);

I mention tool support because it seems that's the problem you're having. Jasmine 2.0 has only been out for a couple of months (at the time of writing). Support for Jasmine 2.0 in Karma, by comparison, has been out for a couple of weeks (I'm not sure about other tools).

To solve your issue, investigate which tool(s) you're using to run tests and see if any of them support Jasmine 2.0. If all of them do, then go for the upgrade. Otherwise, downgrade your browser tests to Jasmine 1.3.1 and wait until tool support is better. Just make sure you're consistent across the board.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...