47

我正在使用Cypress测试我的 Web 应用程序。

此代码段当前有效,并将提交一个新内容:

describe('The Create Page', () => {
  it('successfully creates a thing', () => {
    cy.visit('/create')
    cy.get('input[name=description]').type('Hello World')
    cy.get('button#submit')
      .click()

    // After POST'ing this data via AJAX, the web app then
    // receives the id of the new thing that was just created
    // and redirects the user to /newthing/:id
    // How do I test that this redirection worked?
  })
})

正如评论所示,我不确定如何测试重定向到新路由是否有效。我可以在浏览器模拟中看到重定向有效,我只想为它添加一个测试。

谢谢!!!

4

2 回答 2

65

您需要做的是断言 url 处于预期状态。

Cypress 中有许多命令可用于对 url 进行断言。具体来说,cy.url()cy.location()可能cy.hash()满足您的要求。您的用例的最佳示例可能是:

cy.location('pathname').should('eq', '/newthing/:id')
于 2017-10-19T22:42:57.007 回答
-1

无论其他参数如何,使用下面的代码找出 url

cy.location().should((loc) => {
   expect(loc.pathname.toString()).to.contain('/home');
 });
于 2020-11-24T20:47:15.487 回答