当前位置: 首页 > 软件库 > 程序开发 > >

ember-sinon-qunit

Sinon sandbox test integration for QUnit
授权协议 MIT License
开发语言 JavaScript
所属分类 程序开发
软件类型 开源软件
地区 不详
投 递 者 潘安平
操作系统 跨平台
开源组织
适用人群 未知
 软件概览

Ember Sinon QUnit

This addon integrates sinon & ember-qunitvia ember-sinon, originally inspired by sinon-qunit.

Why not simply use ember-sinon alone? Two reasons:

  1. ember-sinon does not handle cleanup of ember-qunit tests. While sinonsandboxes 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.
  2. 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-sinonwith other addons like ember-cli-mocha, for example.

Compatibility

  • Sinon.js v5.0.0 or above
  • Ember.js v3.16 or above
  • Ember CLI v2.13 or above
  • Node.js v12 or above

Installation

ember install ember-sinon-qunit

Usage

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.

Accessing sinon Within Tests

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

Using sinon with the @action decorator

The @action decorator is used with methods to bind them to the this of the class. The @actiondoes 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);

Migrating To 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-sinoffand ember-sinon-sandbox.Below, you will find simple instructions for migrating from each of these feature sets to the new patterns.

Migration from sinon 5+

  1. Import and consume setupSinon.
  2. Remove any manual calls to sinon.restore(). It won't hurt to leave them, but they are redundant now!

Migration from older versions of sinon

  1. Import and consume setupSinon.
  2. Remove calls to 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.
  3. Remove any manual restore() calls for your sandboxes.

Migration from older versions of ember-sinon-qunit

  1. Revert to using the standard ember-qunit test import:import { test } from 'qunit';
  2. Import and consume setupSinon.

Migration from ember-sinon-sinoff or ember-sinon-sandbox

  1. import sinon from 'sinon'; within each test that currently uses a sandbox.
  2. Replace this.sandbox with the imported sinon object.
  3. Remove references to setupSinonSinoff/setupSinonSandbox from your tests.
  4. Import and consume 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

Contributing

See the Contributing guide for details.

License

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 检查路由,数据选项卡和库信息 您可以看到检查