上一篇 Jasmine入门(上) 介绍了Jasmine以及一些基本的用法,本篇我们继续研究Jasmine的其他一些特性及其用法(注:本篇中的例子均来自于官方文档)。

Spy

Spy用来追踪函数的调用历史信息(是否被调用、调用参数列表、被请求次数等)。Spy仅存在于定义它的describe和it方法块中,并且每次在spec执行完之后被销毁。

示例1:

 (function(){
describe("A spy", function() {
var foo, bar = null; beforeEach(function() {
foo = {
setBar: function(value) {
bar = value;
}
}; spyOn(foo, 'setBar'); // 在foo对象上添加spy // 此时调用foo对象上的方法,均为模拟调用,因此不会执行实际的代码
foo.setBar(123); // 调用foo的setBar方法
foo.setBar(456, 'another param');
}); it("tracks that the spy was called", function() {
expect(foo.setBar).toHaveBeenCalled(); //判断foo的setBar是否被调用
}); it("tracks all the arguments of its calls", function() {
expect(foo.setBar).toHaveBeenCalledWith(123); //判断被调用时的参数
expect(foo.setBar).toHaveBeenCalledWith(456, 'another param');
}); it("stops all execution on a function", function() {
expect(bar).toBeNull(); // 由于是模拟调用,因此bar值并没有改变
});
});
})();

从示例1中看到,当在一个对象上使用spyOn方法后即可模拟调用对象上的函数,此时对所有函数的调用是不会执行实际代码的。示例1中包含了两个Spy常用的expect:

toHaveBeenCalled: 函数是否被调用

toHaveBeenCalledWith: 调用函数时的参数

and.callThrough()

那如果说我们想在使用Spy的同时也希望执行实际的代码呢?

示例2:

 (function(){
describe("A spy, when configured to call through", function() {
var foo, bar, fetchedBar; beforeEach(function() {
foo = {
setBar: function(value) {
bar = value;
},
getBar: function() {
return bar;
}
}; spyOn(foo, 'getBar').and.callThrough(); // 与示例1中不同之处在于使用了callThrough,这将时所有的函数调用为真实的执行
//spyOn(foo, 'getBar'); // 可以使用示例1中的模拟方式,看看测试集执行的结果 foo.setBar(123);
fetchedBar = foo.getBar();
}); it("tracks that the spy was called", function() {
expect(foo.getBar).toHaveBeenCalled();
}); it("should not effect other functions", function() {
expect(bar).toEqual(123); // 由于是真实调用,因此bar有了真实的值
}); it("when called returns the requested value", function() {
expect(fetchedBar).toEqual(123); // 由于是真实调用,fetchedBar也有了真实的值
});
});
})();

通过在使用spyOn后面增加了链式调用and.CallThrough(),这将告诉Jasmine我们除了要完成对函数调用的跟踪,同时也需要执行实际的代码。

and.returnValue()

由于Spy是模拟函数的调用,因此我们也可以强制指定函数的返回值。

示例3:

 (function(){
describe("A spy, when configured to fake a return value", function() {
var foo, bar, fetchedBar; beforeEach(function() {
foo = {
setBar: function(value) {
bar = value;
},
getBar: function() {
return bar;
}
}; spyOn(foo, "getBar").and.returnValue(745); // 这将指定getBar方法返回值为745 foo.setBar(123);
fetchedBar = foo.getBar();
}); it("tracks that the spy was called", function() {
expect(foo.getBar).toHaveBeenCalled();
}); it("should not effect other functions", function() {
expect(bar).toEqual(123);
}); it("when called returns the requested value", function() {
expect(fetchedBar).toEqual(745);
});
});
})();

如果被调用的函数是通过从其他函数获取某些值,我们通过使用returnValue模拟函数的返回值。这样做的好处是可以有效的隔离依赖,使测试流程变得更简单。

and.callFake()

与returnValue相似,callFake则更进一步,直接通过指定一个假的自定义函数来执行。这种方式比returnValue更灵活,我们可以任意捏造一个函数来达到我们的测试要求。
示例4:

 (function(){
describe("A spy, when configured with an alternate implementation", function() {
var foo, bar, fetchedBar; beforeEach(function() {
foo = {
setBar: function(value) {
bar = value;
},
getBar: function() {
return bar;
}
}; spyOn(foo, "getBar").and.callFake(function() {
return 1001;
}); foo.setBar(123);
fetchedBar = foo.getBar();
}); it("tracks that the spy was called", function() {
expect(foo.getBar).toHaveBeenCalled();
}); it("should not effect other functions", function() {
expect(bar).toEqual(123);
}); it("when called returns the requested value", function() {
expect(fetchedBar).toEqual(1001);
});
});
})();

