2016-09-20 89 views
1

這是我的期望:量角器 'toContain' 錯誤

expect(mandatoryFields[index].getAttribute('class')).toContain('error'); 

這是在控制檯中的錯誤:

預期['formControl NG-原始NG-不變NG-有效NG-NG-空有效最大長度錯誤']包含'錯誤'。

儘管該類包含ERROR類名稱,量角器仍然拋出錯誤。可能是什麼原因?任何幫助!

+0

http://jsfiddle.net/ZvkuP/21/ ,很困惑 –

+0

http://jsfiddle.net/ZvkuP/22/ 只是通過添加角度方括號,它開始失敗。 JuliRalp在這裏需要你的幫助 –

回答

1

而不是toContain嘗試使用toMatch。 toContain用於檢查所需值是否存在於數組中。而toMatch使用正則表達式來驗證任何值中存在的文本。

+1

但我在相同的spec文件中使用它:「** expect(coBorrowerVisibility.getAttribute('class'))。toContain('formInactive'); **」這很好。所以如果它在這種情況下工作,爲什麼它不在上述地方工作 –

0

你可以嘗試在你的beforeEach添加自定義匹配(),然後在這個小提琴同上面的代碼工作調用expect(mandatoryFields[index]).toHaveClass('error');

jasmine.addMatchers([ 
toHaveClass: function() { 
     return { 
      compare: function (element, className) { 
       return { 
        pass: element.getAttribute('class').then(function (classes) { 
         return classes.split(' ').indexOf(className) !== -1||classes.split(' ').indexOf(className+"\n") !== -1; 
        }), 
        message: "Expected elemenet to have class: "+className 
       } 
      }, 
      negativeCompare: function(element, className){ 
       return { 
        pass: element.getAttribute('class').then(function (classes) { 
         return classes.split(' ').indexOf(className) !== -1||classes.split(' ').indexOf(className+"\n") === -1; 
        }), 
        message: "Expected element not to have class: " + className 
       } 
      } 
     } 
    } 
]);