capybara have_title NoMethodError

时间:2016-06-27 13:37:09

标签: ruby-on-rails capybara rspec-rails

目前,这是一个简单的项目 - 只是几个静态页面。我正在开发一个通用的测试框架,但我正在努力区分不同的测试选项。我添加了Rspec,Capybara,Faker,Factory Girl,Spring和shoulda(虽然我现在没有使用shoulda匹配器)。

我有这个控制器测试文件:

require 'rails_helper'

RSpec.describe StaticPagesController, type: :controller do

  describe "GET #a_page" do
    before(:each) { get :a_page }

    it "returns http success" do
      expect(response).to have_http_status(:success)
    end
    it "has a page title Static Site" do 
      expect(response).to have_title('Static Site') 
    end 
  end

end

当它通过guard运行时,它会抛出一个错误堆栈:

23:13:39 - INFO - Run all
23:13:39 - INFO - Running all specs
Running via Spring preloader in process 4498
Running via Spring preloader in process 4506
/home/steve/workspaces/static_site/db/schema.rb doesn't exist yet. Run `rake db:migrate` to create it, then try again. If you do not intend to use a database, you should instead alter /home/steve/workspaces/static_site/config/application.rb to limit the frameworks that will be loaded.
.F

Failures:

  1) StaticPagesController GET #a_page has a page title Static Site
     Failure/Error: expect(response).to have_title('Static Site')

     NoMethodError:
       undefined method `match' for nil:NilClass
       Did you mean?  catch
     # /home/steve/.rvm/gems/ruby-2.3.1/gems/capybara-2.7.1/lib/capybara/queries/title_query.rb:18:in `resolves_for?'
     # /home/steve/.rvm/gems/ruby-2.3.1/gems/capybara-2.7.1/lib/capybara/node/document_matchers.rb:20:in `block in assert_title'
     # /home/steve/.rvm/gems/ruby-2.3.1/gems/capybara-2.7.1/lib/capybara/node/simple.rb:144:in `synchronize'
     # /home/steve/.rvm/gems/ruby-2.3.1/gems/capybara-2.7.1/lib/capybara/node/document_matchers.rb:19:in `assert_title'
     # /home/steve/.rvm/gems/ruby-2.3.1/gems/capybara-2.7.1/lib/capybara/rspec/matchers.rb:105:in `matches?'
     # ./spec/controllers/static_pages_controller_spec.rb:34:in `block (3 levels) in <top (required)>'
     # /home/steve/.rvm/gems/ruby-2.3.1/gems/spring-commands-rspec-1.0.4/lib/spring/commands/rspec.rb:18:in `call'
     # /home/steve/.rvm/gems/ruby-2.3.1/gems/spring-1.7.1/lib/spring/command_wrapper.rb:38:in `call'
     # /home/steve/.rvm/gems/ruby-2.3.1/gems/spring-1.7.1/lib/spring/application.rb:191:in `block in serve'
     # /home/steve/.rvm/gems/ruby-2.3.1/gems/spring-1.7.1/lib/spring/application.rb:161:in `fork'
     # /home/steve/.rvm/gems/ruby-2.3.1/gems/spring-1.7.1/lib/spring/application.rb:161:in `serve'
     # /home/steve/.rvm/gems/ruby-2.3.1/gems/spring-1.7.1/lib/spring/application.rb:131:in `block in run'
     # /home/steve/.rvm/gems/ruby-2.3.1/gems/spring-1.7.1/lib/spring/application.rb:125:in `loop'
     # /home/steve/.rvm/gems/ruby-2.3.1/gems/spring-1.7.1/lib/spring/application.rb:125:in `run'
     # /home/steve/.rvm/gems/ruby-2.3.1/gems/spring-1.7.1/lib/spring/application/boot.rb:19:in `<top (required)>'
     # -e:1:in `<main>'

Finished in 0.029 seconds (files took 2.54 seconds to load)
2 examples, 1 failure

Failed examples:

rspec ./spec/controllers/static_pages_controller_spec.rb:33 # StaticPagesController GET #a_page has a page title Static Site

第一次测试运行正常,没有第二次,我得到一个干净的结果。我花了很多时间浏览我的配置,看起来还不错。我还查看了文档和一些支持网站。

有人可以帮忙吗?

2 个答案:

答案 0 :(得分:1)

需要针对html / xml文档元素(或解析为文档的字符串)调用Capybara匹配器,而不是针对响应对象。默认情况下,Capybaras匹配器通常仅在功能和视图规格(非控制器)中可用,是否有特殊原因将它们包含在控制器规格中?验证页面标题确实应该更倾向于作为视图规范而不是控制器(默认情况下,视图不会在控制器规范中呈现 - https://www.relishapp.com/rspec/rspec-rails/docs/controller-specs)。

答案 1 :(得分:0)

  1. 正如@Thomas Walpole 指出的那样,Capybara is not enabled in controller specs by default。一种选择是将测试类型更改为 feature (type: :feature)。
  2. 要使 Capybara 的内置匹配器 have_title 正常工作,您需要使用 Capybara API 中的 visit,而不是 get 中的 ActionDispatch::IntegrationTest