and.throwError()

throwError便于我们模拟异常的抛出。

 (function(){
describe("A spy, when configured to throw an error", function() {
var foo, bar; beforeEach(function() {
foo = {
setBar: function(value) {
bar = value;
}
}; spyOn(foo, "setBar").and.throwError("quux");
}); it("throws the value", function() {
expect(function() {
foo.setBar(123)
}).toThrowError("quux");
});
});
})();

and.stub

示例5:

 (function(){
describe("A spy", function() {
var foo, bar = null; beforeEach(function() {
foo = {
setBar: function(value) {
bar = value;
},
getBar: function(){
return bar;
}
}; spyOn(foo, 'setBar').and.callThrough(); // 标记1
spyOn(foo, 'getBar').and.returnValue(999); // 标记2
}); it("can call through and then stub in the same spec", function() {
foo.setBar(123);
expect(bar).toEqual(123); var getValue = foo.getBar();
expect(getValue).toEqual(999); foo.setBar.and.stub(); // 相当于'标记1'中的代码变为了spyOn(foo, 'setBar')
foo.getBar.and.stub(); // 相当于'标记2'中的代码变为了spyOn(foo, 'getBar')
bar = null; foo.setBar(123);
expect(bar).toBe(null);
expect(foo.setBar).toHaveBeenCalled(); // 函数调用追踪并没有被重置 getValue = foo.getBar();
expect(getValue).toEqual(undefined);
expect(foo.getBar).toHaveBeenCalled(); // 函数调用追踪并没有被重置
});
});
})();

其他追踪属性: 

calls:对于被Spy的函数的调用,都可以在calls属性中跟踪。

  • .calls.any(): 被Spy的函数一旦被调用过,则返回true,否则为false;
  • .calls.count(): 返回被Spy的函数的被调用次数;
  • .calls.argsFor(index): 返回被Spy的函数的调用参数,以index来指定参数;
  • .calls.allArgs():返回被Spy的函数的所有调用参数;
  • .calls.all(): 返回calls的上下文,这将返回当前calls的整个实例数据;
  • .calls.mostRecent(): 返回calls中追踪的最近一次的请求数据;
  • .calls.first(): 返回calls中追踪的第一次请求的数据;
  • .object: 当调用all(),mostRecent(),first()方法时,返回对象的object属性返回的是当前上下文对象;
  • .calls.reset(): 重置Spy的所有追踪数据;

示例6:

 (function(){
describe("A spy", function() {
var foo, bar = null; beforeEach(function() {
foo = {
setBar: function(value) {
bar = value;
}
}; spyOn(foo, 'setBar');
}); it("tracks if it was called at all", function() {
expect(foo.setBar.calls.any()).toEqual(false); foo.setBar(); expect(foo.setBar.calls.any()).toEqual(true);
}); it("tracks the number of times it was called", function() {
expect(foo.setBar.calls.count()).toEqual(0); foo.setBar();
foo.setBar(); expect(foo.setBar.calls.count()).toEqual(2);
}); it("tracks the arguments of each call", function() {
foo.setBar(123);
foo.setBar(456, "baz"); expect(foo.setBar.calls.argsFor(0)).toEqual([123]);
expect(foo.setBar.calls.argsFor(1)).toEqual([456, "baz"]);
}); it("tracks the arguments of all calls", function() {
foo.setBar(123);
foo.setBar(456, "baz"); expect(foo.setBar.calls.allArgs()).toEqual([[123],[456, "baz"]]);
}); it("can provide the context and arguments to all calls", function() {
foo.setBar(123); expect(foo.setBar.calls.all()).toEqual([{object: foo, args: [123], returnValue: undefined}]);
}); it("has a shortcut to the most recent call", function() {
foo.setBar(123);
foo.setBar(456, "baz"); expect(foo.setBar.calls.mostRecent()).toEqual({object: foo, args: [456, "baz"], returnValue: undefined});
}); it("has a shortcut to the first call", function() {
foo.setBar(123);
foo.setBar(456, "baz"); expect(foo.setBar.calls.first()).toEqual({object: foo, args: [123], returnValue: undefined});
}); it("tracks the context", function() {
var spy = jasmine.createSpy('spy');
var baz = {
fn: spy
};
var quux = {
fn: spy
};
baz.fn(123);
quux.fn(456); expect(spy.calls.first().object).toBe(baz);
expect(spy.calls.mostRecent().object).toBe(quux);
}); it("can be reset", function() {
foo.setBar(123);
foo.setBar(456, "baz"); expect(foo.setBar.calls.any()).toBe(true); foo.setBar.calls.reset(); expect(foo.setBar.calls.any()).toBe(false);
});
});
})();

