2017-09-24 17 views
1

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** 

更新的代码财产 'user_mainValue'

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”:真 “值”:[{ “carsId”: “100”, “carsName”: “uuiiuui”, “carsType”: “标题”, “carsData”:“YWZjYXJlZ2V yamh2dmFyZWdoYnZi”, “注意事项”: “”, “notesId”: “100”, “鱼”: “100”, “URL”: “ioioioiooi”, “日期”: “IOIOI”, “addedByName”: “哈拉”, “用户ID”: “6765676565”, “operationType”: “创建”},{ “carsId”: “101”, “carsName”: “为例.pdf”, “carsType”: “标题”, “carsData”:“约翰“ ”注意事项“: ”“, ”notesId“: ”101“, ”鱼“: ”101“, ”URL“: ”ioioioiooi“, ”日期“: ”IOIOI“, ”addedByName“: ”哈拉“,”用户ID “:” 6765676565" , “operationType”: “创建”},{ “carsId”: “101”, “carsName”: “example.ppt”, “carsType”: “标题”, “carsData”: “约翰” “注意事项”: “”, “notesId”: “101”, “鱼”: “101”, “URL”: “ioioioiooi”, “日期”: “IOIOI”, “addedByName”: “哈拉”,“用户ID “:”6765676565“,”operationType“:”create“}],”scheduler“:null}' LOG:'_dataSource._data ---> undefined' Chrome 61.0.3163(Windows 10 0.0.0)Jasmine Unit测试:添加失败 [0]类型错误:无法读取未定义的属性“长度” Chrome 61.0.3163(Windows 10 0.0.0):执行69(1失败) 68)错误(0.56秒/ 0.422秒)

更新

嘿,我没有进一步研究的问题是此行让车= $( '#laptopMousesGrid')。数据( 'kendoGrid') ;

当我尝试打印汽车时,我越来越不确定是否有可能嘲笑汽车或spyon的价值,以便我们可以设置一些价值并通过测试用例...我做了一些研究,但不知道如何继续

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

let mainValue=this.spaceService.getSeesionStorageValue(); 

我能看到价值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" 

mainValue结果我无法看到。

我得到一个未定义的错误在我的测试案例日志:

'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); 
    }); 
}); 
+0

请问您可以添加'console.log(“正在播放mainValue ---->”+ mainValue);'结果的问题? –

+0

@MohammadJavadSeyyedi jhey从浏览器中更新了mainValue,但是我在测试用例中得到了未定义...当我在终端中运行它时 –

+0

您说你给我们展示的是'this.spaceService.getSeesionStorageValue()'的结果?那么这个结果如何不确定,说你在浏览器中看到这是'this.spaceService.getSeesionStorageValue()'的结果让我们感到困惑。问题必须来自该方法。你还可以将代码添加到问题中吗?这是打算还是只是一个遗漏所有的错字? 'getSeesionStorageValue'? Seesion而不是Session? –

回答

0

林假设this.spaceService.getSeesionStorageValue();实际上是从BR读点owsers session Storage,这是测试时尚未添加到测试浏览器sessionStorage中的东西。无论哪种方式,您都应该将该服务嘲笑为您的单元测试的外部依赖。茉莉间谍会工作。

注入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'

+0

感谢您的回复......你能告诉我,为什么我们使用beforeEach因此,在今后的其他问题,我可以自己修复... –

+0

所以服务是每次测试前实例和任何国家,他们已经之前被重新初始化 – Bowofola

+0

和最新注入的优势...是不可能的实现不注入? –

0

你看到错字?你写了两个e而不是两个s

let mainValue = this.spaceService.getSeesionStorageValue()

我知道这是愚蠢的,但如果没有得到一个错误,mainValue是竟被undefined

+0

如果功能不存在,他将得到“getSeesionStorageVaue是不是一个函数”什么 – Bowofola

+0

哎如果我更新我的代码库下面的代码,我收到以下错误,你可以告诉我如何解决它..提供我的代码和下面的错误------> 让spaceService; beforeEach(注入(功能(_spaceService_, ''){ spaceService = _spaceService_ )); –

+0

参数类型的“(_spaceService_:任意)=>任何”是不能分配给 –

0

它看起来不像_dataSource._data是从_dataSource的输出定义的。希望有所帮助!

+0

嘿感谢您的回复...你可以在我的代码更新......我不知道如何解决它 –

相关问题