[关闭]
@buluoXu 2015-12-29T06:44:18.000000Z 字数 4695 阅读 1816

jasmine api 及使用文档

karma单元测试


jasmine api 及使用文档


jasmine官网

API


toEqual

值和对象的比较

  1. var foo = 0;
  2. foo += 1;
  3. expect(foo).toEqual(1)

toBe

简单类型的比较使用===运算符进行精确比较

  1. //expect(value).toBe(value)
  2. expect(true).toBe(false)

toBeDefined

检查value当前是否具有任何已定义的类型(即value !== undefined)

  1. var tape;
  2. beforeEach(function() {
  3. tape = jasmine.createSpyObj('tape', ['play', 'pause', 'stop', 'rewind']);
  4. tape.play();
  5. tape.pause();
  6. tape.rewind(0);
  7. });
  8. it("creates spies for each requested function", function() {
  9. expect(tape.play).toBeDefined();
  10. expect(tape.pause).toBeDefined();
  11. expect(tape.stop).toBeDefined();
  12. expect(tape.rewind).toBeDefined();
  13. });

toBeTruthy/toBeFalsy

这两个匹配器使用JavaScript标准的真值规则进行判断

  1. var a, foo = "foo";
  2. expect(foo).toBeTruthy();
  3. expect(a).not.toBeTruthy();
  4. expect(a).toBeFalsy();
  5. expect(foo).not.toBeFalsy();

toMatch

检查value是否符合指定的正则表达式。正则表达式既可以用字符串的形式传入,也可以用正则表达式对象的形式(如new RegExp('.')或/./)传入。

  1. //expect(value).toMatch(expectedRegExp)
  2. var message = "foo bar baz";
  3. expect(message).toMatch(/bar/);
  4. expect(message).toMatch("bar");
  5. expect(message).not.toMatch(/quux/);

toBeNull

使用===精确检查null值

  1. //expect(value).toBeNull()
  2. var a = null;
  3. var foo = "foo";
  4. expect(null).toBeNull();
  5. expect(a).toBeNull();
  6. expect(foo).not.toBeNull();

toContain

内部用Array.indexOf(...)函数检查指定的元素是否包含在当前数组中。

  1. //expect(value).toContain(expected)
  2. var a = ["foo", "bar", "baz"];
  3. expect(a).toContain("bar");
  4. expect(a).not.toContain("quux");

toBeLessThan/toBeGreaterThan

使用<>运算符进行数值比较。

  1. var pi = 3.1415926,
  2. e = 2.78;
  3. expect(pi).toBeGreaterThan(e)
  4. expect(e).not.toBeGreaterThan(pi)

toBeUndefined

匹配undefined

  1. expect(value).toBeUndefined(expected)

toBeCloseTo

匹配精密的数据公式

  1. var pi = 3.1415926,
  2. e = 2.78;
  3. expect(pi).not.toBeCloseTo(e, 2);
  4. expect(pi).toBeCloseTo(e, 0);

toThrow

用于测试一个函数抛出一个异常

  1. var foo = function() {
  2. return 1 + 2;
  3. };
  4. var bar = function() {
  5. return a + 1;
  6. };
  7. expect(foo).not.toThrow();
  8. expect(bar).toThrow();

toThrowError

测试特定抛出异常

  1. var foo, bar;
  2. beforeEach(function() {
  3. foo = {
  4. setBar: function(value) {
  5. bar = value;
  6. }
  7. };
  8. spyOn(foo, "setBar").and.throwError("quux");
  9. });
  10. it("throws the value", function() {
  11. expect(function() {
  12. foo.setBar(123)
  13. }).toThrowError("quux");
  14. });

使用 beforeEach 和 afterEach

beforeEach:每次describe调用开始前调用(遇到describe,就会执行一次,有多个,就会执行多次)
afterEach:每次describe调用结束后调用(遇到describe,就会执行一次,有多个,就会执行多次)

  1. var foo = 0;
  2. beforeEach(function() {
  3. foo += 1;
  4. });
  5. afterEach(function() {
  6. foo = 0;
  7. });
  8. it("is just a function, so it can contain any code", function() {
  9. expect(foo).toEqual(1);
  10. });
  11. it("can have more than one expectation", function() {
  12. expect(foo).toEqual(1);
  13. expect(true).toEqual(true);
  14. });

使用 beforeAll 和 afterAll

beforeAll:所有 describe 开始测试前只会调用一次(不管有多少个 describe ,只会调用一次)
afterAll:所有 describe 测试结束后调用(不管有多少个 describe ,只会调用一次)

  1. var foo;
  2. beforeAll(function() {
  3. foo = 1;
  4. });
  5. afterAll(function() {
  6. foo = 0;
  7. });
  8. it("sets the initial value of foo before specs run", function() {
  9. expect(foo).toEqual(1);
  10. foo += 1;
  11. });
  12. it("does not reset foo between specs", function() {
  13. expect(foo).toEqual(2);
  14. });

and.returnValue

通过 and.returnValue 链接断言,所有调用函数都会返回一个具体的值

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

and.returnValues

通过 and.returnValues 链接断言,所有调用的函数将按顺序返回具体的值,直到它到达结束返回值列表,这时它会为所有后续的调用函数指出返回未定义。

  1. describe("A spy, when configured to fake a series of return values", function() {
  2. var foo, bar;
  3. beforeEach(function() {
  4. foo = {
  5. setBar: function(value) {
  6. bar = value;
  7. },
  8. getBar: function() {
  9. return bar;
  10. }
  11. };
  12. spyOn(foo, "getBar").and.returnValues("fetched first", "fetched second");
  13. foo.setBar(123);
  14. });
  15. it("tracks that the spy was called", function() {
  16. foo.getBar(123);
  17. expect(foo.getBar).toHaveBeenCalled();
  18. });
  19. it("should not affect other functions", function() {
  20. expect(bar).toEqual(123);
  21. });
  22. it("when called multiple times returns the requested values in order", function() {
  23. expect(foo.getBar()).toEqual("fetched first");
  24. expect(foo.getBar()).toEqual("fetched second");
  25. expect(foo.getBar()).toBeUndefined();
  26. });
  27. });

and.callFake

通过 and.callFake 链接断言, 所有调用断言将代表所提供的功能。

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

其他使用方法


调用错误的方法

  1. var foo = function(x, callBack) {
  2. if (x) {
  3. callBack();
  4. }
  5. };
  6. it("should not call the callBack", function() {
  7. foo(false, function() {
  8. fail("Callback has been called");
  9. });
  10. });
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注