摘要:只测试指定测试套件同一层级中出现两个测试将忽略同理,同一层级出现和,将会忽略案例忽略该测试忽略该测试忽略该测试自定义比较函数覆盖比较函数在声明周期使用,返回或,如果返回或则表示使用默认的进行比较自定义匹配规则在声明周期使用加入自定义匹配规则
focus spec
只测试指定测试套件, 同一层级中出现 it, fit 两个测试 spec, 将忽略 it, 同理,同一层级出现 describe和fdescribe,将会忽略desribe
案例</>复制代码
describe("Focused specs", function() {
fit("is focused and will run", function() {
expect(true).toBeTruthy();
});
// 忽略该测试 spec
it("is not focused and will not run", function(){
expect(true).toBeFalsy();
});
fdescribe("focused describe", function(){
it("will run", function(){
expect(true).toBeTruthy();
});
it("will also run", function(){
expect(true).toBeTruthy();
});
});
fdescribe("another focused describe", function(){
// 忽略该测试
fit("is focused and will run", function() {
expect(true).toBeTruthy();
});
it("is not focused and will not run", function(){
expect(true).toBeFalsy();
});
});
// 忽略该测试
describe("ignore describe", function () {
fit("is focused and will run", function() {
expect(true).toBeTruthy();
});
})
});
custom equality
自定义比较函数, 覆盖 toEqual 比较函数. 在beforeEach声明周期使用 jasmine.addCustomEqualityTester, 返回true 或 false, 如果返回 undefined 或 null. 则表示使用默认的toEqual进行比较
</>复制代码
describe("test suite", function () {
beforeEach(function() {
jasmine.addCustomEqualityTester(function (actual, expected) {
var actualType = typeof actual
if (actualType === "string" && actualType === typeof expected) {
return actual[0] === expected[0]
}
});
});
it("should be custom equal", function () {
expect("aa").toEqual("ab");
});
});
custom matchers
自定义匹配规则, 在beforeEach声明周期使用 jasmine.addMatchers 加入自定义匹配规则. 比 custom equality
更完善, 可以自定义错误信息.
</>复制代码
beforeEach(function () {
jasmine.addMatchers({
// TO BE LIKE
toBeLike: function (util, customEqualityTesters) {
return {
// 匹配规则
// 参数 actual 实际传入值
// 参数 expected 期待值
compare: function (actual, expected) {
var pass = false;
var message = "";
var actualType = typeof actual;
var expectedType = typeof expected;
if (actual === expected) {
pass = true;
} else if (actual === null) {
pass = expected === null || expected === undefined;
} else if (actualType === expectedType) {
pass = true;
if (actualType === "object") {
for (name in actual) {
if (actual[name] === null || actual[name] === undefined) {
pass = expected === null || expected === undefined;
} else if (typeof actual[name] !== typeof expected[name]) {
pass = false;
}
if (!pass) {
break;
}
}
}
}
if (pass) {
message = "Expected " + actual + " to be like"
} else {
message = "Expected " + actual + " not to be like"
}
return {
pass: pass,
message: message
};
} // end compare
};
} // end toBeLike function
});
});
使用方式
</>复制代码
describe("to be like suite", () => {
function noop () {}
function noop2 () {}
it("assert to be like", function () {
expect("f").toBeLike("a");
expect(null).toBeLike(undefined);
expect({
msg: "first msg",
cb: noop
}).toBeLike({
msg: "second msg",
cb: noop2
});
});
it("assert no to be like", function () {
expect(1).not.toBeLike("1");
expect(noop).not.toBeLike();
});
});
相关文章推荐
Jasmine基础教程
文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。
转载请注明本文地址:https://www.ucloud.cn/yun/85175.html
摘要:只测试指定测试套件同一层级中出现两个测试将忽略同理,同一层级出现和,将会忽略案例忽略该测试忽略该测试忽略该测试自定义比较函数覆盖比较函数在声明周期使用,返回或,如果返回或则表示使用默认的进行比较自定义匹配规则在声明周期使用加入自定义匹配规则 focus spec 只测试指定测试套件, 同一层级中出现 it, fit 两个测试 spec, 将忽略 it, 同理,同一层级出现 descri...
摘要:介绍是基于测试框架行为驱动测试的定义它是通过用自然语言书写非程序员可读的测试用例扩展了测试驱动开发方法行为驱动开发人员使用混合了领域中统一的语言的母语语言来描述他们的代码的目的函数包含两个参数一个测试套件的名称实现测试套件的代码块函数定义 介绍 Jasmine 是基于 BBD (behavior-driven development)测试框架 行为驱动测试的定义:它是通过用自然语言书写...
摘要:不论你是在写浏览器端还是后端的,总存在那么一个问题我该使用什么单元测试库去确保我的代码如预期的运行呢总是有那么一些流行的框架可供选择。在中仍然流行,并且拥有来自许多地方性的支持。如果你的测试使用它,直到调用了才能通过。 不论你是在写浏览器端javascript还是后端的nodejs,总存在那么一个问题:我该使用什么单元测试库去确保我的代码如预期的运行呢?总是有那么一些流行的框架可供选择...
摘要:而测试驱动开发技术并不只是单纯的测试工作。需求向来就是软件开发过程中感觉最不好明确描述易变的东西。这里说的需求不只是指用户的需求,还包括对代码 可能很多人和我一样, 首次听到前端架构这个词, 第一反应是: 前端还有架构这一说呢? 在后端开发领域, 系统规划和可扩展性非常关键, 因此架构师备受重视, 早在开发工作启动之前, 他们就被邀请加入到项目中, 而且他们会跟客户讨论即将建成的平台的...
摘要:而测试驱动开发技术并不只是单纯的测试工作。需求向来就是软件开发过程中感觉最不好明确描述易变的东西。这里说的需求不只是指用户的需求,还包括对代码 可能很多人和我一样, 首次听到前端架构这个词, 第一反应是: 前端还有架构这一说呢? 在后端开发领域, 系统规划和可扩展性非常关键, 因此架构师备受重视, 早在开发工作启动之前, 他们就被邀请加入到项目中, 而且他们会跟客户讨论即将建成的平台的...
阅读 2419·2021-11-22 14:56
阅读 10970·2021-09-08 10:45
阅读 2099·2019-08-30 13:54
阅读 2934·2019-08-29 16:54
阅读 2097·2019-08-29 14:20
阅读 1858·2019-08-29 12:25
阅读 1922·2019-08-29 12:17
阅读 1121·2019-08-23 18:29