0

好的,所以我想创建一个测试来检查所有页面是否都有特定的标题。但是,我认为如果我可以将页面标题包含在一个中会很好,array这样我就不必为每个页面复制块。它允许我通过修改pages array.

我遇到的问题是页面变量没有在测试中被插值。那么这是语法错误还是 Rspec 不允许在it should do...块内插值?

describe "LayoutLinks" do

    page = ["home", "contact", "about", "help"]
    i = page.count
    x = 0

    while x < i  
        it "should have a #{page[x]} page" do
        get "#{page[x]}"
        response.should have_selector("title", :content => "#{page[x]}")
        end
    x += 1
    end

end

测试显示以下故障:

  1) PagesController LayoutLinks should have a help page
     Failure/Error: get "#{page[x]}"
     ActionController::RoutingError:
       No route matches {:controller=>"pages", :action=>""}
     # ./spec/controllers/layout_links_spec.rb:14:in `block (3 levels) in <top (required)>'

  2) PagesController LayoutLinks should have a contact page
     Failure/Error: get "#{page[x]}"
     ActionController::RoutingError:
       No route matches {:controller=>"pages", :action=>""}
     # ./spec/controllers/layout_links_spec.rb:14:in `block (3 levels) in <top (required)>'

  3) PagesController LayoutLinks should have a about page
     Failure/Error: get "#{page[x]}"
     ActionController::RoutingError:
       No route matches {:controller=>"pages", :action=>""}
     # ./spec/controllers/layout_links_spec.rb:14:in `block (3 levels) in <top (required)>'

  4) PagesController LayoutLinks should have a home page
     Failure/Error: get "#{page[x]}"
     ActionController::RoutingError:
       No route matches {:controller=>"pages", :action=>""}

这里的失败错误是显而易见的。它不应该说 get"#{page[x]}"而是应该是get homeandget about等等......

我该如何补救?谢谢您的帮助。非常感激 :)

4

1 回答 1

0

尝试以下操作:

describe "LayoutLinks" do
  %w(home contact about help).each do |page|
    it "should have a #{page} page" do
      get page
      response.should have_selector("title", content: page)
    end
  end
end

%w 创建一个字符串数组(空格变成逗号,所以你得到 ["home", "contact", etc])

于 2013-09-01T21:34:20.643 回答