createSpy

假如没有函数可以追踪,我们可以自己创建一个空的Spy。创建后的Spy功能与其他的Spy一样:跟踪调用、参数等,但该Spy没有实际的代码实现,这种方式经常会用在对JavaScript中的对象的测试。

示例7:

 (function(){
describe("A spy, when created manually", function() {
var whatAmI; beforeEach(function() {
whatAmI = jasmine.createSpy('whatAmI'); whatAmI("I", "am", "a", "spy");
}); it("is named, which helps in error reporting", function() {
expect(whatAmI.and.identity()).toEqual('whatAmI');
}); it("tracks that the spy was called", function() {
expect(whatAmI).toHaveBeenCalled();
}); it("tracks its number of calls", function() {
expect(whatAmI.calls.count()).toEqual(1);
}); it("tracks all the arguments of its calls", function() {
expect(whatAmI).toHaveBeenCalledWith("I", "am", "a", "spy");
}); it("allows access to the most recent call", function() {
expect(whatAmI.calls.mostRecent().args[0]).toEqual("I");
});
});
})(); 

createSpyObj

如果需要spy模拟多个函数调用,可以向jasmine.createSpyObj中传入一个字符串数组,它将返回一个对象,你所传入的所有字符串都将对应一个属性,每个属性即为一个Spy。

示例8:

 (function(){
describe("Multiple spies, when created manually", function() {
var tape; beforeEach(function() {
tape = jasmine.createSpyObj('tape', ['play', 'pause', 'stop', 'rewind']); tape.play();
tape.pause();
tape.rewind(0);
}); it("creates spies for each requested function", function() {
expect(tape.play).toBeDefined();
expect(tape.pause).toBeDefined();
expect(tape.stop).toBeDefined();
expect(tape.rewind).toBeDefined();
}); it("tracks that the spies were called", function() {
expect(tape.play).toHaveBeenCalled();
expect(tape.pause).toHaveBeenCalled();
expect(tape.rewind).toHaveBeenCalled();
expect(tape.stop).not.toHaveBeenCalled();
}); it("tracks all the arguments of its calls", function() {
expect(tape.rewind).toHaveBeenCalledWith(0);
});
});
})();

关于createSpy和createSpyObj,读到这里大家可能很难理解其真正的应用场景。不过没关系,后续的例子中也包含了其用法,大家应该能慢慢理解如何运用它们。

其他匹配方式

jasmine.any

jasmine.any方法以构造器或者类名作为参数,Jasmine将判断期望值和真实值的构造器是否相同,若相同则返回true。

示例9:

 (function(){
describe("jasmine.any", function() {
it("matches any value", function() {
expect({}).toEqual(jasmine.any(Object));
expect(12).toEqual(jasmine.any(Number));
}); describe("when used with a spy", function() {
it("is useful for comparing arguments", function() {
var foo = jasmine.createSpy('foo');
foo(12, function() {
return true;
}); expect(foo).toHaveBeenCalledWith(jasmine.any(Number), jasmine.any(Function));
});
});
});
})();

jasmine.anything

jamine.anything判断只要不是null或undefined的值,若不是则返回true。

示例10:

 (function(){
describe("jasmine.anything", function() {
it("matches anything", function() {
expect(1).toEqual(jasmine.anything());
}); describe("when used with a spy", function() {
it("is useful when the argument can be ignored", function() {
var foo = jasmine.createSpy('foo');
foo(12, function() {
return false;
}); expect(foo).toHaveBeenCalledWith(12, jasmine.anything());
});
});
});
})();

