Javascript测试框架Jasmine(五):Spies

zz from: http://keenwon.com/1218.html

Spies是Jasmine里面比较高端的方法,可以用来模拟函数的执行,以达到隔离复杂依赖的效果。例如,你要测试列表的处理是否正确,但是数据是异步请求接口取得的,这时你就可以使用Spies,模拟返回各种不同的数据进行测试。

spy可以保存任何函数的调用记录和输入的参数,spy只能存在于describe和it中,在spec执行完之后销毁。先说两个针对spies的Matcher,toHaveBeenCalled 用来测试函数是否被调用过;toHaveBeenCalledWith 用来测试函数被调用时的参数列表,匹配的话返回true ,看个例子:

 
 
  1. describe("A spy", function() {
  2. var foo, bar = null;
  3.  
  4. beforeEach(function() {
  5. foo = {
  6. setBar: function(value) {
  7. bar = value;
  8. }
  9. };
  10.  
  11. spyOn(foo, 'setBar');
  12.  
  13. foo.setBar(123);
  14. foo.setBar(456, 'another param');
  15. });
  16.  
  17. it("tracks that the spy was called", function() {
  18. expect(foo.setBar).toHaveBeenCalled();
  19. });
  20.  
  21. it("tracks all the arguments of its calls", function() {
  22. expect(foo.setBar).toHaveBeenCalledWith(123);
  23. expect(foo.setBar).toHaveBeenCalledWith(456, 'another param');
  24. });
  25.  
  26. it("stops all execution on a function", function() {
  27. expect(bar).toBeNull();
  28. });
  29. });

and.callThrough

spy链式调用and.callThrough 后,在获取spy的同时,调用实际的函数,看示例:

 
 
  1. describe("A spy, when configured to call through", function() {
  2. var foo, bar, fetchedBar;
  3.  
  4. beforeEach(function() {
  5. foo = {
  6. setBar: function(value) {
  7. bar = value;
  8. },
  9. getBar: function() {
  10. return bar;
  11. }
  12. };
  13.  
  14. spyOn(foo, 'getBar').and.callThrough();
  15.  
  16. foo.setBar(123);
  17. fetchedBar = foo.getBar();
  18. });
  19.  
  20. it("tracks that the spy was called", function() {
  21. expect(foo.getBar).toHaveBeenCalled();
  22. });
  23.  
  24. it("should not effect other functions", function() {
  25. expect(bar).toEqual(123);
  26. });
  27.  
  28. it("when called returns the requested value", function() {
  29. expect(fetchedBar).toEqual(123);
  30. });
  31. });

如果不加and.callThrough 的话,最后一个测试中的fetchedBar ,其实只是spyOn 方法的返回值,并不会执行原始的函数,返回123。

and.returnValue

spy链式调用and.returnValue ,返回指定值。

 
 
  1. describe("A spy, when configured to fake a return value", function() {
  2. var foo, bar, fetchedBar;
  3.  
  4. beforeEach(function() {
  5. foo = {
  6. setBar: function(value) {
  7. bar = value;
  8. },
  9. getBar: function() {
  10. return bar;
  11. }
  12. };
  13.  
  14. spyOn(foo, "getBar").and.returnValue(745);
  15.  
  16. foo.setBar(123);
  17. fetchedBar = foo.getBar();
  18. });
  19.  
  20. it("tracks that the spy was called", function() {
  21. expect(foo.getBar).toHaveBeenCalled();
  22. });
  23.  
  24. it("should not effect other functions", function() {
  25. expect(bar).toEqual(123);
  26. });
  27.  
  28. it("when called returns the requested value", function() {
  29. expect(fetchedBar).toEqual(745);
  30. });
  31. });

and.callFake

