javascript - 使用 Cypress 测试重定向到新路由

标签 javascript testing mocha.js cypress

我正在使用 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?
  })
})

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

谢谢!!!

最佳答案

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

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

cy.location('pathname').should('eq', '/newthing/:id')

关于javascript - 使用 Cypress 测试重定向到新路由,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46839520/

相关文章:

testing - 如何对极其复杂的系统进行功能测试?

javascript - stub 请求在多个测试函数中共享

javascript - 如何 stub 对象的属性而不是方法?

Javascript window.location.href 不重新加载数据

javascript - Google 标签管理器 - 通过带有过滤器的自定义事件触发

javascript - 点击重叠的 div

node.js - 如何告诉 mocha 在哪里递归地找到我的测试?

javascript - 通过和弦确定歌曲的调

c - 如何模拟内存分配失败?

internet-explorer - 你用什么来测试你的浏览器扩展/BHO?