锡农. JS Chai 请求
希农 柴仔 柴仔 柴仔 提供一套自定义断言,用于使用 西农. 联合来文 间谍、间谍和嘲弄框架 柴仔 主张图书馆。 你用西农的强大工具 得到柴的所有好处 联合来文.
取而代之用西农. 联合来文称:
sinon.assertCalledWith(mySpy, "foo");
或尴尬地试图用柴的 should 或者说 expect 间谍属性的界面 :
mySpy.calledWith("foo").should.be.ok;
expect(mySpy.calledWith("foo")).to.be.ok;
你可以说
mySpy.should.have.been.calledWith("foo");
expect(mySpy).to.have.been.calledWith("foo");
请求
所有你最喜欢的Sinon.JS的说法都进入了Sinon-Chai。 我们展示 should 这里的语法 expect
也可提供等同数据.
| Sinon.JS财产/方法 | Sinon-Chai 主张 |
|---|---|
| 调用 | 间谍。 。 |
| 调用 | 间谍 |
| 名为 Once | 间谍,应该... 已经.. |
| 两次 | 间谍,应该... 已经... 已经.. |
| 名为Thrike | 间谍,应该... 已经.. |
| 调用前 | 间谍1号,应该. been before (spy2) |
| 后调用 | 间谍1号,应该的,应该的,应该的 |
| 名为即时 | 间谍,应该的,应该的,应该的,应该的 |
| 之后立即调用 | 间谍,应该的,应该的,应该的,应该的,应该的 |
| 名为新 | 间谍,应该... 应该.. |
| 总是用新调用 | 一直以来 一直以来 一直叫着"新" |
| 调用 | 间谍,应该... 已经.. |
| 总是唤起 | 间谍,应该的,永远的,永远的,永远的 |
| 调用 | 间谍,应该... 已经.. |
| 总是用 | 间谍,应该一直... 一直.. |
| 调用 准确 | 间谍,应该... 已经.. |
| 总是叫 准确 | 间谍,应该的,永远的,永远的,永远的 |
| 名为 Match | 间谍,应该的,应该的,应该的 |
| 总是用匹配调用 | 间谍,应该永远... 一直.. |
| 已返回 | 已返回(返回Val) |
| 总是回归 | 总是有 返回(返回) |
| 丢弃 | 间谍。应该。 已经。 |
| 永远 线索 | 间谍,应该. 总是. 推 (错误的ObjOror Error Type String Or nothing) (错误的ObjOror Error Type String Or nothing) (错误的ObjOrror Error Type String Or nothing) (错误的ObjOrror or or or nothing (错误的ObjOrreor or or or or or or or rerror Type String Or nothing) (错误的Objjor,应该 |
关于每项主张行为的更多信息,见: 相应的间谍方法的文档当然,这些工作不仅针对间谍,而且针对 个别间谍的电话, 棍子,和嘲弄.
请注意,你可以否定任何断言 蔡的 .not. E. g. 用于 notCalled 使用情况 spy.should.have.not.been.called。 。 。 .
对于 assert 界面不需要此库。 您可以安装 Sinon.JS的说法 进入柴家 assert 对象为 expose数字 :
var chai = require("chai");
var sinon = require("sinon");
sinon.assert.expose(chai.assert, { prefix: "" });
实例
使用柴的 should数字 :
"use strict";
var chai = require("chai");
var sinon = require("sinon");
var sinonChai = require("sinon-chai");
chai.should();
chai.use(sinonChai);
function hello(name, cb) {
cb("hello " + name);
}
describe("hello", function () {
it("should call callback with correct greeting", function () {
var cb = sinon.spy();
hello("foo", cb);
cb.should.have.been.calledWith("hello foo");
});
});
使用柴的 expect数字 :
"use strict";
var chai = require("chai");
var sinon = require("sinon");
var sinonChai = require("sinon-chai");
var expect = chai.expect;
chai.use(sinonChai);
function hello(name, cb) {
cb("hello " + name);
}
describe("hello", function () {
it("should call callback with correct greeting", function () {
var cb = sinon.spy();
hello("foo", cb);
expect(cb).to.have.been.calledWith("hello foo");
});
});
安装和使用
节点
做一个 npm install sinon-chai 起来跑步 然后说:
var chai = require("chai");
var sinonChai = require("sinon-chai");
chai.use(sinonChai);
您当然可以将这个代码放入普通的测试固定文件; 例如使用 摩诃迦叶,参见 希农-蔡测试本身。 。 。 .
AMD (英语)
希农 Chai 支持用作一种 AMD (英语) 模块,以匿名方式注册(和Chai一样)。 所以,假设你
已配置您的加载器, 将 Cai 和 Sinon- Chai 文件映射到相应的模块ID "chai" 财务报告和审定财务报表
"sinon-chai",可以按以下方式使用:
define(function (require, exports, module) {
var chai = require("chai");
var sinonChai = require("sinon-chai");
chai.use(sinonChai);
});
<script> 标签
若您直接将 Sinon – Chai 包含在其中,则 <script> 标记,在Chai本身之后,它会自动插入
准备使用 注意您会想要获得西农的最新浏览器构建. 联合来文:
<script src="chai.js"></script>
<script src="sinon-chai.js"></script>
<script src="sinon.js"></script>