【问题标题】:Test utils files in Angular. What are the Best practices?在 Angular 中测试 utils 文件。最佳实践是什么?
【发布时间】:2019-11-14 08:35:50
【问题描述】:

在 Angular 中,我想分享一些在我的测试中使用的实用程序。

这样做的最佳做法/标准是什么?

即要测试组件/服务/无论您创建一个具有相同名称和扩展名.spec.ts 的文件并放置在同一文件夹中。对于测试工具来说,最好的放置和命名是什么?也以.spec.ts结尾?

【问题讨论】:

    标签: angular testing karma-jasmine


    【解决方案1】:

    使用文件扩展名 .spec.ts 以便 Karma 可以识别包含测试的文件。这是您在 test.ts 文件中指定的内容。

    const context = require.context('./', true, /\.spec\.ts$/);
    

    我会将测试 utils 类放置在 src/app/shared/testing 文件夹中的每个 .ts 文件中。为了提高测试的可靠性(并提高整体代码覆盖率统计数据),您还应该为每个测试实用程序类创建一个对应的 .spec.ts 文件。

    【讨论】:

      【解决方案2】:

      Util 函数可以在没有任何 TestBed 的情况下进行测试。也不需要初始化任何东西。考虑 util 函数应该是纯函数(无状态)。 为不同的功能创建不同的块是好的。 其他通用最佳实践可以在这里找到:https://github.com/CareMessagePlatform/jasmine-styleguide

      // test cases for isEmpty function
      describe('CommonUtil', function () {
        describe('.isEmpty(x)', function () {
          it('should returns true when x is null', function () {
            expect(CommonUtil.isEmpty(null)).toBeTrue();
          });
      
          it('should returns false when x is an object', function () {
            expect(CommonUtil.isEmpty({ dummy: 123 })).toBeFalse();
          });
        });
      });
      

      【讨论】:

        猜你喜欢
        • 2019-03-16
        • 1970-01-01
        • 2010-09-17
        • 1970-01-01
        • 2011-08-16
        • 1970-01-01
        • 2015-11-22
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多