spy链式调用and.callFake ,执行指定函数。

 
 
  1. describe("A spy, when configured with an alternate implementation", function() {
  2. var foo, bar, fetchedBar;
  3.  
  4. beforeEach(function() {
  5. foo = {
  6. setBar: function(value) {
  7. bar = value;
  8. },
  9. getBar: function() {
  10. return bar;
  11. }
  12. };
  13.  
  14. spyOn(foo, "getBar").and.callFake(function() {
  15. return 1001;
  16. });
  17.  
  18. foo.setBar(123);
  19. fetchedBar = foo.getBar();
  20. });
  21.  
  22. it("tracks that the spy was called", function() {
  23. expect(foo.getBar).toHaveBeenCalled();
  24. });
  25.  
  26. it("should not effect other functions", function() {
  27. expect(bar).toEqual(123);
  28. });
  29.  
  30. it("when called returns the requested value", function() {
  31. expect(fetchedBar).toEqual(1001);
  32. });
  33. });

and.throwError

spy链式调用and.throwError ,抛出异常:

 
 
  1. describe("A spy, when configured to throw an error", function() {
  2. var foo, bar;
  3.  
  4. beforeEach(function() {
  5. foo = {
  6. setBar: function(value) {
  7. bar = value;
  8. }
  9. };
  10.  
  11. spyOn(foo, "setBar").and.throwError("quux");
  12. });
  13.  
  14. it("throws the value", function() {
  15. expect(function() {
  16. foo.setBar(123)
  17. }).toThrowError("quux");
  18. });
  19. });

and.stub

spy恢复到原始状态,不执行任何操作。直接看下源码比较好懂 https://github.com/pivotal/jasmine/blob/master/src/core/SpyStrategy.js 

 
 
  1. describe("A spy", function() {
  2. var foo, bar = null;
  3.  
  4. beforeEach(function() {
  5. foo = {
  6. setBar: function(value) {
  7. bar = value;
  8. }
  9. };
  10.  
  11. spyOn(foo, 'setBar').and.callThrough();
  12. });
  13.  
  14. it("can call through and then stub in the same spec", function() {
  15. foo.setBar(123);
  16. expect(bar).toEqual(123);
  17.  
  18. foo.setBar.and.stub();
  19. bar = null;
  20.  
  21. foo.setBar(123);
  22. expect(bar).toBe(null);
  23. });
  24. });

jasmine.createSpy

当没有函数可以spy的时候,可以创建一个“基础的”spy,它的背后没有任何实际的操作,例如:

 
 
  1. describe("A spy, when created manually", function() {
  2. var whatAmI;
  3.  
  4. beforeEach(function() {
  5. whatAmI = jasmine.createSpy('whatAmI');
  6.  
  7. whatAmI("I", "am", "a", "spy");
  8. });
  9.  
  10. it("is named, which helps in error reporting", function() {
  11. expect(whatAmI.and.identity()).toEqual('whatAmI');
  12. });
  13.  
  14. it("tracks that the spy was called", function() {
  15. expect(whatAmI).toHaveBeenCalled();
  16. });
  17.  
  18. it("tracks its number of calls", function() {
  19. expect(whatAmI.calls.count()).toEqual(1);
  20. });
  21.  
  22. it("tracks all the arguments of its calls", function() {
  23. expect(whatAmI).toHaveBeenCalledWith("I", "am", "a", "spy");
  24. });
  25.  
  26. it("allows access to the most recent call", function() {
  27. expect(whatAmI.calls.mostRecent().args[0]).toEqual("I");
  28. });
  29. });

jasmine.createSpyObj

使用jasmine.createSpyObj 可以模拟多个spies。给jasmine.createSpyObj 传入一个字符串数组,返回一个对象,字符串数组的每一项都是它的spy类型的属性。

 
 
  1. describe("Multiple spies, when created manually", function() {
  2. var tape;
  3.  
  4. beforeEach(function() {
  5. tape = jasmine.createSpyObj('tape', ['play', 'pause', 'stop', 'rewind']);
  6.  
  7. tape.play();
  8. tape.pause();
  9. tape.rewind(0);
  10. });
  11.  
  12. it("creates spies for each requested function", function() {
  13. expect(tape.play).toBeDefined();
  14. expect(tape.pause).toBeDefined();
  15. expect(tape.stop).toBeDefined();
  16. expect(tape.rewind).toBeDefined();
  17. });
  18.  
  19. it("tracks that the spies were called", function() {
  20. expect(tape.play).toHaveBeenCalled();
  21. expect(tape.pause).toHaveBeenCalled();
  22. expect(tape.rewind).toHaveBeenCalled();
  23. expect(tape.stop).not.toHaveBeenCalled();
  24. });
  25.  
  26. it("tracks all the arguments of its calls", function() {
  27. expect(tape.rewind).toHaveBeenCalledWith(0);
  28. });
  29. });

