expect(page) .to have_content not working - ruby-on-rails

The expect(page).to have_content syntax is legal according to the capybara docs, but it does not seem to work
I have added the spec file and the error below
require 'spec_helper.rb'
feature "Looking up recipes", js: true do
before do
Recipe.create!(name: 'Baked Potato w/ Cheese')
Recipe.create!(name: 'Garlic Mashed Potatoes')
Recipe.create!(name: 'Potatoes Au Gratin')
Recipe.create!(name: 'Baked Brussel Sprouts')
end
scenario "finding recipes" do
visit '/'
fill_in "keywords", with: "baked"
click_on "Search"
expect(page).to have_content("Baked Potato")
expect(page).to have_content("Baked Brussel Sprouts")
end
end
Error when running spec:
sai#saip:~/rails_apps/receta$ rspec spec/features/search_spec.rb
F
Failures:
1) Looking up recipes finding recipes
Failure/Error: expect(page).to have_content("Baked Potato")
only the `receive` matcher is supported with `expect(...).to`, but you have provided: #<Capybara::RSpecMatchers::HaveText:0x000000057c0470>
# ./spec/features/search_spec.rb:15:in `block (2 levels) in <top (required)>'
Finished in 9.95 seconds
1 example, 1 failure
Failed examples:
rspec ./spec/features/search_spec.rb:10 # Looking up recipes finding recipes
Randomized with seed 31312
These are the versions of the various rspec gems
rspec (2.99.0)
rspec-collection_matchers (1.1.2)
rspec-core (2.99.2)
rspec-expectations (2.99.2)
rspec-mocks (2.99.3)
rspec-rails (2.99.0)
The vesion of capybara is capybara (2.4.4)

You're using the wrong version of RSpec for that syntax. The use of expect(...).to ... is the accepted way of writing tests in RSpec 3+. You're using 2.99.
Your syntax is
page.should have_content("Baked Potato")
You should really upgrade RSpec.

Related

Rails 5, Rspec-rails, Capybara, Selenium-webdriver - Unable to find Mozilla geckodriver

I try test my new gem, with use js.
But got errors
rails 5.1.5
rspec-rails 3.7.2
capybara 2.18.0
selenium-webdriver 3.10.0
app/spec/rails_helper.rb
require 'spec_helper'
...
require 'rspec/rails'
require 'capybara/rspec'
require 'capybara/rails'
require 'support/factory_bot'
app/spec/features/view_helper_spec.rb
require 'rails_helper'
RSpec.describe DynamicNestedForms::ViewHelpers do
# it "does something" do
it "displays patient" do
patient = create(:patient)
visit edit_patient_path(patient)
expect(page).to have_selector("input[value='#{patient.name}']")
end
it "displays physicians", :js => true do
patient = create(:patient)
physician = create(:physician)
visit edit_patient_path(patient)
fill_in "autocomplete_nested_content", with: physician[0..2]
expect(page).to have_selector(".ui-menu-item-wrapper", text: physician.name)
end
end
I get this error:
Selenium::WebDriver::Error::WebDriverError:
Unable to find Mozilla geckodriver. Please download the server from https://github.com/mozilla/geckodriver/releases and place it somewhere on your PATH. More info at https://developer.mozilla.org/en-US/docs/Mozilla/QA/Marionette/WebDriver.
The easiest solution to this is to add the webdrivers gem to your project which will automatically download/install the relevant driver program and inform selenium-webdriver of its location - https://github.com/titusfortner/webdrivers

Rails Rspec IntegrationTest Capybara

