用rails中的rspec测试标题

时间:2015-02-13 09:43:07

标签: ruby-on-rails ruby testing rspec title

启动:

it "doit avoir le bon titre" do
  get 'home'
  response.should have_selector("title", :content => "Simple App du Tutoriel Ruby on Rails | Accueil")
end

我最终得到了这个,因为我没有使用相同版本的rspec或rails而不是我正在遵循的教程:

it "doit avoir le bon titre" do
    get 'home'
    expect(response).to have_selector("title", :text => "Simple App du Tutoriel Ruby on Rails | Accueil")
end

现在它正在告诉我这个

Failure/Error: expect(response).to have_selector("title", :text => "Simple App du Tutoriel Ruby on Rails | Accueil")
       expected to find css "title" with text "Simple App du Tutoriel Ruby on Rails | Accueil" but there were no matches

为什么我要做的就是验证标题?我是否正确地考虑到我想做的第一个代码样本正在做什么?

显然不是因为我有错误,而是我做错了什么?

感谢。

2 个答案:

答案 0 :(得分:1)

正确的语法是:

it "doit avoir le bon titre" do
  get :home
  expect(response.body).to have_title('Simple App du Tutoriel Ruby on Rails | Accueil')
end

感谢Siekfried和他的链接部分解决了:How can I test the page title with Capybara 2.0?

答案 1 :(得分:0)

你试过了吗?

    it "doit avoir le bon titre" do
      get :home
      expect(response.body).to include('<title>My Title</title>')
    end
  end

...

相关问题