Ruby on Rails教程,第10.3.3节未通过测试

时间:2011-12-25 22:32:59

标签: ruby-on-rails ruby testing rspec

使用Rails 3.1.0和Ruby 1.9.2p290,我使用10.30列表来测试sample_app:

   it "should paginate users" do
    get :index
    ...
    response.should have_selector("a", :href => "/users?page=2",
                                       :content => "2")
    ...
  end

结果是:

Failures:

  1) UsersController GET 'index' for signed-in users should paginate users
     Failure/Error: response.should have_selector("a", :href => "/users?page=2",
       expected following output to contain a <a href='/users?page=2'>2</a> tag:

虽然返回的网页包含以下行:

 <a rel="next" href="/users?escape=false&amp;page=2">2</a>

由于我仍然不理解has_selector的完整用法,包括它与regexp的关系,我改变了测试,如下所示:

it "should paginate users" do
    get :index
    ...
    response.should have_selector('a', :rel => "next", 
                                       :href => "/users?escape=false&amp;page=2",
                                       :content => "2")
  end

得到了:

Failures:

  1) UsersController GET 'index' for signed-in users should paginate users
     Failure/Error: response.should have_selector('a', :rel => "next",
       expected following output to contain a <a rel='next' href='/users?escape=false&amp;page=2'>2</a> tag:
       <!DOCTYPE html>
...

虽然我提到该页面包含以下表达式:

<a rel="next" href="/users?escape=false&amp;page=2">2</a>

所以我猜我在尝试过滤这个表达式时做错了。 我做了一些关于如何使用have_selector的调查,但是找不到任何有意义的东西。

任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

使用以下

  it "should paginate users" do
    get :index
    response.should have_selector("div.pagination")
    response.should have_selector("span.disabled", :content => "Previous")
    response.should have_selector("a", :href => "/users?escape=false&page=2",
                                       :content => "2")
    response.should have_selector("a", :href => "/users?escape=false&page=2",
                                       :content => "Next")
  end

我认为转义参数是由于will_paginate的变化(但我不确定。)