如何在 cy.intercept() 之前强制 cy.writeFile()

问题描述 投票:0回答:1

我想在每次运行测试之前将固定装置中的时间更新为当前时间,并在 cy.intercept() 中将更新的时间作为响应提供给固定装置文件。但是每次 cy.intercept() 都会从原始文件加载数据(不包含更新时间),然后使用更新时间写入夹具文件。如何确保夹具文件先更新然后拦截调用?

describe('NBA 赛前 EventTile 验证', () => {

beforeEach('强制游戏开始时间', function () {

// Function to read and update schedule file, returning a promise
const updateSchedule = (filePath, updateCallback) => {
  return cy.fixture(filePath).then((nbaData) => {
    updateCallback(nbaData);
    return cy.writeFile(filePath, nbaData);
  });
};

// Update QB schedule and set up interception
const updateQBAndIntercept = () => {
  return updateSchedule('Odds/QB/NBA1Schedule.json', (nbaData) => {
    const nbaGame = nbaData.NBA[0];
    const currentTimestamp = Date.now();
    const adjustedStartTime = currentTimestamp + (2 * 60 * 60 * 1000); // 2 hours ahead
    nbaGame.startTime = adjustedStartTime;
  }).then(() => {
    return cy.intercept('GET', '**/quarterback/lighterGames/leagues/**', {
      fixture: 'Odds/QB/NBA1Schedule.json'
    }).as('nbaPregameqb');
  });
};

// Update DK schedule and set up interception
const updateDKAndIntercept = () => {
  return updateSchedule('Odds/DK/NBA1Schedule.json', (nbaData) => {
    const nbaGame = nbaData.NBA[0];
    const currentDate = new Date();
    const adjustedStartDate = new Date(currentDate.getTime() + (2 * 60 * 60 * 1000)); // 2 hours ahead
    nbaGame.startDate = adjustedStartDate.toISOString();
  }).then(() => {
    return cy.intercept('GET', '**/sports-oddsapp-offers.xreapps.net/dk/events/**', {
      fixture: 'Odds/DK/NBA1Schedule.json'
    }).as('nbaPregamedk');
  });
};

cy.intercept('GET', '**/quarterback/lighterGame/league/**', {
  fixture: 'Odds/QB/NBA1Details.json'
}).as('nbaPregameDetailqb');

cy.intercept('GET', '**/sports-oddsapp-offers.xreapps.net/dk/props,lines/**', {
  fixture: 'Odds/DK/NBA1Details.json'
}).as('nbaPregameDetaildk');

// Launch the Odds App and wait for intercepts to be set up
const launchAppAndWaitForIntercepts = () => {
  cy.launchOddsApp();
  return cy.wait(['@nbaPregameqb', '@nbaPregamedk']);
};

// Chain the promises to ensure sequential execution
updateQBAndIntercept()
  .then(updateDKAndIntercept)
  .then(launchAppAndWaitForIntercepts);

})

context('赔率浏览器页面', () => {

it.only('Should have NBA pregame Tile', () => {

我想在每次运行测试之前将固定装置中的时间更新为当前时间,并在 cy.intercept() 中将更新的时间作为响应提供给固定装置文件。但每次 cy.intercept() 都会从原始文件加载数据(不包含更新的时间),然后使用更新的 timecy.writeFile 写入固定文件。如何确保夹具文件先更新然后拦截调用?

cypress stubbing cypress-intercept enforcement cy.intercept
1个回答
0
投票

这很可能是使用

cy.fixture()
缓存的错误。

请参阅如何...在 testdata.json 中获取新更新的值

请记住,假设夹具文件在测试期间保持不变,因此 Cypress 仅加载它们一次。即使您覆盖夹具文件本身,已加载的夹具数据仍保持不变。

如果您希望在测试期间动态更改文件的内容,请考虑使用

cy.readFile()

测试不太简洁 - 您必须扩展截距以使用

routeHandler
函数,并提供扩展的
filePath

使用routeHandler函数

扩展文件路径功能

const pathTofixture = (filePath)  => `cypress/fixtures/${filePath}`;

用readFile()回复的函数

const interceptWithFile = ({url, file, alias}) => {

  return cy.intercept(url, (req) => {
    cy.readFile(pathToFixture(file)).then(data => req.reply(data)) 
  }).as(alias)
}
// Function to read and update schedule file, returning a promise
const updateSchedule = (filePath, updateCallback) => {
  return cy.readFile(pathToFixture(filePath)).then((nbaData) => {
    updateCallback(nbaData);
    return cy.writeFile(pathToFixture(filePath), nbaData);
  });
}

// Update QB schedule and set up interception
const updateQBAndIntercept = () => {
  return updateSchedule('Odds/QB/NBA1Schedule.json', (nbaData) => {
    ...
  }).then(() => {
    interceptWithFile({
      url:   '**/quarterback/lighterGames/leagues/**', 
      file:  'Odds/QB/NBA1Schedule.json',
      alias: 'nbaPregameqb'
    })
  });
}

// Update DK schedule and set up interception
const updateDKAndIntercept = () => {
  return updateSchedule('Odds/DK/NBA1Schedule.json', (nbaData) => {
    ...
  }).then(() => {
    interceptWithFile({
      url:   '**/sports-oddsapp-offers.xreapps.net/dk/events/**', 
      file:  'Odds/DK/NBA1Schedule.json',
      alias: 'nbaPregamedk'
    })
  });
};

interceptWithFile({
  url:   '**/quarterback/lighterGame/league/**', 
  file:  'Odds/QB/NBA1Details.json',
  alias: 'nbaPregameDetailqb'
})

interceptWithFile({
  url:   '**/sports-oddsapp-offers.xreapps.net/dk/props,lines/**', 
  file:  'Odds/DK/NBA1Details.json',
  alias: 'nbaPregameDetaildk'
})

...
© www.soinside.com 2019 - 2024. All rights reserved.