This addon integrates sinon
& ember-qunit
via ember-sinon
, originally inspired by sinon-qunit
.
Why not simply use ember-sinon
alone? Two reasons:
ember-sinon
does not handle cleanup of ember-qunit
tests. While sinon
sandboxes itself, it's up to the user toconsistently clean up sinon
after each test. ember-sinon-qunit
automaticallyrestores sinon
's state to ensure nothing is leaked between tests. All spies/stubs createdwill be automatically restored to their original methods at the end of each test.sinon
is a framework-agnostic library; as such, ember-sinon
should be as well. This addon exists to enableember-sinon
to remove its qunit
specific functionality, making it easier to utilize ember-sinon
with other addons like ember-cli-mocha
, for example.ember install ember-sinon-qunit
To use, import the setup method into your tests/test-helper.js
file and execute it.
import { setApplication } from '@ember/test-helpers';
import { start } from 'ember-qunit';
import Application from '../app';
import config from '../config/environment';
import setupSinon from 'ember-sinon-qunit';
setApplication(Application.create(config.APP));
setupSinon();
start();
This will automatically wire-up sinon
's setup & restoration to QUnit's testStart
and testDone
respectively.
sinon
Within TestsIn each test you are able to access sinon
via the sinon
object available as an import in your tests:
import { module } from 'qunit';
import { test } from 'ember-qunit';
import sinon from 'sinon';
module('Example test', function(hooks) {
hooks.beforeEach(function() {
this.testStub = sinon.stub();
});
test('sinon is wired up correctly', function(assert) {
this.testStub();
assert.ok(this.testStub.calledOnce, 'stub was called once');
});
test('sinon state restored after every test run', function(assert) {
assert.ok(this.testStub.notCalled, 'stub cleaned up after each test run');
});
});
The sinon
object's state is automatically self-contained to each specific test, allowing you tosafely create mocks for your tests without worrying about any overrides leaking between each test.
@action
decoratorThe @action
decorator is used with methods to bind them to the this
of the class. The @action
does this by wrapping the method in a property with the getter
of the property returning theoriginal method bound to this
. That means when you wish to stub or spy the method, you have to treat it as aproperty not a method.
let stubAction = sinon.stub(service, "methodToStub").get(
function() {
return null;
}
);
let spyAction = sinon.spy(service, "methodToStub", ["get"]);
assert.ok(stubAction.get.calledOnce);
assert.ok(spyAction.get.calledOnce);
ember-sinon-qunit
Read this post to learn more about the overhaul of this package. |
---|
The above functionality replaces previous features within ember-sinon-qunit
,as well as the sister addons ember-sinon-sinoff
and ember-sinon-sandbox
.Below, you will find simple instructions for migrating from each of these feature sets to the new patterns.
sinon
5+setupSinon
.sinon.restore()
. It won't hurt to leave them, but they are redundant now!sinon
setupSinon
.sinon.createSandbox()
. Anywhere you used the sandbox
object returned by this method,you can now use sinon
directly. See the sinon
Migration Guidefor more information.restore()
calls for your sandboxes.ember-sinon-qunit
ember-qunit
test import:import { test } from 'qunit';
setupSinon
.ember-sinon-sinoff
or ember-sinon-sandbox
import sinon from 'sinon';
within each test that currently uses a sandbox
.this.sandbox
with the imported sinon
object.setupSinonSinoff
/setupSinonSandbox
from your tests.setupSinon
.Or, if you'd like to save some effort, try the following codemod ember-sinon-qunit-codemod
:
cd my-ember-app-or-addon
npx ember-sinon-qunit-codemod tests
See the Contributing guide for details.
This project is licensed under the MIT License.
最近我准备构建一个自己的轻量级的库,但是我遇到了一些问题,最大的问题就是比如写着写着,我怕后面写的东西会覆盖掉前面的一些功能,所以我只能不停的测试,于是我想到了用一些测试库,试着安装传说中的mocha啥的,on my god,我的破电脑完全不给力装不上去,最后找了这一款超级轻量级的语法简单的测试库,而且还是牛逼的John Resig发明的. ``` QUnit.test( "isFunctio
Sinon.js是个测试辅助工具,在为Node程序写测试时可能可以派上用场。 在测试领域有这么几个基本名词:spy, stub, mock,这三个概念都是测试所用到的手段。Sinon.js就提供了相应的工具来实现这三种测试手段。 sinon.stub() 完成后一定要sinon.restore(),否则会影响其他的测试 spy spy的作用在于可以监视一个函数被调用的情况。spy相当于给我们感兴趣
我在获取一个sinon存根以返回/解析另一个sinon存根时遇到问题。我正在使用西农、柴、柴和摩卡。 我正在按顺序执行许多异步任务,我想测试的代码看起来像这样: 我尝试为此创建存根的尝试如下所示: “saveit”方法在Terminal.prototype,这就是为什么我需要在那里存根它。当我尝试运行它时,我收到错误消息: 在线上: 但如果我在控制台中转储终端对象,它看起来很好,就像任何其他存根对
我很难找到sinon间谍没有被触发的原因。在下面的测试中,两个控制台语句都报告为false,因此两个方法都没有被调用(以防出现错误)。 这是我的一个摩卡测试通常的样子: }); PostOnToller中的方法: 最后是PostModel中的方法: 如果我以正常的方式调用方法,它们会执行查找,返回预期的Posts数组。但是,不会执行间谍。另外,如果我将控制器类中的方法更改为 间谍函数(res)确实
我正在用sinon和ava做测试。我正在清除一些函数,并检查这些函数是否被调用。我检查了控制台,函数正在被调用。但是sinon返回的是notCalled(。称为假)。下面是这段代码。 }; });
我需要为一个endpoint创建一个单元测试,这个endpoint将向某个API发出HTTP请求,并发送回包含HTTP请求结果的响应, 我使用请求promise链接在这里节点包,你可以看到下面的代码: 来自 预计正文(响应数据)是: 我使用摩卡、柴和西农来运行单元测试,你可以在下面看到上述功能的单元测试案例: 当我运行时,此集成测试总是失败,需要找出如何正确地存根。
我有一个带有路由器的express应用程序,我想与Sinon一起测试。我无法成功模拟传递到请求处理程序的参数,希望能得到一些帮助。 这是我当前使用Mocha、Sinon、Chai的测试设置
Ember检查器是一个浏览器插件,用于调试Ember应用程序。 灰烬检查员包括以下主题 - S.No. 灰烬检查员方式和描述 1 安装Inspector 您可以安装Ember检查器来调试您的应用程序。 2 Object Inspector Ember检查器允许与Ember对象进行交互。 3 The View Tree 视图树提供应用程序的当前状态。 4 检查路由,数据选项卡和库信息 您可以看到检查