- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试对一个 html 模板进行单元测试,该模板在段落、 anchor 标记和 {{header.title || 中具有变量translate}}
但是无论我尝试过什么帖子,它似乎都不起作用。我得到了检索到的 HTML 模板,当它被编译时它仍然是一样的。例如,在模板中我仍然看到 {{user}}
。似乎它们都没有真正被编译。
当前模板输出:
<h1>{{header.title | translate}}</h1>
<h2>{{homeCtrl.name}}</h2>
预期输出:
<h1>Cake Blogger</h1>
<h2>Alexandria</h2>
测试套件:
(function() {
'use strict';
describe('home.tpl.html', function() {
var scope, controller, createController, template, element, rootScope;
beforeEach(module('Templates'));
beforeEach(module('mainApp'));
/**
@name: home.tpl.html
@description: Inject and set test related objects
@param {Service} rootScope - Used to get the language
@param {Compiler} $templateCache - Holding the compiled template
@param {Injector} $compile - Compiles an HTML string or DOM
*/
beforeEach(inject(
function(_$controller_, _$rootScope_, $templateCache, $compile) {
scope = _$rootScope_.$new();
createController = function() {
return _$controller_('homeCtrl', {
'$scope': scope
});
};
controller = createController();
rootScope = _$rootScope_;
template = $templateCache.get('home.tpl.html');
element = $compile(template)(rootScope);
// var ctrl = element.controller('homeCtrl');
rootScope.$digest();
console.log("home page", element);
}));
/**
@name: Describe Block - home.tpl.html
@description: Test cases related to home.tpl.html
*/
describe('home.tpl.html tests', function() {
fit('should have "Alexandria"', function() {
expect(element.html()).toContain("Alexandria");
});
});
});
})();
karma 文件:
files: ['list of files'],
port: 8080,
browsers: [
'PhantomJS'
],
plugins: [
'karma-phantomjs-launcher',
'karma-jasmine',
'karma-ng-html2js-preprocessor',
],
preprocessors: {
'app/**/*.tpl.html': 'html2js'
},
ngHtml2JsPreprocessor: {
'moduleName': 'Templates',
'stripPrefix': 'app/'
}
package.json
{
"name": "",
"private": true,
"devDependencies": {
"autoprefixer-core": "^5.2.1",
"grunt": "^0.4.5",
"grunt-angular-templates": "^0.5.7",
"grunt-concurrent": "^1.0.0",
"grunt-contrib-clean": "^0.6.0",
"grunt-contrib-compass": "^1.0.0",
"grunt-contrib-concat": "^0.5.0",
"grunt-contrib-connect": "^0.9.0",
"grunt-contrib-copy": "^0.7.0",
"grunt-contrib-cssmin": "^0.12.0",
"grunt-contrib-htmlmin": "^0.4.0",
"grunt-contrib-imagemin": "^1.0.0",
"grunt-contrib-jshint": "^0.11.0",
"grunt-contrib-uglify": "^0.7.0",
"grunt-contrib-watch": "^0.6.1",
"grunt-filerev": "^2.1.2",
"grunt-google-cdn": "^0.4.3",
"grunt-jscs": "^1.8.0",
"grunt-karma": "*",
"grunt-modernizr": "^1.0.2",
"grunt-newer": "^1.1.0",
"grunt-ng-annotate": "^0.9.2",
"grunt-ng-constant": "^2.0.1",
"grunt-postcss": "^0.5.5",
"grunt-svgmin": "^2.0.0",
"grunt-usemin": "^3.0.0",
"grunt-wiredep": "^2.0.0",
"jasmine-core": "^2.4.1",
"jit-grunt": "^0.9.1",
"jshint-stylish": "^1.0.0",
"karma": "^0.13.22",
"karma-coverage": "^0.5.5",
"karma-fixture": "^0.2.6",
"karma-jasmine": "*",
"karma-json-fixtures-preprocessor": "0.0.6",
"karma-json-preprocessor": "^0.3.3",
"karma-junit-reporter": "^1.2.0",
"karma-ng-html2js-preprocessor": "~0.1.0",
"karma-phantomjs-launcher": "*",
"phantomjs": "^2.1.7",
"time-grunt": "^1.0.0"
},
"engines": {
"node": ">=0.10.0"
},
"scripts": {
"test": "grunt test"
},
"dependencies": {}
}
根据我读过的帖子,例如来自帖子 plunker example 的这个 plunker ,这应该可以正常工作。我在想也许 Controller 没有绑定(bind)到范围,所以当 $digest
运行时模板可能找不到 Controller 。
有用信息:
rootscope.$digest()
我也试过它作为 scope.$digest()
,我也试过使用 $apply()
。ngHtml2JsPreprocessor
TemplateCache
$compile
查看的链接:
模板返回,但模板中的 Angular 永远不会编译。总是看到 {{homeCtrl.name}}
而不是 Alexandria
。
更新 1.1
我想也许是因为翻译 {{header.title | translate}}
不工作,也许 angular-translate
(模块:pascalprecht.translate
)实际上没有正常工作,然后导致 Controller 也失败捆绑。将继续调查。
最佳答案
在注入(inject) angular-translate
之后 | translate
过滤器正常工作并在 $compile 后产生预期的翻译值。以这个 plunker 为例:
http://plnkr.co/edit/j8rbnMI067YntllwTcGi?p=preview
导致您出现问题的一个可能原因是时间安排 - 假设您有很大的翻译数组/json 并且需要一段时间才能加载,但 Jasmine 测试已经开始并在它们完全加载和准备就绪之前完成。然后 Jasmine 将在测试期间看到未翻译的字符串。
2017-06-30 更新
经过几次实验,我可以确认任何异步 json 加载器,无论是 .useStaticFilesLoader
还是 $.getJSON()
,都不会在 jasmine 之前执行。参见 http://plnkr.co/edit/nreUd52iqOVvwLPMNtJA?p=preview例如,静态加载器可以很好地用于页面 View ,但单元测试失败。
一种可能的方法是完全放弃 .useStaticFilesLoader
。相反,我们可以使用 grunt-replace
在构建过程中注入(inject)翻译。示例 grunt 任务:
// replace string with json
replace: {
dist: {
options: {
patterns: [
{
match: /\"JSONEN\"/,
replacement: '<%= grunt.file.read("app/resources/en.json") %>'
}
]
},
files: [
{expand: true, flatten: true, src: ['app/scripts/app.js'], dest: 'public/'}
]
}
}
关于angularjs - 使用 Jasmine : Compiling a template not working to verify data output in template 进行 Angular 1.x 测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44809743/
我一直在开发数字签名 Node.js 应用程序,然后我遇到了我无法弄清楚的奇怪行为。 首先我生成了公钥/私钥对 described here 然后我实现了以下脚本 const crypto = r
缺少[路由:验证。通知] [URI:{language} / email / verify]所需的参数 使用本地化后,我将laravel电子邮件验证添加到了我的项目中。 但是现在我遇到了问题:Rout
我用对象列表调用方法 performAction 并验证相同的方法。调用此方法后,我修改了一些“对象”。 Mockito 验证失败,说参数不匹配(显示修改的对象),但我可以在 Debug模式下看到对象
我在处理opensl时遇到麻烦,提示无法在具有CA链的地方验证本地颁发的证书。除了本地CA链证书(CER,PEM,CRT)之外,我还有本地颁发的证书(PEM和CRT)。根和颁发者是同一台服务器。比较两
我正在使用 protobuf 自动生成我的 API 服务,在 gRPC 中技术,在我的安卓客户端。自从我一直在毫无问题地工作以来,已经过去了几个月。现在突然之间,AndroidRuntime Veri
我们目前正在使用“mvn clean verify”在 Hudson 上运行我们的持续构建。这就是我们一直在做的,所以我们从不质疑它。 问题是:仅使用“mvn verify”运行连续构建是否安全?因此
我已经在我的虚拟机上安装了 redis。然后我运行命令 [root@vm001jeguan ~]# rladmin help verify 它显示以下错误: [root@dltasvm001jegua
我正在使用以下应用链接:https://developer.android.com/training/app-links/verify-site-associations 使用cmd时:adb she
我写了一个应用程序,我想在网上销售它,所以我把它上传到我的网站,并试图下载并运行它作为测试,但出现了一个窗口,它说:“无法验证发布者。你确定吗?要运行此软件吗?”,并且还说:“发布者:未知发布者”。它
我们正在迁移与Kotlin接触的类,因此我们需要在Java测试类中模拟Kotlin类。我们使用io.mockk:mockk:1.9.3。 为了至少验证x次,我们有一个包装器方法 public stat
在完全更新的 XCode 4.6(以及之前的 4.5)中,构建过程的“验证应用程序”步骤在我的机器上通常需要两分钟以上的时间。在类似计算机上构建相同应用程序的同事只需几秒钟即可完成此步骤。在过去的几个
它在 documentation 中说 构建生命周期中的验证阶段 run any checks on results of integration tests to ensure quality cr
我有一个模拟对象,正在验证一些调用的东西。但想知道 MockedObject.Verify(thingToBeVerified) 的默认参数是什么? 是 Times.Zero、Times.Once 还
谁能向我解释一下每种用户类型(荣誉、已验证、审核和员工之间的区别)他们的角色? (对每个用户及其可以执行的操作的简要说明)。 我查了官方文档,但没有找到! 谢谢。 最佳答案 Honor 用户会自动注册
我正在编写一个 Chrome 扩展,其中内容脚本向后台脚本发送消息。 chrome.runtime.sendMessage({greeting: "hello"}, function(response
我在编写测试时已经习惯了遵循代码模式 public void TestMethod_Condition_Output() { //Arrange---------------- Moc
我有以下代码: class Foo { public Foo() { Size = true; } private bool _size; pr
我有以下代码,取自 ShowSignature来自 PDFBox 2 的示例。我正在运行五个测试,其中调用了它,其中 4 个测试顺利通过(无签名、单签名、双签名、过期签名),但第五个测试是椭圆曲线,但
我正在尝试mockito.verify()功能,该方法仅调用一次,但verify显示调用3次。单元测试只有一个测试,以确保该测试不会受到其他影响。Mockito.verify(utilClass, M
摆弄 Mockito 来实现我的服务的单元测试,但出于某种原因,我无法通过我厚实的头骨来完成它。我的测试通过了,但我不相信我做对了。 这是我测试 count() 方法的示例。该方法只是将调用转发到其存
我是一名优秀的程序员,十分优秀!