- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我的单元测试不断失败,并显示以下错误消息:
LOG: 'f40e0e47-6457-463b-a5f9-9dc97bd2d0ce'
LOG: [Object{phrase_id: 'f40e0e47-6457-463b-a5f9-9dc97bd2d0ce', phrase: 'Training {{group}} to develop their {{attribute}} by ensuring they are comfortable with {{factor}}'}]
PhantomJS 2.1.1 (Windows 8 0.0.0) PhraseDetailCtrl #initialisation should initialise the controller's scope with details on a single phrase FAILED
TypeError: undefined is not an object (evaluating 'value.phrase.replace') in D:/myapp-mobile/www/js/myapp.controllers.js (line 87)
D:/myapp-mobile/www/js/myapp.controllers.js:87:34
forEach@[native code]
D:/myapp-mobile/www/js/myapp.controllers.js:85:35
then@unit-tests/phrase-detail.controller.tests.js:32:26
PhraseDetailCtrl@D:/myapp-mobile/www/js/myapp.controllers.js:78:55
PhraseDetailCtrl@[native code]
instantiate@D:/myapp-mobile/www/lib/ionic/js/ionic.bundle.js:18010:61
$controller@D:/myapp-mobile/www/lib/ionic/js/ionic.bundle.js:23412:39
D:/myapp-mobile/www/lib/angular-mocks/angular-mocks.js:2221:21
unit-tests/phrase-detail.controller.tests.js:44:31
loaded@http://localhost:9876/context.js:151:17
PhantomJS 2.1.1 (Windows 8 0.0.0): Executed 3 of 3 (1 FAILED) (0.01 secs / 0.084 secs)
这是我正在尝试测试的 Controller :
function PhraseDetailCtrl($scope, $stateParams, PhraseService, CompetencyService, PopupService, ModalService, FilterService, $ionicLoading, $ionicActionSheet, $timeout) {
// $ionicLoading.show({
// content: 'Loading',
// animation: 'fade-in',
// showBackdrop: true,
// maxWidth: 200,
// showDelay: 0
// });
// $timeout(function () {
// $ionicLoading.hide();
$scope.dataLoaded = true;
$scope.phraseId = $stateParams.phraseId;
console.log($scope.phraseId);
PhraseService.getPhraseDetail($scope.phraseId).then(function(dataResponse) {
$scope.phraseDetail = dataResponse.data;
console.log($scope.phraseDetail);
var replacers = FilterService.getItemFilter();
Object.keys(replacers).forEach(function(key){
var value = $scope.phraseDetail;
value.phrase = value.phrase.replace(new RegExp(key, 'g'), replacers[key]);
})
})
PhraseService.getPhraseCompetency($scope.phraseId).then(function(dataResponse) {
$scope.phraseCompetencyList = dataResponse.data;
})
PhraseService.getPhraseExample($scope.phraseId).then(function(dataResponse) {
var data = dataResponse.data;
var phrase = data[0].phrase;
// console.log("phrase: " + phrase);
var tokens = data.map(function(tmp) {
return tmp.example;
});
// console.log("tokens: " + tokens);
var re = /{{[^}]*}}/gi;
var placeholders = phrase.match(re);
// console.log("placeholders: " + placeholders);
if (tokens.length != placeholders.length) {
throw new Error("Mismatch number of tokens and placeholders");
}
for (var i = 0; i < tokens.length; ++i) {
var token = tokens[i];
var placeholder = placeholders[i];
phrase = phrase.replace(placeholder, "<button class=\"button button-small button-outline button-dark button-side-padding\">" + token + "</button>");
}
// console.log(phrase);
$scope.phraseExample = phrase;
})
CompetencyService.getCompetencyList().then(function(dataResponse) {
$scope.competencyList = dataResponse.data;
})
// }, 1000);
// Action sheet
$scope.showOptions = function() {
$ionicActionSheet.show({
titleText: 'Options',
buttons: [
{ text: '<i class="icon ion-heart"></i>Add to Favourites' },
{ text: '<i class="icon ion-chatbubble"></i> Suggest Improvement' }
],
cancelText: 'Cancel',
buttonClicked: function(index) {
if(index === 0) {
$scope.addtoFavouritesPopup();
} else if(index === 1) {
$scope.suggestimprovementModal();
}
//console.log('BUTTON CLICKED', index);
return true;
},
cancel: function() {
//console.log('CANCELLED');
},
});
};
// Popups
$scope.phraseExamplePopup = function() {
var myPopup = PopupService.phraseExamplePopup($scope);
};
$scope.addtoFavouritesPopup = function() {
var myPopup = PopupService.addtoFavouritesPopup($scope);
};
// Modals
$scope.phraseCompetencyModal = function() {
var vm = $scope;
ModalService.show('templates/modals/phrase-competency-modal.html', 'PhraseDetailCtrl as vm');
}
$scope.suggestimprovementModal = function() {
var vm = $scope;
ModalService.show('templates/modals/suggest-improvement-modal.html', 'PhraseDetailCtrl as vm');
}
}
我认为错误消息正在此时发生,但我不确定如何在单元测试中模拟它:
Object.keys(replacers).forEach(function(key){
var value = $scope.phraseDetail;
value.phrase = value.phrase.replace(new RegExp(key, 'g'), replacers[key]);
})
这是我的单元测试:
describe('PhraseDetailCtrl', function () {
var $controller, $stateParams, $ionicModal, $ionicPopup, $PhraseService, $CompetencyService, $PopupService, $ModalService, $FilterService
$stateParams = {
phraseId: "f40e0e47-6457-463b-a5f9-9dc97bd2d0ce"
};
var response = {
status: 200,
data: [{
"phrase_id": "f40e0e47-6457-463b-a5f9-9dc97bd2d0ce",
"phrase": "Training {{group}} to develop their {{attribute}} by ensuring they are comfortable with {{factor}}"
}]
};
beforeEach(module('ionic'));
beforeEach(module('dingocv.services'));
beforeEach(module('dingocv.controllers'));
beforeEach(inject(function (_$controller_, _PhraseService_, _CompetencyService_, _ModalService_, _PopupService_, _FilterService_) {
$controller = _$controller_;
$PhraseService = _PhraseService_;
$CompetencyService = _CompetencyService_;
$ModalService = _ModalService_;
$PopupService = _PopupService_;
$FilterService = _FilterService_;
spyOn(_PhraseService_, 'getPhraseDetail').and.callFake(function(){
return{
then: function(successCallback){
successCallback(response);
}
}
});
}));
describe('#initialisation', function() {
var $scope, controller;
beforeEach(function () {
$scope = {};
controller = $controller('PhraseDetailCtrl', {
$scope: $scope,
$stateParams: $stateParams,
$ionicModal: $ionicModal,
$ionicPopup: $ionicPopup,
PhraseService: $PhraseService,
CompetencyService: $CompetencyService,
PopupService: $PopupService,
ModalService: $ModalService,
FilterService: $FilterService
});
});
it('should initialise the controller\'s scope with details on a single phrase', function(){
expect($PhraseService.getPhraseDetail).toHaveBeenCalled();
expect($scope.phraseDetail).toBeDefined();
});
});
});
最佳答案
在尝试使用该对象之前,您应该检查是否为空/未定义,例如
if(replacers!=undefined){
Object.keys(replacers).forEach(function(key){
var value = $scope.phraseDetail;
if(value !=undefined && value.phrase !=undefined){
value.phrase = value.phrase.replace(new RegExp(key, 'g'), replacers[key]);
}
})
}
关于javascript - Undefined 不是一个对象(评估 'value.phrase.replace' ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39866310/
目前,由于生成变量的评估,我的Makefile遇到了问题。我降低了复杂性,仅保留了导致问题的基本要素。 读取Makefile时, $(LIST)被评估为文件列表。 在步骤1中,其中一个文件被删除。 在
为什么这 eval 没有调用alert("Summer") ? eval('(caption="Summer";alert(caption))'); 和《夏天》里的台词有关系吗? 最佳答案 Uncau
我正在努力让以下工作正常进行。最初似乎可以工作,但不知何故它停止工作了 var setCommonAttr = "1_row1_common"; var val = document.getEleme
eval('({"suc":true})') 以上错误,应该是: eval('{"suc":true}') 为什么? 最佳答案 当尝试评估时,解释器会看到大括号并认为它是一个 block 开头。将其括
我的页面 A 发出了 ajax 调用并引入了片段 B。该片段被添加到 DOM 中,并且该片段中的所有脚本都经过了评估。在该代码片段中,我有 2 个脚本标签: function doOptions()
这里是javascript代码: var test = { "h" : function (a) {return a;}, "say" : "hello" }; 第一次运行: test
我正在查看一些工作代码,并遇到了这一行: eval("\$element = \"$element\";"); 我真的很困惑为什么任何 PHP 开发人员都会写这一行。除了给自己设置一个变量之外,这还有
谁能帮我解决以下问题: 我有这样的代码: if(cond1 && cond2 && .. && cond10) 这里,cond1 是昂贵的操作,其输出是 boolean 值。 现在我的问题是,当 co
**摘要:**华为AppCube应用魔方顺利通过信通院评估,被认证为具备 “低代码开发平台通用能力”的企业服务平台。 本文分享自华为云社区《华为AppCube通过中国信通院“低代码开发平台通用能力要求
我正在尝试通过 PHP 从图像的 EXIF 数据中获取焦距。 这是我目前得到的代码: $exif = exif_read_data("$photo"); $length10 = $exif['Foca
我想使用id =“key”将一个类添加到元素中,但是为什么不起作用?我是js的初学者:这是代码: audio.classList.add('yellow'); 这是错误: null is not an
这是我的 XML: QueWay Password Recovery 现在我想用 php 用 xpath 选择文本“QueWay”。到目前为止我所拥有的一切都很好: $xml =
使用下面的代码,即使我输入的数字大于 18,我也会得到这个结果。 运行:你今年多大? 21你还没有达到成年年龄!构建成功(总时间:3 秒) 我是java新手,正在尝试自学,有人可以帮忙吗? impor
我正在阅读 http://www.cran.r-project.org/doc/manuals/R-lang.pdf手册第 4.3 章,我就是不明白。也许有人可以给我一个快速的解释,为什么 R 的行为
在这个实现中,每次都会评估 hand 并返回另一个列表吗? foreach (Card card in hand.Cards) { } 我们应该用下面的实现替换上面的实现吗? var cards =
我正在制作 LINQ lambda 表达式: Expression> add = (x, y) => x + y; 但现在我将如何评估它,比如说找到 2+3? 最佳答案 这应该适合你: var su
我正在制作一个语言解释器,我已经到了需要评估 if 语句的地步。起初我认为这很简单,我能够让我的解释器评估简单的 if 条件,10 == 10 但是当我试图让它评估更复杂的条件时, 10 == 10
我正在尝试以下代码,该代码向 RDD 中的每一行添加一个数字,并使用 PySpark 返回 RDD 列表。 from pyspark.context import SparkContext file
在阅读了很多关于 Lisp eval-when 运算符的文档后,我仍然无法理解它的用途,我知道使用这个运算符我可以控制表达式的计算时间,但我做不到找出任何可能适用的示例? 最好的问候,utxee. 最
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 要求我们推荐或查找工具、库或最喜欢的场外资源的问题对于 Stack Overflow 来说是偏离主题的,
我是一名优秀的程序员,十分优秀!