I have started to test my app via Rspec (Capybara). This is how I am doing it:
require 'rails_helper'
RSpec.describe "Homepages", type: :request do
describe "GET / without login" , js: true do
before(:all) do
Employee.create(username: "username", password: "12345", password_confirmation: "12345")
end
it "works!" do
visit root_path
fill_in "loginname", with: "username"
fill_in "password", with: "12345"
click_button('sign_in')
end
end
end
Because of env namely "TEST-ENV" I have to create an employee at first.
the problem is, if I run 'rake spec:requests', I get this errors:
1) Homepages GET / without login works!
Got 0 failures and 2 other errors:
1.1) Failure/Error:
def initialize(template, original_exception)
super(original_exception.message)
#template, #original_exception = template, original_exception
#sub_templates = nil
set_backtrace(original_exception.backtrace)
end
ArgumentError:
wrong number of arguments (1 for 2)
#/.rvm/gems/ruby-2.1.1/gems/actionview-4.2.7/lib/action_view/template/error.rb:64:in `initialize'
# /.rvm/gems/ruby-2.1.1/gems/capybara-2.10.1/lib/capybara/session.rb:128:in `exception'
# /.rvm/gems/ruby-2.1.1/gems/capybara-2.10.1/lib/capybara/session.rb:128:in `raise'
# /.rvm/gems/ruby-2.1.1/gems/capybara-2.10.1/lib/capybara/session.rb:128:in `rescue in raise_server_error!'
# /.rvm/gems/ruby-2.1.1/gems/capybara-2.10.1/lib/capybara/session.rb:125:in `raise_server_error!'
# /.rvm/gems/ruby-2.1.1/gems/capybara-2.10.1/lib/capybara/session.rb:113:in `reset!'
# /.rvm/gems/ruby-2.1.1/gems/capybara-2.10.1/lib/capybara.rb:334:in `block in reset_sessions!'
# /.rvm/gems/ruby-2.1.1/gems/capybara-2.10.1/lib/capybara.rb:334:in `reverse_each'
# /.rvm/gems/ruby-2.1.1/gems/capybara-2.10.1/lib/capybara.rb:334:in `reset_sessions!'
# /.rvm/gems/ruby-2.1.1/gems/capybara-2.10.1/lib/capybara/rspec.rb:21:in `block (2 levels) in <top (required)>'
# ------------------
# --- Caused by: ---
# Capybara::CapybaraError:
# Your application server raised an error - It has been raised in your test code because Capybara.raise_server_errors == true
# /.rvm/gems/ruby-2.1.1/gems/capybara-2.10.1/lib/capybara/session.rb:126:in `raise_server_error!'
I'm not sure but I think better if type = feature
Sample
require "rails_helper"
RSpec.feature "Homepages", type: :feature do
before do
Employee.create(username: "username", password: "12345", password_confirmation: "12345")
end
context "GET / without login" do
scenario "works!", js: true do
visit root_path
fill_in "loginname", with: "username"
fill_in "password", with: "12345"
click_button('sign_in')
end
end
end
Please make sure your input name is correct by inspect element to get the input name
I think
fill_in "loginname", with: "username"
maybe be
fill_in "user[loginname]", with: "username"
As others have stated, Capybara tests should be of type 'feature' not 'request', however that's not the primary cause of your error. Your apps code is raising an exception during template rendering, and then you're running into a bug in the current version of Capybara with handling exceptions whose initializers take multiple parameters. As long as you're not using jRuby you can lock your Capybara version to 2.10.0 and you should see the correct error your app is raising. If you are using jRuby, or if you prefer to not lock to an older version, you can specify to use the master branch of Capybara
gem 'capybara', github: 'teamcapybara/capybara'
which has the bug fixed.
As a side-note, you've tagged this question with capybara-webkit when you're not actually using the capybara-webkit driver (since it only supports up to Capybara 2.7.1 currently), so you might want to change the tag to just capybara.

Failure/Error: visit movies_url NameError: undefined local variable or method `movies_url' for #<RSpec::Core::ExampleGroup::Nested_1:0x007ff450c81800>

I am just getting started with RSpec. I receive the error of having an undefined variable or method 'movies_url' when running my spec file. This is what it looks like when I run rspec:
bundle exec rspec spec/list_movies_spec.rb
F
Failures:
1) when viewing the list of movies should show the movies
Failure/Error: visit movies_url
NameError:
undefined local variable or method `movies_url' for #<RSpec::Core::ExampleGroup::Nested_1:0x007ff450c81800>
# ./spec/list_movies_spec.rb:24:in `block (2 levels) in <top (required)>'
Finished in 0.01881 seconds
1 example, 1 failure
Failed examples:
rspec ./spec/list_movies_spec.rb:4 # when viewing the list of movies should show the movies
Randomized with seed 45910
list_movies_spec.rb this is my test I am running.
require "spec_helper"
describe "when viewing the list of movies" do
it "should show the movies" do
movie1 = Movie.create(title: "Iron Man",
rating: "PG-13",
total_gross: 318412101.00,
description: "Tony Stark builds an armored suit to fight the throes of evil",
released_on: "2008-05-02")
movie2 = Movie.create(title: "Superman",
rating: "PG",
total_gross: 134218018.00,
description: "Clark Kent grows up to be the greatest super-hero",
released_on: "1978-12-15")
movie3 = Movie.create(title: "Spider-Man",
rating: "PG-13",
total_gross: 403706375.00,
description: "Peter Parker gets bit by a genetically modified spider",
released_on: "2002-05-03")
visit movies_url
expect(page).to have_text("3 Movies")
expect(page).to have_text("Iron Man")
expect(page).to have_text("Batman")
expect(page).to have_text("Captain America")
end
end
Gemfile this is what I have installed with Capybara and RSpec versions.
group :test, :development do
gem "minitest"
gem "rspec-rails", "2.13.1"
end
group :test do
gem "capybara", "2.1.0"
end
I do have the proper route that should be matching the spec movies_url.
rake routes
Prefix Verb URI Pattern Controller#Action
movies GET /movies(.:format) movies#index
On my console I run:
app.movies_url
=> "http://www.example.com/movies"
I have looked else where for clues but I have not had any success. I think it is an RSpec and Capybara version issue?
More details:
gem list rspec
*** LOCAL GEMS ***
rspec-core (3.3.1, 2.13.1)
rspec-expectations (3.3.0, 2.13.0)
rspec-mocks (3.3.1, 2.13.1)
rspec-rails (3.3.2, 2.13.1)
rspec-support (3.3.0)
gem list capybara
*** LOCAL GEMS ***
capybara (2.1.0)
Place this in spec file.
include Rails.application.routes.url_helpers
The above code, would make all the urls helpers such as root_path, movies_url accessible in your test. But to visit we should define the host in the test.rb environment file as well. For which place the below code in test.rb
Rails.application.routes.default_url_options[:host]= 'localhost:3000'

