小编Hus*_*enK的帖子

在Rails单元测试中使用assert_match和regex

我正在研究Rails Tutorial第11章ex 2

本教程要求通过替换FILL_IN变量来检查微博的数量(可以是任何数字)来完成测试

  test "micropost sidebar count" do
    log_in_as(@user)
    get root_path
    assert_match "#{FILL_IN} microposts", response.body
    # User with zero microposts
    other_user = users(:mallory)
    log_in_as(other_user)
    get root_path
    assert_match "0 microposts", response.body
    other_user.microposts.create!(content: "A micropost")
    get root_path
    assert_match FILL_IN, response.body
  end
Run Code Online (Sandbox Code Playgroud)

在第一个FILL_IN中,我在任一数字的正则表达式中添加/^[1-9]\d*$/并尝试通过将其赋值给变量进行插值,如下所示

regex_number = /^[1-9]\d*$/
assert_match "#{regex_number } microposts", response.body
Run Code Online (Sandbox Code Playgroud)

这对我不起作用(我的灯具的响应体中有34个微博)

我使用工具检查了正则表达式,应该匹配34

最后,你如何检查像复数这样的复数的匹配这是因为最后的FILL_IN用于"1微博"

regex ruby-on-rails railstutorial.org

3
推荐指数
1
解决办法
2006
查看次数

标签 统计

railstutorial.org ×1

regex ×1

ruby-on-rails ×1