【问题标题】:AngularJs testing. Where to put templateAngularJs 测试。在哪里放置模板
【发布时间】:2015-06-30 11:09:11
【问题描述】:

我目前正在测试一个 angularjs 指令。该指令有一个 templateUrl。我想测试视图以确保它已正确初始化,即正确数量的按钮、隐藏的某些元素。

我遇到的问题是,当我将 html 文件插入模板缓存时,我仍然得到:

"message": "Unexpected request: GET partials/stuff/stuff-leader.html

我假设当我使用 templateCache 时,我将不再需要使用:

 $httpBackend.whenGET("partials/stuff/stuff-leader.html").respond([{
        userId: 000
    }]);

但情况似乎并非如此。我想知道我是否正确插入了模板,这是我的做法:

 template = $templateCache.get('/full/root/disk/path/to/file/stuff/stuff-leader.html');
    $templateCache.put('/myApp/templates/stuff-leader.html',template);

这是正确的,还是我应该把它放在其他地方?

【问题讨论】:

  • 您是否应该将其放在与查找时相同的相对路径(即使用partials/stuff 而不是templates/
  • 谢谢。这很有魅力。如果您想提出答案,我会接受。

标签: javascript angularjs unit-testing karma-runner angular-template


【解决方案1】:

您的普通模板在/partials/stuff/stuff-leader.html 中查找,因此您需要将其注入模板缓存而不是/myApp/templates/stuff-leader.html

【讨论】:

    【解决方案2】:

    您正在使用$templateCache.get 执行请求。而是这样做:

     beforeEach(inject(function ($templateCache) { $templateCache.put('partials/stuff/stuff-leader.html', '< div >...TemplateCode....< /div >'); })); 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-09-06
      • 2020-06-07
      • 2016-07-29
      • 1970-01-01
      • 1970-01-01
      • 2022-08-02
      • 2016-09-23
      相关资源
      最近更新 更多