Angularjs Jasmine测试因来自不同套件的错误而失败
正在体验karma jasmine测试一款有角度的应用程序时的一些特殊行为 在一个套件中有一个报告失败的测试,但消息来自另一个测试套件文件中的单独测试 测试结果:Angularjs Jasmine测试因来自不同套件的错误而失败,angularjs,unit-testing,coffeescript,jasmine,Angularjs,Unit Testing,Coffeescript,Jasmine,正在体验karma jasmine测试一款有角度的应用程序时的一些特殊行为 在一个套件中有一个报告失败的测试,但消息来自另一个测试套件文件中的单独测试 测试结果: factory: page should uncheck all items FAILED Expected undefined to contain '<div class="ui-select-container ui-select-bootstrap dropdown ng-valid" ng-class="{open
factory: page should uncheck all items FAILED
Expected undefined to contain '<div class="ui-select-container ui-select-bootstrap dropdown ng-valid" ng-class="{open: $select.open}" ng-model="test.slctbx.selected" theme="bootstrap" ng-disabled="disabled"></div>'
错误实际上来自:
describe 'directive: select', () ->
...
it 'should replace select box', () ->
replacementMarkup = '<div class="ui-select-container ui-select-bootstrap dropdown ng-valid" ng-class="{open: $select.open}" ng-model="test.slctbx.selected" theme="bootstrap" ng-disabled="disabled"></div>'
setTimeout () ->
expect($('select').length).toEqual 0
expect($('form').html()).toContain replacementMarkup
return
, 0
如果我删除factory:page套件,这种行为只会存在,而不是另一个测试套件
超时很难闻,但这只是因为该指令中有一个超时,等待任意时间,因此它要替换的元素已填充。。。这本身就有点臭
==编辑+指令代码====
link: (scope, element, attrs) ->
items = []
name = element.attr('name').replace '[]', ''
placeholder = ''
for index, el of element.find('option') when typeof el is 'object' and index isnt '0'
if index == '1'
placeholder = el.innerHTML
else if typeof el[0] == 'undefined'
items.push {value: el.getAttribute('value'), label: el.innerHTML}
if typeof scope.$parent.test != 'object'
scope.$parent.test = {}
scope.$parent.test[name] = items
select = '<ui-select ng-model="test.' + name + '.selected" theme="bootstrap" ng-disabled="disabled">
<ui-select-match placeholder="' + placeholder + '">{{ $select.selected.label }}</ui-select-match>
<ui-select-choices repeat="item in test[\'' + name + '\'] | filter: $select.search">
<div ng-bind-html="item.label | highlight: $select.search"></div>
</ui-select-choices>
</ui-select>'
newSel = $compile(select)(scope.$parent)
setTimeout () ->
element.replaceWith angular.element(newSel)
, 10
return false
为什么不在指令和测试中都使用setTimeout来代替setTimeout呢?您可以在测试中使用$timeout.flush来验证指令是否在没有等待的情况下等待了任意时间
您的指令应该使用$timeoutpunc,arbitraryamountftime而不是settimeoutpunc,arbitraryamountftime,然后测试变成:
it 'should replace select box', ->
inject ($timeout) ->
$timeout.flush()
expect($('select').length).toEqual 0
expect($('form').html()).toContain replacementMarkup
我假设在当前代码中,jasmie继续进行下一个测试,然后您的timeouted调用出现在任何地方,没有通过当前正在执行的测试。但这只是一种预感。谢谢你的回复。它试图这样做,但无济于事:我认为问题在于指令本身,我没有编写它,但改型测试是我必须做的,我已经添加到OP中的令人不快的代码-如果可以改进,请呼啦,我希望消除其中的超时…为什么不将整个select变量值作为模板,并将replace设置为符合事实的你可能会对阅读感兴趣,或者。是的——老实说,我不知道为什么会这样,但我对重构和使事情变得更好并不感到不安——这会让你大吃一惊。泰
it 'should replace select box', ->
inject ($timeout) ->
$timeout.flush()
expect($('select').length).toEqual 0
expect($('form').html()).toContain replacementMarkup