其他跟踪属性

spy的任何调用都可以使用calls 属性跟踪

.calls.any()

如果spy没有被调用过,则返回false,否则返回true

.calls.count()

返回spy被调用的次数

.calls.argsFor(index)

返回spy被第index次调用时的参数

.calls.allArgs()

返回spy所有被调用的参数

.calls.all()

返回spy所有被调用的this 上下文和参数

.calls.mostRecent()

返回spy最近一次被调用的this 上下文和参数

.calls.first()

返回spy第一次被调用的this 上下文和参数

.calls.reset()

清空spy的所有跟踪,看看他们的用法,点这里看在线运行效果:

 
 
  1. describe("Jasmine Spy的跟踪属性", function() {
  2. var foo, bar = null;
  3.  
  4. beforeEach(function() {
  5. foo = {
  6. setBar: function(value) {
  7. bar = value;
  8. }
  9. };
  10.  
  11. spyOn(foo, 'setBar');
  12. });
  13.  
  14. it(".calls.any()示例", function() {
  15. expect(foo.setBar.calls.any()).toEqual(false);
  16.  
  17. foo.setBar();
  18.  
  19. expect(foo.setBar.calls.any()).toEqual(true);
  20. });
  21.  
  22. it(".calls.count()示例", function() {
  23. expect(foo.setBar.calls.count()).toEqual(0);
  24.  
  25. foo.setBar();
  26. foo.setBar();
  27.  
  28. expect(foo.setBar.calls.count()).toEqual(2);
  29. });
  30.  
  31. it(".calls.argsFor(index)示例", function() {
  32. foo.setBar(123);
  33. foo.setBar(456, "baz");
  34.  
  35. expect(foo.setBar.calls.argsFor(0)).toEqual([123]);
  36. expect(foo.setBar.calls.argsFor(1)).toEqual([456, "baz"]);
  37. });
  38.  
  39. it(".calls.allArgs()示例", function() {
  40. foo.setBar(123);
  41. foo.setBar(456, "baz");
  42.  
  43. expect(foo.setBar.calls.allArgs()).toEqual([
  44. [123],
  45. [456, "baz"]
  46. ]);
  47. });
  48.  
  49. it(".calls.all()示例", function() {
  50. foo.setBar(123);
  51.  
  52. expect(foo.setBar.calls.all()).toEqual([{
  53. object: foo,
  54. args: [123]
  55. }]);
  56. });
  57.  
  58. it(".calls.mostRecent()示例", function() {
  59. foo.setBar(123);
  60. foo.setBar(456, "baz");
  61.  
  62. expect(foo.setBar.calls.mostRecent()).toEqual({
  63. object: foo,
  64. args: [456, "baz"]
  65. });
  66. });
  67.  
  68. it(".calls.first()示例", function() {
  69. foo.setBar(123);
  70. foo.setBar(456, "baz");
  71.  
  72. expect(foo.setBar.calls.first()).toEqual({
  73. object: foo,
  74. args: [123]
  75. });
  76. });
  77.  
  78. it(".calls.reset()示例", function() {
  79. foo.setBar(123);
  80. foo.setBar(456, "baz");
  81.  
  82. expect(foo.setBar.calls.any()).toBe(true);
  83.  
  84. foo.setBar.calls.reset();
  85.  
  86. expect(foo.setBar.calls.any()).toBe(false);
  87. });
  88. });

– 下一篇讲一下异步代码测试 –


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
提供的源码资源涵盖了Java应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码中配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 适合毕业设计、课程设计作业。这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程中,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。 所有源码均经过严格测试,可以直接运行,可以放心下载使用。有任何使用问题欢迎随时与博主沟通,第一时间进行解答!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值