jasmine.objectContaining

jasmine.objectContaining用来判断对象中是否存在指定的键值属性对。

示例11:

 describe("jasmine.objectContaining", function() {
var foo; beforeEach(function() {
foo = {
a: 1,
b: 2,
bar: "baz"
};
}); it("matches objects with the expect key/value pairs", function() {
expect(foo).toEqual(jasmine.objectContaining({
bar: "baz"
}));
expect(foo).not.toEqual(jasmine.objectContaining({
c: 37
}));
}); describe("when used with a spy", function() {
it("is useful for comparing arguments", function() {
var callback = jasmine.createSpy('callback'); callback({
bar: "baz"
}); expect(callback).toHaveBeenCalledWith(jasmine.objectContaining({
bar: "baz"
}));
expect(callback).not.toHaveBeenCalledWith(jasmine.objectContaining({
c: 37
}));
});
});
});

jasmine.arrayContaining

jasmine.arrayContaining可以用来判断数组中是否有期望的值。

示例12:

 (function(){
describe("jasmine.arrayContaining", function() {
var foo; beforeEach(function() {
foo = [1, 2, 3, 4];
}); it("matches arrays with some of the values", function() {
expect(foo).toEqual(jasmine.arrayContaining([3, 1])); // 直接在期望值中使用jasmine.arrayContaining达到目的
expect(foo).not.toEqual(jasmine.arrayContaining([6]));
}); describe("when used with a spy", function() {
it("is useful when comparing arguments", function() {
var callback = jasmine.createSpy('callback'); // 创建一个空的Spy callback([1, 2, 3, 4]); // 将数组内容作为参数传入Spy中 expect(callback).toHaveBeenCalledWith(jasmine.arrayContaining([4, 2, 3]));
expect(callback).not.toHaveBeenCalledWith(jasmine.arrayContaining([5, 2]));
});
});
});
})();

jasmine.stringMatching

jasmine.stringMatching用来模糊匹配字符串,在jasmine.stringMatching中也可以使用正则表达式进行匹配,使用起来非常灵活。

示例13:

 describe('jasmine.stringMatching', function() {
it("matches as a regexp", function() {
expect({foo: 'bar'}).toEqual({foo: jasmine.stringMatching(/^bar$/)});
expect({foo: 'foobarbaz'}).toEqual({foo: jasmine.stringMatching('bar')});
}); describe("when used with a spy", function() {
it("is useful for comparing arguments", function() {
var callback = jasmine.createSpy('callback'); callback('foobarbaz'); expect(callback).toHaveBeenCalledWith(jasmine.stringMatching('bar'));
expect(callback).not.toHaveBeenCalledWith(jasmine.stringMatching(/^bar$/));
});
});
});

不规则匹配(自定义匹配):asymmetricMatch

某些场景下,我们希望能按照自己设计的规则进行匹配,此时我们可以自定义一个对象,该对象只要包含一个名为asymmetricMatch的方法即可。

示例14:

 describe("custom asymmetry", function() {
var tester = {
asymmetricMatch: function(actual) {
var secondValue = actual.split(',')[1];
return secondValue === 'bar';
}
}; it("dives in deep", function() {
expect("foo,bar,baz,quux").toEqual(tester);
}); describe("when used with a spy", function() {
it("is useful for comparing arguments", function() {
var callback = jasmine.createSpy('callback'); callback('foo,bar,baz'); expect(callback).toHaveBeenCalledWith(tester);
});
});
});

注:示例中的asymmetricMatch方法使我们判断字符串以','分割之后,index为1的内容为'bar'。

Jasmine Clock

Jasmine中可以使用jasmine.clock()方法来模拟操纵时间。

要想使用jasmine.clock(),先调用jasmine.clock().install告诉Jasmine你想要在spec或者suite操作时间,当你不需要使用时,务必调用jasmine.clock().uninstall来恢复时间状态。

