Javascript测试框架Jasmine(五):Spies

来源:互联网 发布:网络推广公司wtg168 编辑:程序博客网 时间:2024/06/06 13:10

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