开发者问题收集

类型错误:无法读取未定义的属性“user_mainValue”

2017-09-24
1206

updated2

hey, 



 I moved the grid value in a new mthod carSource so the previous error I removed but right now I am getting another error,
    - is there any way I can spy on _dataSource or mock the value to pass the test cases.

     can you tell me how to fix it

**TypeError: Cannot read property 'length' of undefined**

更新代码

    public carSource():any{
      let car = $('#laptopMousesGrid').data('kendoGrid');
      return  grid.dataSource;
    }

  playing(evt, dropValue, values) {
      let todayDate = this.todayDate();
      //et car = $('#laptopMousesGrid').data('kendoGrid');
      //let _dataSource = car.dataSource;

          let _dataSource =this.carSource();

      let pageSize = _dataSource.pageSize();
      if(_dataSource) {

         console.log("playing--->");
      console.log("_dataSource--->" + JSON.stringify(_dataSource));
      console.log("_dataSource._data--->" + _dataSource._data);

        let carLength = _dataSource._data.length;
      }
      let filename;
      let mainValue = this.spaceService.getSeesionStorageValue();
      console.log("playing mainValue----> " + mainValue);
      if (mainValue) {
        this.userStem = mainValue.user_mainValue.SSO[0];
        this.loggedUserName = mainValue.user_mainValue.DisplayName[0];
      }
  }

输出 LOG: '正在播放--->' LOG: '_dataSource-->{"_isScalar":true,"value":[{"carsId":"100","carsName":"uuiiuui","carsType":"TITLE","carsData":"YWZjYXJlZ2Vyamh2dmFyZWdoYnZi","notes":"","notesId":"100","fish":"100","url":"ioioioiooi","date":"ioioi","addedByName":"Kamal","userID":"6765676565","operationType":"create"},{"carsId":"101","carsName":"example.pdf","carsType":"TITLE","carsData":"john","notes":"" ,"notesId":"101","fish":"101","url":"ioioioiooi","date":"ioioi","addedByName":"Kamal","userID":"6765676565","operationType":"create"},{"carsId":"101","carsName":"example.ppt","carsType":"TITLE","carsData":"john","notes":"","notesId":"101","fish":"101","url":"ioioioiooi","date":"ioioi","addedByName":"Kamal","userID":"6765676565","operationType":"create"}],"scheduler":null}' LOG: '_dataSource._data--->undefined' Chrome 61.0.3163 (Windows 10 0.0.0) Jasmine 单元测试:添加失败 [0] TypeError:无法读取未定义的属性“length” Chrome 61.0.3163 (Windows 10 0.0.0):执行了 69 个中的 1 个(1 个失败)(跳过 68 个)错误(0.56 秒 / 0.422 秒)

更新

嘿,我做了进一步的研究,问题出在这行 let car = $('#laptopMousesGrid').data('kendoGrid');

当我尝试打印 car 时,我得到了未定义的值,是否可以模拟 car 或 spyon 的值,以便我们可以设置一些值并通过测试用例......我做了一些研究,但不确定如何继续

我的应用程序使用下面的代码行运行良好

let mainValue=this.spaceService.getSeesionStorageValue();

我能够在浏览器中看到 mainValue 的值,就像在我的测试用例中一样。提供以下 mainValue 结果

acr:{values: Array(1)}
amr:["ext"]
cid:"7823459-78237832387278-237823873278"
client_id:"7823459-78237832387278-237823873278"
email:"parrot.tiger.com"
scope:["openid"]
sub:"3623673267-278782378-23872872378-2378327832"
user_mainValue:{BusinessSegment:["Operations & Technical Services"], DisplayName:["pen"], Title: ["Developer"], SSO:["bndsbsbnsd"]}
zid:"6fc3c2b8-f6b4-4376-824f-9c93dfcc19a3"

我看不到。

我的测试用例 LOG 中出现未定义错误:

'playing mainValue----> undefined'.

我需要将 spyon 添加到这些值吗?

  • 我也对 spyon 感到困惑。

你能告诉我如何修复它吗?

