jasmine:spyOn(obj,'method')。andCallFake或and.callFake? [英] jasmine: spyOn(obj, 'method').andCallFake or and.callFake?

查看:146
本文介绍了jasmine:spyOn(obj,'method')。andCallFake或and.callFake?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的Jasmine测试中模拟测试数据。这是两个版本:

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

当我使用浏览器(Chrome,Firefox)执行测试时,第一个版本可以运行。但是,当我使用phantomjs运行相同的测试时,我必须使用第二个版本。否则,它会抱怨函数未定义。

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.

以下是错误消息:

// 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)

我搜索了Jasmine API但找不到哪个版本是正确的。我发现的所有示例似乎都使用第二个版本。

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.

Jasmine的API最近有变化吗?我怎样才能编写我的测试,所以它始终有效?

推荐答案

是的,间谍API已从Jasmine 1.3更改.1到Jasmine 2.0。没有正确的版本。如果你能找到Jasmine 2.0的工具支持,我建议升级。

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语法(文档

Jasmine 1.3.1 syntax (documentation)

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

Jasmine 2.0语法(文档

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"]);

我提到了工具支持,因为这似乎是你遇到的问题。 Jasmine 2.0只出现了几个月(在撰写本文时)。相比之下,Karma对Jasmine 2.0的支持已经推出了几周(我不确定其他工具)。

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).

要解决您的问题,请调查您正在使用哪些工具来运行测试,看看它们是否支持Jasmine 2.0。如果他们都这样做,那么去升级。否则,将浏览器测试降级到Jasmine 1.3.1并等待工具支持更好。只要确保你的全面一致。

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.

这篇关于jasmine:spyOn(obj,'method')。andCallFake或and.callFake?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