【问题标题】:Loading ngMock only in testing context when using Protractor使用 Protractor 时仅在测试上下文中加载 ngMock
【发布时间】:2015-08-03 20:28:27
【问题描述】:

我正在将 Angular 应用程序的所有量角器 E2E 测试切换到 ngMock,以便我们可以模拟我们的资源/http 调用。但是,我找不到在那种情况下加载ngMock 的推荐方法。

我当然不想将脚本本身包含在我的实时应用程序中,但我没有看到通过量角器注入额外 script 元素或动态加载它的明确方法。

【问题讨论】:

    标签: angularjs protractor ngmock ngmocke2e


    【解决方案1】:

    您可以做的是使用grunt-targethtml 或gupl-targethtml 所以在您的index.html 中您可以添加何时添加脚本的条件:

        <!--(if target  mock || e2e)><!-->
        <script src="dev-mocks/mock-utils.js"></script>
        <script src="dev-mocks/modules/authentication-service-mock.js"></script>
        <!--<!(endif)-->
    

    首先配置您的 grunt 任务以使用 target e2e/mock 执行 targethtml 任务

     grunt.registerTask(
        'e2e',
        'Automated tests',
        function(target) {
          var tasks = [
              'clean:server',
              'targethtml:e2e',
              'concurrent:server',
              'autoprefixer',
              'connect:e2e'
            ];
            return grunt.task.run(tasks);
        });
    

    Here 是一个工作模板,包含实现Protractor + CucumberJS + sugar-step 所需的所有任务、包和逻辑(用于轻松同步-异步步骤执行),并且还使用动态注入模拟模块CucumberJS的@Around方法

    【讨论】:

    • 我真的不希望在我的实际应用程序模板中的任何地方使用 ngMock,即使它只是在 gulp 任务中被删除,但部分问题是应用程序中的所有内容都加载了 @ 987654334@.
    • 更简洁的方法是使用 targethtml,否则您将不得不手动执行 targethtml 所做的事情,您是否说过,将 requiredjs 与特定的 gulp 任务一起使用并手动将模拟模块加载到您的 Angular 应用程序中
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-02-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多