Page 284 - AngularJS权威教程
P. 284

264  第 19 章  测试


                          </tr>
                      </tbody>
                 </table>

                 我们的emails数据看起来是下面这样的:
                  [
                      {
                         from: 'ari@fullstack.io',
                          subject: 'ng-book and things'
                      },
                      {
                          from: 'ari@fullstack.io',
                          subject: 'Other things about ng-book and angular'
                      },
                     {
                         from: 'ted@google.com',
                         subject: 'Conference speaking gig'
                     }
                 ];
                 在此,我们要测试两个过滤器:在ng-repeat上设置了预期的那个实时搜索过滤器,以及在
             subject上的(确保它是大写的)过滤器。
                 要确保实时搜索是起作用的,需要在输入字段中输入一个值,并且确保这个字段按照我们的
             预期来变化。

                 it('should filter on the search', function() {
                     expect(repeater('#emailTable tbody tr')).count()
                         .toBe(3);
                     // things过滤出email中的两条数据
                     input('search.$').enter('things');
                     expect(repeater('#emailTable tbody tr')).count()
                         .toBe(2);
                 });
                 可以看到当给search.$字段设置了不同输入时,ng-repeat从3变成了2。
                 也可以测试我们自己的过滤器(大写过滤器),确保它是按照预期方式工作的:

                 it('should capitalize the subject line', function() {
                     expect(
                     repeater('#emailTable tbody tr:first')
                         column('email.subject')
                     ).toEqual(["Ng-book and things"]);
                 });


             19.14.6  测试模板

                 测试模板时,我们着重于确保:特定的内容模板被加载了,模板中特定的数据也在视图中显示了。

                 1. 单元测试模板
                 既然模板是直接绑定到视图的,那么在视图中对组件的单元测试也就没有什么意义了:我们
             断言的建立基础是,视图的创建依赖于多个组件的完成。
                 可以建立一个断言:模板正常加载了。要做到这个,需要建立测试来期望对主页模板的一个
   279   280   281   282   283   284   285   286   287   288   289