当我在 Mocha 测试中将函数编写为 lambda 时的上下文 eval 表达式

Context eval expression when I write function in Mocha Test as lambda

当我使用下面的代码时,我没有得到变量数据的上下文评估错误

 describe('Hooks',()=>{
    before(()=>{
        cy.fixture("example.json").then(function(data){
            this.data=data
        })
    })

    it("my First Test case", function(){
        cy.visit("https://rahulshettyacademy.com/angularpractice/")
        
        cy.get(":nth-child(1) > .form-control").type(this.data.name)
       
        cy.get("#exampleFormControlSelect1").select(this.data.gender)
    })
})

当我将我的 mocha 测试函数写成 lambda(参考下面的代码)时,我收到数据变量的上下文评估错误 代码

describe('Hooks',()=>{
    before(()=>{
        cy.fixture("example.json").then((data)=>{
            this.data=data
        })
    })

    it("my First Test case", () =>{
        cy.visit("https://rahulshettyacademy.com/angularpractice/")
        
        cy.get(":nth-child(1) > .form-control").type(this.data.name)
       
        cy.get("#exampleFormControlSelect1").select(this.data.gender)
    })
})

错误

at Context.eval (webpack:///cypress/integration/examples/Test8Framework.js:4:13) From previous event: at Context.thenFn (https://rahulshettyacademy.com/__cypress/runner/cypress_runner.js:155190:24) at Context.then (https://rahulshettyacademy.com/__cypress/runner/cypress_runner.js:155629:22) at Context. (https://rahulshettyacademy.com/__cypress/runner/cypress_runner.js:170229:22) at (https://rahulshettyacademy.com/__cypress/runner/cypress_runner.js:169653:16) From previous event: at runCommand (https://rahulshettyacademy.com/__cypress/runner/cypress_runner.js:169632:9) at next (https://rahulshettyacademy.com/__cypress/runner/cypress_runner.js:169778:15) at (https://rahulshettyacademy.com/__cypress/runner/cypress_runner.js:169806:17) From previous event: at next (https://rahulshettyacademy.com/__cypress/runner/cypress_runner.js:169778:35) From previous event: at Promise.catch.err.name (https://rahulshettyacademy.com/__cypress/runner/cypress_runner.js:169819:38) From previous event: at run (https://rahulshettyacademy.com/__cypress/runner/cypress_runner.js:169812:22) at $Cy.cy. [as fixture] (https://rahulshettyacademy.com/__cypress/runner/cypress_runner.js:170269:12) at Context.runnable.fn (https://rahulshettyacademy.com/__cypress/runner/cypress_runner.js:170496:22) at callFn (https://rahulshettyacademy.com/__cypress/runner/cypress_runner.js:104252:22) at Hook.../driver/node_modules/mocha/lib/runnable.js.Runnable.run (https://rahulshettyacademy.com/__cypress/runner/cypress_runner.js:104239:8) at (https://rahulshettyacademy.com/__cypress/runner/cypress_runner.js:176219:29) From previous event: at Object.onRunnableRun (https://rahulshettyacademy.com/__cypress/runner/cypress_runner.js:176207:18) at $Cypress.action (https://rahulshettyacademy.com/__cypress/runner/cypress_runner.js:166637:29) at Hook.Runnable.run (https://rahulshettyacademy.com/__cypress/runner/cypress_runner.js:174384:14) at next (https://rahulshettyacademy.com/__cypress/runner/cypress_runner.js:104754:11) at (https://rahulshettyacademy.com/__cypress/runner/cypress_runner.js:104798:6) at timeslice (https://rahulshettyacademy.com/__cypress/runner/cypress_runner.js:98724:28)

If you store and access the fixture data using this test context object, make sure to use function () { ... } callbacks. Otherwise the test engine will NOT have this pointing at the test context.

这个在cypress docs中有提到。因此,在您使用 it("my First Test case", () =>{ 的测试中,它失败了,而当您使用 it("my First Test case", function(){ 时,它通过了。