[0] 23 09 2017 22:28:36.720:DEBUG [web-server]: serving (cached): C://app/components/laptops/jumping-l    LOG: '**playing mainValue----> undefined'**
[0] Chrome 60.0.3112 (Windows 10 0.0.0): Executed 17 of 34 (1 FAILED) (0 secs / 3.434 secs)
[0] 23 09 2017 22:28:36.824:DEBUG [middleware:source-files]: Requesting /app/components/laptops/swimming.html /
[0] 23 09 2017 22:28:36.824:DEBUG [middleware:source-files]: Fetching /app/components/laptops/swimming.html
[0] 23 09 2017 22:28:36.832:DEBUG [proxy]: proxying request - /app/components/laptops/swimming.html to localhost:9876
[0] 23 09 2017 22:28:36.832:DEBUG [middleware:source-files]: Requesting /base/app/components/shared/progress/progress-circle.html /
[0] 23 09 2017 22:28:36.832:DEBUG [middleware:source-files]: Fetching C://app/components/shared/progress/progress-circle.html
[0] 23 09 2017 22:28:36.832:DEBUG [middleware:source-files]: Requesting /app/components/laptops/jumping-left-slider.html /
[0] 23 09 2017 22:28:36.832:DEBUG [middleware:source-files]: Fetching /app/components/laptops/jumping-left-slider.html
[0] 23 09 2017 22:28:36.836:DEBUG [web-server]: serving (cached): C://app/components/shared/progress/progress-circle.html
    Chrome 60.0.3112 (Windows 10 0.0.0) Jasmine Unit Tests: parrot-Manage-Mouses Add Hiding Mouse FAILED
**[0]     TypeError: Cannot read property 'user_mainValue' of undefined**
[0] Chrome 60.0.3112 (Windows 10 0.0.0): Executed 18 of 34 (2 FAILED) (0 secs / 3.562 secs)

实际代码

playing(evt, dropValue, values) {
  let todayDate = this.todayDate();
  let car = $('#laptopMousesGrid').data('kendoGrid');
  let _dataSource = car.dataSource;
  let pageSize = _dataSource.pageSize();
  let carLength = _dataSource._data.length;
  let filename;
  let mainValue = this.spaceService.getSeesionStorageValue();
  console.log("playing mainValue----> " + mainValue);
  if (mainValue) {
    this.userStem = mainValue.user_mainValue.SSO[0];
    this.loggedUserName = mainValue.user_mainValue.DisplayName[0];
  }

测试用例

<!-- language-all: lang-or-tag-here -->

it('Add Hiding Mouse', (done) => {
  let callFirstTime: boolean = true;
  let url = spyOn(rainbow.HidingMouses.spaceService, 'getResponse').and.
  callFake(() => {
    if (callFirstTime) {
      callFirstTime = false; // Invoked by detectChanges() 
      return Observable.of([{
        "starsStem": "100",
        "starsName": "/assets/js/actualairings.json",
        "starsType": "TITLE",
        "starsData": "YWZjYXJlZ2Vyamh2dmFyZWdoYnZi",
        "jersey": "",
        "jerseyStem": "100",
        "DocRing": "100",
        "url": "/upload",
        "date": "06/27/2017",
        "addedByName": "Kamal",
        "userRing": "",
        "operationType": "create"
      }]);
    }
  });
  spyOn(rainbow.HidingMouses.carkendo, 'enableSaveMouse').and.returnValue(
    null);
  rainbow.HidingMouses.fileSelect = "text.txt";
  let values = {
    "url": "laptops/v1/stars/current",
    "columns": [],
    "title": "sky",
    "excelFileName": "ViewAiringsExport",
    "mainStem": current
  };

  rainbow.HidingMouses.playing('', '', values);
  fixture.whenStable().then(() => {
    done();
    expect(rainbow.HidingMouses._dataSource._data.length).toEqual(3);
  });
});
3个回答

我假设 this.spaceService.getSeesionStorageValue(); 实际上是从浏览器会话存储中读取一些内容,而这些内容在测试时尚未添加到测试浏览器的 sessionStorage 中。无论哪种方式,您都应该模拟该服务,因为它是单元测试的外部依赖项。Jasmine 间谍会起作用。

像这样将 spaceService 注入到您的测试中

var spaceService;
beforeEach(inject(function(_spaceService_){
spaceService = _spaceService_
));

现在模拟服务上的方法

spyOn(spaceService, 'getSeesionStorageValue');
spaceService.getSeesionStorageValue.andReturn('here is the main value');

现在您应该得到一条日志,上面写着 'playing mainValue----> here is the main value'

Bowofola
2017-10-04

您看到了拼写错误吗?您在 session 中写有两个 e ,而不是两个 s

let mainValue = this.spaceService.getSeesionStorageValue()

我知道这很蠢,但如果出现错误, mainValue 将是 undefined

Carlos Augusto Grispan
2017-10-05

从 _dataSource 的输出来看,_dataSource._data 似乎没有定义。希望对您有所帮助!

Alex Bott
2017-10-07