Rails Capybara not working

I am trying to run a simple test using rspec and capybara:
describe "Create" do
visit new_client_path
page.should have_selector('h1', text: "New Client")
end
However i get error:
undefined local variable or method `visit'
And if i remove the visit line i get this error:
undefined local variable or method `page'
I have included the DSL in the spec_helper file.
what is the problem?
Thanks
It seems that you simply forgot to put your test inside the it block:
describe "Create" do
it "does something" do
visit new_client_path
page.should have_selector('h1', text: "New Client")
end
end
Assuming you are using Rails 3 and RSpec, have you placed your specs in features instead of requests? You shouldn't need to include the DSL if you are using features directory.
Also check that you have latest version of the gems.
From https://github.com/jnicklas/capybara:
Using Capybara with RSpec
Load RSpec 2.x support by adding the following line (typically to your
spec_helper.rb file):
require 'capybara/rspec' If you are using Rails, put your Capybara
specs in spec/features.

Integration tests with webrat and Rails3

I'm upgrading a Rails 2.3.5 app to Rails 3.0.3. But my integration tests
aren't working. I'm getting this error:
NoMethodError: undefined method `content_type' for nil:NilClass
The line to blame is
assert_select "input#artist.title.unsolved", 1
My test_helper.rb for webrat looks likes this:
require "webrat"
require 'webrat/core/matchers'
include Webrat::Methods
Webrat.configure do |config|
config.mode = :rack
end
I'm using shoulda 2.11.3 and webrat 0.7.3 for the testing. I've read,
that webrat and shoulda are compatible with Rails3.
Anyone an idea how to fix this?
Thanks!
tux
ADDITION:
It seems, that the NoMethodError appears from shoulda, not from Webrat, as mentioned in the title. Here is the trace:
NoMethodError: undefined method `content_type' for nil:NilClass
/Users/23tux/.rvm/gems/ruby-1.9.2-p136#rails3/gems/activesupport-3.0.3/lib/active_support/whiny_nil.rb:48:in `method_missing'
/Users/23tux/.rvm/gems/ruby-1.9.2-p136#rails3/gems/actionpack-3.0.3/lib/action_dispatch/testing/assertions/selector.rb:605:in `response_from_page_or_rjs'
/Users/23tux/.rvm/gems/ruby-1.9.2-p136#rails3/gems/actionpack-3.0.3/lib/action_dispatch/testing/assertions/selector.rb:213:in `assert_select'
test/integration/user_integration_test.rb:52:in `block (3 levels) in <class:UserIntegrationTest>'
/Users/23tux/.rvm/gems/ruby-1.9.2-p136#rails3/gems/shoulda-context-1.0.0.beta1/lib/shoulda/context/context.rb:412:in `call'
/Users/23tux/.rvm/gems/ruby-1.9.2-p136#rails3/gems/shoulda-context-1.0.0.beta1/lib/shoulda/context/context.rb:412:in `block in run_current_setup_blocks'
/Users/23tux/.rvm/gems/ruby-1.9.2-p136#rails3/gems/shoulda-context-1.0.0.beta1/lib/shoulda/context/context.rb:411:in `each'
/Users/23tux/.rvm/gems/ruby-1.9.2-p136#rails3/gems/shoulda-context-1.0.0.beta1/lib/shoulda/context/context.rb:411:in `run_current_setup_blocks'
/Users/23tux/.rvm/gems/ruby-1.9.2-p136#rails3/gems/shoulda-context-1.0.0.beta1/lib/shoulda/context/context.rb:393:in `block in create_test_from_should_hash'
And here is the whole block context around the assert_select:
class SongIntegrationTest < ActionController::IntegrationTest
context "a visitor" do
context "solving a song" do
setup do
#song = Song.make
visit song_path(#song)
end
should "have two guess fields" do
assert_select "input#artist.title.unsolved", 1
assert_select "input#title.title.unsolved", 1
end
Maybe the assert_select isn't longer available in Rails 3 with Shoulda.
Hope someone can help me!
thx!
There is a fork of webrat that works with rails3 at https://github.com/joakimk/webrat. At some point it should get pulled into the main branch. rails 2.3 is still very popular at this time.

Resources