ruby-on-rails - 在 rails 中使用 rspec 测试标题

标签 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

当我只想验证标题时,为什么还要谈论 CSS?考虑到我想做第一个代码示例正在做的事情,我这样做是否正确?

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

谢谢。

最佳答案

正确的语法是:

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?

关于ruby-on-rails - 在 rails 中使用 rspec 测试标题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28496571/

相关文章:

ruby-on-rails - 验证少数未保存的对象

ruby-on-rails - after_create :foo vs after_commit :bar, :on => :创建

ruby - 如何在 Ruby 中初始化哈希的大小?

ruby - 如何使用 ruby​​zip 将 zip 存档中的目录复制到第二个 zip 存档?

reactjs - React 测试库 - userEvent 上传到输入 : "cannot find toLowerCase"

multithreading - 如何优化Grails构建和测试执行速度?

ruby-on-rails - 使用 RSpec 进行 Rails 片段缓存测试

html - 如何将整个 pdf 页面背景色白色更改为 rails 中的其他颜色

ruby - Ruby 中是否有允许插值但不允许转义字符的字符串语法?

reactjs - Jest 无法从测试文件中找到模块