簡體   English   中英

如何基於依賴於specs jasmine中的其他函數的變量值運行函數?

[英]How to run a function based on variable value which is dependent on other function in specs jasmine?

要求是基於依賴於函數 'getSteps' 的變量 'data' 運行 testRunner 函數

我的代碼如下

    function getSteps()
    it('test', async function () {
            testStepsArray =  await excel.getColValue('smoke.xlsx', 'Sheet1', 'A')
            return testStepsArray
        });
    }  
    
 function testRunner(count) { **//i want to run this function based on value of data variable below**
          it('test', async function () {        
                for(var j=0;j<count;j++)
                {
                    ...
                }
                            
            });
        }  
        
        
    var data =  getSteps(); 
    console.log(data.length+"LENGTH")    **//returns Cannot read property 'length' of undefined.**
        for(var i=1; i<= data.length;i+=count)   
        {
            ... 
            testRunner(i)
        }

我認為最后一個塊不是在等待 getSteps 的結果。 請建議。

更新:輸入后,我修改如下,我看到了差異。 我現在可以正確獲取數據值,但是當有一個函數環繞規范但與常規函數一起工作時執行失敗

function testRunner(count) { 
 it('test', async function () {     
    for(var j=0;j<count;j++)
    {
            ...
    }               
   });
}  
    
function test(){
    ...
}  

let promise = new Promise((resolve,reject)=>{
    it('test', async function () {
            testStepsArray =  await excel.getColValue('smoke.xlsx', 'Sheet1', 'A')
            resolve(testStepsArray)
        });
    }  
})
promise.then((data)=>{
    console.log(data.length+"LENGTH")
        for(var i=1; i<= data.length;i+=count)   
        {
            testRunner(i) //fails if function is like testrunner() - function wrraped around specs
            test() //works if function is like test() - regular function
        }
})

更新 2:Promise 拒絕錯誤日志

[32m. [0mError: 'it' should only be used in 'describe' function
    at ensureIsNotNested (\nodejs\node_modules\jasmine_3.5.0\node_modules\jasmine-core\lib\jasmine-core\jasmine.js:1786:15)
    at Env.it (\nodejs\node_modules\jasmine_3.5.0\node_modules\jasmine-core\lib\jasmine-core\jasmine.js:1933:7)
    at it (nodejs\node_modules\jasmine_3.5.0\node_modules\jasmine-core\lib\jasmine-core\jasmine.js:6576:21)
    at testRunner (webapps\mysamples\testRunner.js:66:4)
    at webapps\mysamples\testRunner.js:59:4
    at processTicksAndRejections (internal/process/task_queues.js:97:5)

諾言呢? 它等到您的函數解析“數據”

let promise = new Promise((resolve,reject)=>{
    it('test', async function () {
            testStepsArray =  await excel.getColValue('smoke.xlsx', 'Sheet1', 'A')
            resolve(testStepsArray)
        });
    }  
})
promise.then((data)=>{
    console.log(data.length+"LENGTH")
        for(var i=1; i<= data.length;i+=count)   
        {
            ... 
            testRunner(i)
        }
})

閱讀更多關於承諾

更新

被拒絕的承諾意味着您的代碼執行沒有按預期工作。

即一個網站 GET 請求。

let promise = new Promise((resolve,reject)=>{
    $.get(some_website, function(data, status, error){
        if(status==200){
            //Get request success, then return the data from website
            resolve(data);
        } else {
            //Get request fail, return error message
            reject(error);
        }
    });
})

我猜您沒有成功運行excel.getColValue('smoke.xlsx', 'Sheet1', 'A')嘗試為失敗情況添加拒絕(某事)。 然后在您的消費部分,添加以下內容:

promise.then((data)=>{
    //blah blah blah
}).catch((err)=>{
   console.log(err) //read what's up with your operation
})

或者,如果你發現你的諾言陷在for循環(沒有等到for循環完成后,請使用promise.all() 引用

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM