【问题标题】:Rails Integration Test SyntaxRails 集成测试语法
【发布时间】:2016-01-23 15:02:05
【问题描述】:

在 Michael Hartl 的 Ruby on Rails 教程中,作者实现了集成测试来测试布局链接。他使用的语法:

test "layout links" do

“布局链接”的作用是告知读者测试在做什么,还是代码的一部分?

再往下,他运行assert_select 来测试href 链接是否正常工作:

assert_select "a[href=?]", help_path   

为什么[href=?]在括号中,问号起什么作用?

test/integration/site_layout_test.rb 的完整代码如下:

require 'test_helper'
class SiteLayoutTest < ActionDispatch::IntegrationTest
  test "layout links" do
    get root_path
    assert_template 'static_pages/home'
    assert_select "a[href=?]", root_path, count: 2
    assert_select "a[href=?]", help_path
    assert_select "a[href=?]", about_path
    assert_select "a[href=?]", contact_path
  end
end

【问题讨论】:

    标签: ruby-on-rails


    【解决方案1】:

    “布局链接”只是用来命名测试

    assert_select 记录在 here 中,并且可以采用带有替换值的 CSS 选择器表达式(? 是发生替换的位置)所以

    assert_select "a[href=?]", help_path

    基本一样

    assert_select "a[href=#{help_path}]"

    【讨论】:

    • 谢谢,这有帮助!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-12-11
    • 2013-02-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多