【问题标题】:Capybara - Not catching Angular2 URLCapybara - 没有捕捉到 Angular2 URL
【发布时间】:2017-05-19 10:37:18
【问题描述】:

我有一个用 rspec 编写的测试,在单击链接后检查路径:

click_link("Dew, Muntain A")
expect(current_path).to eq(demographic_information_patient_path(1))

现在,我明白了:

current_path = "/"
demographic_information_patient_path(1) = "/patients/1/demographics"

因此,测试失败。我可以看到,点击链接后,下一页确实是人口统计页面,网址为“/patients/1/demographics”,但不知何故水豚找不到。

注意:当我手动尝试测试并在 rails 控制器中打印 request.url 时,我得到 "/patients/1/demographics"

我能解决的一个原因是,被点击的页面是一个 Angular2 页面,是不是因为水豚找不到正确的 URL?

我对水豚的尝试:

URI.parse(current_url).request_uri #gives "/"
page.current_url #gives "/"
u = page.execute_script("window.location.url"); #gives nil as expected

【问题讨论】:

    标签: ruby-on-rails rspec capybara


    【解决方案1】:

    Capybara 的click_xxx 方法只需单击提到的项目。他们不会等待点击导致的操作发生,因为他们不知道该操作将是什么。因此,您不应将eq 匹配器与current_path 一起使用,而应使用具有内置等待/重试行为的have_current_path 匹配器

    expect(page).to have_current_path(demographic_information_patient_path(1))
    

    【讨论】:

    • 感谢您的回答。我已按照您的建议更改了代码,但出现此错误:Failure/Error: expect(page).to have_current_path(demographic_information_patient_path(1)) expected #<:session:0x0000000c47f548> to response has_current_path?
    • @Abhi 你用的是什么版本的水豚? have_current_path 匹配器是近 18 个月前在 2.5.0 版本中添加的,您可能需要更新。或者,也许您的 spec_helper 中不需要 capybara/rspec - github.com/teamcapybara/capybara#using-capybara-with-rspec
    猜你喜欢
    • 2016-01-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-05
    • 2020-10-19
    相关资源
    最近更新 更多