示例15:

 (function(){
describe("Manually ticking the Jasmine Clock", function() {
var timerCallback; beforeEach(function() {
timerCallback = jasmine.createSpy("timerCallback");
jasmine.clock().install();
}); afterEach(function() {
jasmine.clock().uninstall();
}); it("causes a timeout to be called synchronously", function() {
setTimeout(function() {
timerCallback();
}, 100); expect(timerCallback).not.toHaveBeenCalled(); jasmine.clock().tick(101); expect(timerCallback).toHaveBeenCalled();
}); it("causes an interval to be called synchronously", function() {
setInterval(function() {
timerCallback();
}, 100); expect(timerCallback).not.toHaveBeenCalled(); jasmine.clock().tick(101);
expect(timerCallback.calls.count()).toEqual(1); jasmine.clock().tick(50);
expect(timerCallback.calls.count()).toEqual(1); jasmine.clock().tick(50);
expect(timerCallback.calls.count()).toEqual(2);
}); describe("Mocking the Date object", function(){
it("mocks the Date object and sets it to a given time", function() {
var baseTime = new Date(2013, 9, 23); jasmine.clock().mockDate(baseTime); jasmine.clock().tick(50);
expect(new Date().getTime()).toEqual(baseTime.getTime() + 50);
});
});
});
})();

示例中使用jasmine.clock().tick(milliseconds)来控制时间前进,本例中出现了三种时间控制方式:

  • setTimeout: 定期执行一次,当jasmine.clock().tick()的时间超过了timeout设置的时间时触发
  • setInterval: 定期循环执行,每当jasmine.clock().tick()的时间超过了timeout设置的时间时触发
  • mockDate: 模拟一个指定日期(当不提供基准时间参数时,以当前时间为基准时间)

异步支持

Jasmine可以支持spec中执行异步操作,当调用beforeEach, it和afterEach时,函数可以包含一个可选参数done,当spec执行完毕之后,调用done通知Jasmine异步操作已执行完毕。

示例16:

 (function(){
describe("Asynchronous specs", function() {
var value; beforeEach(function(done) {
setTimeout(function() {
value = 0;
done();
}, 1);
}); // 在上面beforeEach的done()被执行之前,这个测试用例不会被执行
it("should support async execution of test preparation and expectations", function(done) {
value++;
expect(value).toBeGreaterThan(0);
done(); // 执行完done()之后,该测试用例真正执行完成
}); // Jasmine异步执行超时时间默认为5秒,超过后将报错
describe("long asynchronous specs", function() { // 如果要调整指定用例的默认的超时时间,可以在beforeEach,it和afterEach中传入一个时间参数
beforeEach(function(done) {
// setTimeout(function(){}, 2000); // 可以试试如果该方法执行超过1秒时js会报错
done();
}, 1000); it("takes a long time", function(done) {
setTimeout(function() {
done();
}, 9000);
}, 10000); afterEach(function(done) {
done();
}, 1000);
});
});
})();

关于用法在代码中已经加了注释,另外补充一点,如果需要设置全局的默认超时时间,可以设置jasmine.DEFAULT_TIMEOUT_INTERVAL的值。

当异步执行时间超过设置的执行超时时间js将会报错:

至此为止,我们已经了解了Jasmine的所有用法,Jasmine的基本语法并不复杂,但是想要运用熟练还是需要在实际项目中慢慢实践。

参考资料

Jasmine官方:http://jasmine.github.io/2.3/introduction.html

KeenWon:http://keenwon.com/1218.html

