- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
编辑:在提出问题后,我现在正在编辑它以详细说明我的发现。
我的应用程序是使用指令模块化的。我正在编写我的指令,以便它们 (1) 创建自己的范围 (2) 使用 templateUrl,以及 (3) 在其 Controller 中执行大部分逻辑和服务器数据获取。
问题是如何使用 Mocha 和 Karma 对其进行单元测试。
最佳答案
为每个指令编写一个测试。由于指令使用了templateUrl,所以我使用了html2js。 html 键应作为模块包含 - 将模板放入 templateCache。
然后,我编译了指令,并使用 rootScope 运行链接函数。我在获取模板 html 时遇到问题 - 使用 $digest 解决。有数据绑定(bind)问题,通过理解解决。所有记录在下面。
下面的代码,
指示:
angular.module('myApp')
.directive('productThumb', function(){
return {
restrict: 'AE',
scope: true,
templateUrl: 'partials/directives/product-thumb.html',
// controller does most of the work
controller: ProductThumbController
}
}) ;
describe("Unit: Testing Directives", function() {
var elm, scope, linkFn;
beforeEach(
module('ogApp','partials/directives/product-thumb.html') // puts product-thumb.html
// into templateCache
);
beforeEach(inject(function($rootScope, $compile) {
elm = angular.element('<product-thumb></product-thumb>');
scope = $rootScope;
linkFn = $compile(elm);
scope.$digest(); // have to digest to bring html from templateCache
console.log('post compile',elm.html());// <== the html here still have {{}}
}));
it('should show a thumb',function() {
inject(function($controller) {
linkFn(scope); // <== this will create a new scope (since our directive creates
// new scope), runs the controller with it, and bind
// the element to that new scope
scope.$digest();
console.log('post link',elm.html());// <== the html is bound
// inject test data (controller sets up an $on handler for addProductData event)
scope.$broadcast('addProductData',{title:"TEST DISPLAY NAME",
productId: "123", mainImageUrl: "TEST.JPG"});
scope.$digest();
expect(elm.find('H5').text()).to.equal("TEST DISPLAY NAME"); // <=== success
});
});
关于angularjs - 如何测试使用 templateUrl 和 Controller 的指令?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17223850/
我是 AngularJs 的新手。我正在编写我的自定义 angular 指令,其中包含一些 html 内容的模板。当我使用带有以下代码的模板时,它工作正常。 demoApp.directive('de
我的 AngularJS 路由有问题:我没有得到任何页面反馈。没有错误或 View 切换。 我检查了模块的实现,但它以正确的方式声明。然后我搜索了诸如templateURL之类的错别字。 ,但我没有找
当 templateUrl 分配给指向 Controller 的路由时导致 404 错误或 500 错误时,有谁知道如何在 AngularJS 中捕获错误?当发生故障时,我在控制台中看到了问题,但我似
刚接触 angular,在过去一个小时左右的时间里,我一直在处理什么应该是一个简单的指令时遇到问题。真的很感激一些帮助! 我相信你好应该出现,似乎无法让它工作? 测试.html
我正在处理一组 Angular 指令,我想根据属性的存在或值加载正确的模板。 如果是水平的,templateUrl 应该是 /partials/horizontal-form和 如果是垂直的,
标题组件 import { Component } from "angular2/core"; @Component({ selector: "header-component", t
这个问题已经有答案了: Dynamic template URLs in Angular 2 (10 个回答) 已关闭 6 年前。 我想知道是否可以制作templateUrl取决于ifelse . i
我有一个如下所示的 html 元素: 我想让我的指令根据 type 是什么使用不同的 templateUrl。 appDirectives.directive('newElement', funct
这是我的自定义指令 centraApp.directive('counter', function () { return { restrict: 'A', s
我真的需要解决以下问题的帮助: 我尝试实现应用程序的一些设置,因此我想使用UI-Bootstrap Accordion 。 我有以下HTML代码: “ Acc
我按照 angular.io 入门项目使用种子结构。到目前为止一切正常。 现在我想更改顶部组件以从单独的文件中获取 View ,但我遇到了麻烦。工作代码是: import {Component, Vi
我正在尝试在 Angular js 2 中使用以下代码加载 Html 文件。该文件位于产品文件夹下,我尝试加载的 html 文件也在同一文件夹中。仍然无法正常工作。 import {Component
我目前正在声明相对于当前窗口位置的 templateUrl。 cvApp.directive('personalDetails', function () { return {
是否可以将范围读入指令的 templateUrl 中? 我想做这样的事情: mDirective.directive('directive', [function () { return {
这是我的指令代码: // Generated by CoffeeScript 1.6.3 (function() { var autumnDirectives; autumnDirective
我有两个指令,我试图在另一个指令的 templateUrl 中使用某个属性调用一个指令,但我无法在第二个指令中获取已编译的属性值。代码是这样的: 第一个指令 app.directive('myDir'
考虑代码(Angular 1.2 RC3): main.config(['$routeProvider', '$sce', function($routeProvider, $sce) { $
我正在本地开发一个单页应用程序。 我似乎无法使用 Angularjs 中的简单自定义指令将 HTML 文件加载为模板。如果我尝试使用 使用原始 html template: 属性,它工作得很好,但我有
我似乎无法使用 angularjs 路由加载 templateUrl。 使用 django 和 angularjs。 我所做的就是转到 127.0.0.1:8000/phones/,它会将我重定向到
该模板甚至似乎没有被请求。我尝试了绝对 URL、相对 URL,甚至全长 URL。 我已经检查过,根本没有请求该模板。 这是代码: app.directive('field', function ()
我是一名优秀的程序员,十分优秀!