首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用karma-jasmine测试规范中的对象属性更改

使用karma-jasmine测试规范中的对象属性更改
EN

Stack Overflow用户
提问于 2018-07-17 04:29:26
回答 0查看 1.2K关注 0票数 0

我正在尝试测试一个简单的对象,并跟踪它自己的被它自己的方法改变的属性。不知怎么的,在启动这些方法之后,属性的值并没有改变:

代码语言:javascript
复制
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:

代码语言:javascript
复制
"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"
EN

回答

页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51369666

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档