{"id":1164,"date":"2022-08-30T15:13:27","date_gmt":"2022-08-30T15:13:27","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/11\/09\/broken-controller-tests-after-installing-capybara-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:13:27","modified_gmt":"2022-08-30T15:13:27","slug":"broken-controller-tests-after-installing-capybara-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/broken-controller-tests-after-installing-capybara-collection-of-common-programming-errors\/","title":{"rendered":"Broken controller tests after installing Capybara?-Collection of common programming errors"},"content":{"rendered":"<p>I had a bunch of combined controller\/view tests written with rspec. I added the Capybara gem and wrote some integrations tests which pass fine. The only problem is that now in all my controller tests, where I have<\/p>\n<p>response.should have_selector(&#8220;some selector&#8221;)<\/p>\n<p>rspec gives errors such as:<\/p>\n<pre><code>NoMethodError:\n       undefined method `has_selector?' for #\n<\/code><\/pre>\n<p>when I run controller tests. I&#8217;m guessing that Capybara is being used in my controller tests and has overwritten some Rspec methods. How can I fix this?<\/p>\n<pre><code># gemfile.rb\ngroup :test do\n  gem 'rspec'\n  gem \"capybara\"\n  gem \"launchy\"\n  gem 'factory_girl_rails', '1.0'\nend\n\n# spec_helper.rb\nRSpec.configure do |config|\n  config.include IntegrationSpecHelper, :type =&gt; :request\nend\n<\/code><\/pre>\n<p>Here&#8217;s an example of a failing test:<\/p>\n<pre><code># spec\/controllers\/books_controller_spec.rb\nrequire 'spec_helper'\n\ndescribe BooksController do\n  render_views\n\n  it \"should have the right page title\" do\n    get :show, :id =&gt; @book.ean\n    response.should have_selector(\"title\", :content =&gt; \"Lexicase | \" + @book.title)\n  end\nend\n<\/code><\/pre>\n<p>and it&#8217;s associated error:<\/p>\n<pre><code>  1) BooksController GET 'show' should have the right page title\n     Failure\/Error: response.should have_selector(\"title\", :content =&gt; \"Lexicase | \" + @book.title)\n     NoMethodError:\n       undefined method `has_selector?' for #\n     # .\/spec\/controllers\/books_controller_spec.rb:23:in `block (3 levels) in '\n<\/code><\/pre>\n<ol>\n<li>\n<p>You were probably using Webrat earlier, and <strong>has_selector?<\/strong> is a Webrat matcher. Capybaras doesn&#8217;t have a has_selector matcher, it has a matcher called <strong>has_css<\/strong>. You may want to replace the &#8220;has_selector&#8221; with &#8220;has_css&#8221;.<\/p>\n<\/li>\n<li>\n<p>Capybara helpers only works within requests specs. Either create a new request spec, or pass in :type =&gt; :request in the describe block part, like so:<\/p>\n<pre><code>describe \"test for the testing test\", :type =&gt; :request do\n  it \"should work with capybara\" do\n    visit root_path\n    click_link \"Home\"\n    page.should WHATEVA\n  end\nend\n<\/code><\/pre>\n<p>I realize this question was asked a long time ago, but I thought I would share anyway. GLHF<\/p>\n<\/li>\n<\/ol>\n<p id=\"rop\"><small>Originally posted 2013-11-09 23:33:46. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>I had a bunch of combined controller\/view tests written with rspec. I added the Capybara gem and wrote some integrations tests which pass fine. The only problem is that now in all my controller tests, where I have response.should have_selector(&#8220;some selector&#8221;) rspec gives errors such as: NoMethodError: undefined method `has_selector?&#8217; for # when I run [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3,1],"tags":[],"class_list":["post-1164","post","type-post","status-publish","format-standard","hentry","category-capybara","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1164","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=1164"}],"version-history":[{"count":1,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1164\/revisions"}],"predecessor-version":[{"id":8745,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1164\/revisions\/8745"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=1164"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=1164"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=1164"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}