gpt4 book ai didi

ruby-on-rails - 我的 rspec 测试不会通过 : Michael Hartl's rails tutorial

转载 作者:行者123 更新时间:2023-12-04 11:30:22 25 4
gpt4 key购买 nike

我在第五章的最后做练习。我应该测试链接是否转到正确的页面。这是我的测试代码。

require 'spec_helper'

describe "LayoutLinks" do

it "should have the right links on the layout" do
visit root_path
click_link "About"
response.should have_selector('title', :content => "About")
click_link "Home"
response.should have_selector('title', :content => "Home")
click_link "Help"
response.should have_selector('title', :content => "Help")
click_link "Contact"
response.should have_selector('title', :content => "Contact")
click_link "Sign up now!"
response.should have_selector('title', :content => "Sign up")
end
end

除了最后一个测试,一切都通过了。它说找不到带有“立即注册!”文本的链接。 .我知道该页面确实有一个“立即注册!”关联。我想可能它在源代码中的呈现方式不同,但是当我查看源代码时,它看起来很正常 <a href="/signup" class="signup_button round">Sign up now!</a> .根据我的理解,它应该点击链接,然后测试标题是否与 :content 符号匹配。我误解了什么吗?

这是我得到的错误:
Failures:

1) LayoutLinks should have the right links on the layout
Failure/Error: click_link "Sign up now!"
Webrat::NotFoundError:
Could not find link with text or title or id "Sign up now!"

最佳答案

我相信问题在于“立即注册!”链接实际上并非在所有页面上,而且该测试实际上是在浏览页面。

至少在我前段时间浏览的本教程版本中,该链接仅在主页上。

如果您添加 visit root_path就在最后一次之前 click_link它可能会起作用。

更好的测试应该在与主页相关的独立测试中进行检查。

关于ruby-on-rails - 我的 rspec 测试不会通过 : Michael Hartl's rails tutorial,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5361916/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com