Jasmine入门(下)的更多相关文章

  1. Jasmine入门(上)

    什么是Jasmine Jasmine是一个Javascript的BDD(Behavior-Driven Development)测试框架,不依赖任何其他框架. 如何使用Jasmine 从Github上 ...

  2. Jasmine入门(结合示例讲解)

    参考: http://www.cnblogs.com/wushangjue/p/4541209.html http://keenwon.com/1191.html http://jasmine.git ...

  3. jasmine入门

    本文来自http://blog.fens.me/nodejs-jasmine-bdd 粉丝日志 张丹   前言TDD(Test Driven Development)测试驱动开发,是敏捷开发中提出的最 ...

  4. Python 装饰器入门(下)

    继续上次的进度:https://www.cnblogs.com/flashBoxer/p/9847521.html 正文: 装饰类 在类中有两种不通的方式使用装饰器,第一个和我们之前做过的函数非常相似 ...

  5. Django入门(下)

    一.创建APP 在每一个django项目中可以包含多个APP,相当于一个大型项目中的分系统.子模块.功能部件等.互相之间比较独立,但也有联系. 在pycharm下方的Terminal终端中输入命令: ...

  6. 网易云课堂_C++程序设计入门(下)_第10单元:月映千江未减明 – 模板_第10单元 - 单元作业:OJ编程 - 创建数组类模板

    第10单元 - 单元作业:OJ编程 - 创建数组类模板 查看帮助 返回   温馨提示: 1.本次作业属于Online Judge题目,提交后由系统即时判分. 2.学生可以在作业截止时间之前不限次数提 ...

  7. 网易云课堂_C++程序设计入门(下)_第9单元:白公曾咏牡丹芳,一种鲜妍独“异常”_第9单元 - 作业5:OJ编程 - 使用异常进行图形类的错误处理

    第9单元 - 作业5:OJ编程 - 使用异常进行图形类的错误处理 查看帮助 返回   温馨提示: 1.本次作业属于Online Judge题目,提交后由系统即时判分. 2.学生可以在作业截止时间之前 ...

  8. 网易云课堂_C++程序设计入门(下)_第7单元:出入虽同趣,所向各有宜 – 文件输入和输出_第7单元 - 作业2:编程互评

    第7单元 - 作业2:编程互评 查看帮助 返回   提交作业(剩余10天) 完成并提交作业     作业批改 互评训练   互评作业   自评作业     成绩公布 查看成绩   由于在线编程不支持 ...

  9. Pytorch入门下 —— 其他

    本节内容参照小土堆的pytorch入门视频教程. 现有模型使用和修改 pytorch框架提供了很多现有模型,其中torchvision.models包中有很多关于视觉(图像)领域的模型,如下图: 下面 ...

随机推荐

  1. 跳出思维定势,改变交谈习惯zz

    一直以来我都是一个不折不扣的作者所划分的内向者,羞于在公众场合说话,也不愿意与陌生人交谈,甚至是与认识的人聊天,有时候也是一种痛苦,看着在办公室里夸夸其谈的同事们,我总是感觉格格不入.严格说来,我算是 ...

  2. CentOS yum 源的配置与使用(引用)

    http://www.cnblogs.com/mchina/archive/2013/01/04/2842275.html

  3. 201521123014《Java程序设计》第1周学习总结

    201521123014 java第一周总结 1.本周学习总结 刚认识这一门新语言,我就充满了好奇心,想看看Java和学过C语言,C++有什么区别.在这一周的学习中,我认识到,对于初学者而言,Java ...

  4. 笔记:CSS常用中文字体英文名称对照表

    * 分类排序:中文名+英文名+Unicode+Unicode 2 Windows平台新细明体 PMingLiU \65B0\7EC6\660E\4F53 新细明体细明体 MingLiU \7EC6\6 ...

  5. ZOJ2481 Unique Ascending Array 2017-04-18 23:08 33人阅读 评论(0) 收藏

    Unique Ascending Array Time Limit: 2 Seconds      Memory Limit: 65536 KB Given an array of integers ...

  6. word中添加引文操作

    word中添加引文操作 转化为pdf的时候,可以通过引文处进行ctrl + 左键点击,挑战到相应的后文中的参考引文位置. 1.在文章末尾添加如下内容,并在他的下面添加一条被引文的格式

  7. ESP32应用程序的内存布局

    应用程序内存布局 ESP32芯片具有灵活的内存映射功能.本节介绍ESP-IDF在默认情况下如何使用这些功能. ESP-IDF中的应用程序代码可以放置在以下内存区域之一中. IRAM(指令RAM) ES ...

  8. Linux内存子系统及常用调优参数

    1>内存子系统 1>组件: slab    allocator buddy    system kswapd pdflush 2>虚拟化环境: PA:进程地址: HA:虚拟机地址: ...

  9. 团队合作项目—(GG队)

    团队展示 一.队名:GG 二.队员信息 队员 学号 叶尚文(队长) 3116008802 蔡晓晴 3216008808 杜婷萱 3216008809 龙剑初 3116004647 于泽浩 311600 ...

  10. Eclipse的bug,SunTlsRsaPremasterSecret KeyGenerator not available

    这个bug出现在安装完java后,不设置环境变量,直接打开eclipse 以后只要用到SSL相关(即RSA密钥加密相关)的代码,都会报这个错误,该方法在lib\ext的sunjce_provider. ...