Page 257 - AngularJS权威教程
P. 257

19.10  预期    237


                          expect(value).not.toBeGreaterThan(20);                                     19
                     });
                  });

                  12. toBeCloseTo                                                                    20
                  toBeCloseTo()匹配器在一个指定的精度级别内比较一个值是否接近另一个值:
                                                                                                     21
                  describe('A spec suite', function() {
                    it('contains a passing spec', function() {
                      var value = 30.02;
                      expect(value).toBeCloseTo(30, 0);                                              22
                      expect(value).not.toBeCloseTo(20, 2);
                    });
                  });                                                                                23

                  13. toThrow
                  toThrow()匹配器验证一个函数是否抛出了异常:                                                         24

                   describe('A spec suite', function() {
                     it('contains a passing spec', function() {                                      25
                     expect(function() {
                       return a + 10;
                     }).toThrow();
                     expect(function() {                                                             26
                       return 2 + 10;
                     }).not.toThrow();
                   });                                                                               27
                  });
                  14. 创建自定义匹配器
                                                                                                     28
                  在代码中面对更复杂情况时,会需要创建自己的匹配器,Jasmine让这变得非常容易。要创
              建一个匹配器,我们可以在Jasmine块中调用addMatcher()函数,带入一个值:
                                                                                                     29
                  describe('A spec suite', function() {
                    this.addMatchers({
                      toBeLessThanOrEqual: function(expected) {                                      30
                        return this.actual <= expected;
                      }
                    });                                                                              31
                  });
                  然后就可以在测试套件里定义的任意测试中调用这个toBeLessThanOrEqual()匹配器了。
                                                                                                     32

              19.10.2  安装和卸载
                                                                                                     33
                  除了手动在每个测试中设置测试条件,我们可以使用beforeEach方法来运行一组设置函数。
              beforeEach()函数带一个参数:一个函数,在每个细则运行之前被调用一次。它可以在一个描
              述块中使用,就像这样:                                                                            34

                  describe('A spec suite', function() {
                    var message;                                                                     35
                    beforeEach(function() {
                      message = "hello ";
                    });                                                                              36
                    it('should say hello world', function() {
   252   253   254   255   256   257   258   259   260   261   262