用jasmine比對angularjs的object時出現失敗的情況
2014-11-04 00:23:08

問題

原本的測試檔如下:

expect(model).toBe { "opt1":true, "opt2":true, "opt3":true }

結果跑測試出現了下面的錯誤:

PhantomJS 1.9.8 (Mac OS X) tau-checkbox-model contructor should set candidates with given array FAILED
Expected { opt3t1 : true, opt2 : true, opt3 : true } to be { opt1 : true, opt2 : true, opt3 : true }.

原因

AngularJS會在object中多加一些附屬屬性,導致jasmine比對時會視做不同的object。

解法

將上面的測試改成

expect(angular.equals(model, { "opt1":true, "opt2":true, "opt3":true })).toBe true

Refs