茉莉花:spyOn(obj, 'method').andCallFake 还是 and.callFake? [英] jasmine: spyOn(obj, 'method').andCallFake or and.callFake?

查看:12
本文介绍了茉莉花: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.

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

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