测试后反应cypress json-server恢复到原始数据库

Fre*_*sen -2 testing request reactjs json-server cypress

嘿,我正在用 cypress 测试 React 应用程序,并使用 json-server 来测试一个假 api。

当我运行一些测试时,它将更改我的 db.json 文件的数据库。

因此计划是在每次测试后将数据库恢复到原始状态。

这是我的代码:

describe('Testing fake db to be restored to original database after 
updating it in other test', () => {

 let originalDb: any = null

    it('Fetch the db.json file and then updating it to the original copy', 
       () => {

      // Save a copy of the original database
      cy.request('GET', 'localhost:8000/db').then(response => {
         originalDb = response;
         console.log(originalDb);
       })

    // Restore the original database
    cy.wrap(originalDb).then(db => {
      cy.request({
        method: 'PUT',
        url: 'http://localhost:8000/db',
        headers: {
          'Content-Type': 'application/json',
        },
        body: originalDb
      });
    });
  })
})
Run Code Online (Sandbox Code Playgroud)

该代码似乎可以用于获取数据库(洞 db.json 文件)

但无法发出 put 请求。这是来自赛普拉斯的错误:

cy.request() failed on:



http://localhost:8000/db



The response we received from your web server was:



  > 404: Not Found



This was considered a failure because the status code was not 2xx or 3xx.



If you do not want status codes to cause failures pass the option: failOnStatusCode: false



-----------------------------------------------------------



The request we sent was:



Method: PUT

URL: http://localhost:8000/db

Headers: {

  "Connection": "keep-alive",

  "Content-Type": "application/json",

  "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 Safari/537.36",

  "accept": "/",

  "accept-encoding": "gzip, deflate",

  "content-length": 29554

}

Body: {"body":{"users":[{"id":1,"first_name":"Gustav","last_name":"Vingtoft","fullname":"Gustav Vingtoft","email":"gandersen@demo.com","email_verified_at":"2023-01-25T12:13:02.000000Z","created_at":"2023-01-25T12:13:02.000000Z","updated_at":"2023-01-25T12:13:02.000000Z","api_token":"$2y$10$dtYPv.E0zua15DAvvvUQve0XdB77yo.4r.vMaiDOezKZrG7TugG9G","companyName":"Deloitte Denmark","phone":"+4530934268","userProfile":"basic","userRole":"reviewer","riskPatterns":[{"patternUuid":"04bdcd7b-d61b-45cc-b9ea-c2decc77c852"},{"patternUuid":"1b3d8a0f-2bc3-4c50-a00a-6cf99335dd20"}],"pic":"/media/users/Gustav_Vingtoft.jpg"},{"id":2,"first_name":"Frederik","last_name":"Nielsen","fullname":"Frederik Nielsen","email":"fredenielsen@demo.com","email_verified_at":"2023-01-25T12:13:02.000000Z","created_at":"2023-01-25T12:13:02.000000Z","updated_at":"2023-01-25T12:13:02.000000Z","api_token":"$2y$10$dtYPv.E0zua15LKSDFVKXdB77yo.4r.vMaiDOezKZrG7TugG9G","companyName":"Deloitte Denmark","phone":"+4511223344","userProfile":"basic","userRole":"reviewer","riskPatterns":[{"patternUuid":"04bdcd7b-d61b-45cc-b9ea-c2decc77c852"}],"pic":"/media/users/Frederik_Nielsen.jpeg"},{"id":3,"first_name":"David","last_name":"Mortensen","fullname":"David Mortensen","email":"dmortensen@demo.com","email_verified_at":"2023-01-25T12:13:02.000000Z","created_at":"2023-01-25T12:13:02.000000Z","updated_at":"2023-01-25T12:13:02.000000Z","api_token":"$2y$10$dtYPv.E0zSDGKOJSGFJLDVVSve0XdB77yo.4r.vMaiDOezKZrG7TugG9G","companyName":"Deloitte Denmark","phone":"+4511225544","userProfile":"basic","userRole":"investigator","riskPatterns":[{"patternUuid":"04bdcd7b-d61b-45cc-b9ea-c2decc77c852"}],"pic":"/media/users/300-9.jpg"},{"id":4,"first_name":"test","last_name":"test","fullname":"test user","email":"test@demo.com","email_verified_at":"2023-01-25T12:13:02.000000Z","created_at":"2023-01-25T12:13:02.000000Z","updated_at":"2023-01-25T12:13:02.000000Z","api_token":"$2y$10$dtYPv.E0zfghvcvssSve0XdB77yo.4r.vMaiDOezKZrG7TugG9G","companyName"...



-----------------------------------------------------------



The response we got was:



Status: 404 - Not Found

Headers: {

  "x-powered-by": "Express",

  "vary": "Origin, Accept-Encoding",

  "access-control-allow-credentials": "true",

  "cache-control": "no-cache",

  "pragma": "no-cache",

  "expires": "-1",

  "x-content-type-options": "nosniff",

  "content-type": "application/json; charset=utf-8",

  "content-length": "2",

  "etag": "W/"2-vyGp6PvFo4RvsFtPoIWeCReyIC8"",

  "date": "Mon, 24 Apr 2023 15:43:45 GMT",

  "connection": "keep-alive",

  "keep-alive": "timeout=5"

}

Body: {}
Run Code Online (Sandbox Code Playgroud)

小智 5

您的代码执行得太快 -localhost:8000/db当使用 发出下一个请求时, 的请求仍在进行中PUT

您需要(至少)添加 a.then()来正确排序请求。

let originalDb: any = null

// Save a copy of the original database
cy.request('GET', 'localhost:8000/db').then(response => {
  originalDb = response;
  console.log(originalDb);
})
.then(() => {
  // Restore the original database
  cy.wrap(originalDb).then(db => {
  cy.request({
    method: 'PUT',
    url: 'http://localhost:8000/db',
    headers: {
      'Content-Type': 'application/json',
    },
    body: originalDb
  });
});
Run Code Online (Sandbox Code Playgroud)