我是testcafe的新手,并且在运行SPA测试时尝试使用RequestMock模拟服务器,不幸的是,每次运行测试并检查登录页面的网络请求时,我都会得到222响应代码,因此不确定我在做什么错。

试图将模拟变量添加到带有requestHooks的测试中,但没有成功

import { Selector, RequestMock } from "testcafe";

const mock = RequestMock()
  .onRequestTo("http://web.restaurant.docker/api/login")
  .respond(
    {
      result: true,
      name: "Test Restaurant",
      data: {
        name: "Test User",
        api_token:
          "SOME_TOKEN",
        customer_id: 1
      },
      pos: { "1": "Caja", "2": "Caja Emergencias" }
    },
    200,
    {
      "access-control-allow-credentials": true,
      "access-control-allow-origin": "*"
    }
  );

fixture`Login`.page`../index.html`;

test("I can log in to the app", async t => {
  // Realiza el login
  await t
    .expect(
      Selector("form.login-form", {
        visibilityCheck: true
      }).exists
    )
    .ok()
    .typeText("#login-email", "[email protected]")
    .typeText("#login-password", "test")
    .click(".login-form button");

  // Define un nombre al pos
  await t.typeText(".dialog input", "test").click(".dialog button.ok");
  // Verifica que estoy en la ventana de seleccionar POS
  await t.expect(Selector(".restaurant-name").innerText).eql("Test Restaurant");
});



我希望看到传递给RequestMock的对象的响应为200

最佳答案

您需要按照以下说明将RequestMock附加到测试或固定装置:Attaching Hooks to Tests and Fixtures

07-27 18:25