我正在尝试测试一个简单的对象,并跟踪它自己的被它自己的方法改变的属性。不知怎么的,在启动这些方法之后,属性的值并没有改变:
it('extending an object with new properties', function() {
var newCar = Object.create(car);
newCar.climatronicOn = false;
newCar.startClimatronic = function(){
newCar.climatronicOn = true;
}
newCar.stopClimatronic = function(){
newCar.climatronicOn = false;
}
spyOn(newCar, 'startClimatronic');
spyOn(newCar, 'stopClimatronic');
// properties
expect(newCar.hasOwnProperty("climatronicOn")).toBeTruthy();
expect(typeof newCar.climatronicOn).toEqual("boolean");
// methods
expect(newCar.hasOwnProperty("startClimatronic")).toBeTruthy();
expect(typeof newCar.startClimatronic).toEqual("function");
expect(newCar.hasOwnProperty("stopClimatronic")).toBeTruthy();
expect(typeof newCar.stopClimatronic).toEqual("function");
// running methods
newCar.startClimatronic(); // should change the newCar.climatronicOn to true
expect(newCar.startClimatronic).toHaveBeenCalled(); // true
expect(newCar.climatronicOn).toBeTruthy(); // false, I expected true
});
我看到过一些关于使用getters和setters而不是object文字,然后使用spyOnProperty的提示,但我不知道在尝试保持newCar.climatronicOn()和newCar.climatronicOff()的形式时如何设置和运行它。如何检查这些方法是否更新了父对象的属性?
我来自package.json的devDependencies:
"babel-polyfill": "^6.16.0",
"babel-preset-es2015": "^6.24.1",
"babel-preset-es2016": "^6.24.1",
"babel-preset-es2017": "^6.24.1",
"jasmine-core": "^2.5.2",
"karma": "^0.13.20",
"karma-babel-preprocessor": "^6.0.1",
"karma-chrome-launcher": "^0.2.2",
"karma-es6-shim": "^1.0.0",
"karma-firefox-launcher": "^0.1.7",
"karma-ie-launcher": "^0.2.0",
"karma-jasmine": "^0.3.7",
"karma-phantomjs-launcher": "^1.0.0",
"karma-spec-reporter": "0.0.26",
"phantomjs-prebuilt": "^2.1.3",
"run-sequence": "^1.2.2"https://stackoverflow.com/questions/51369666
复制相似问题