I'm getting the following error for my index view:
1) events/index.html.erb should display all the events in a matrix formation
Failure/Error: render
ActionView::Template::Error:
No route matches {:action=>"show", :controller=>"events", :format=>nil, :id=>nil, :locale=>#<Event id: 2, name: "Miles Eichmann", description: "[\"Voluptas quia velit non est.\", \"Commodi aperiam p...", image: "event.jpg", location: nil, created_at: "2015-08-09 13:55:02", updated_at: "2015-08-09 13:55:02", whenwhen: "2015-09-07 04:00:00", speaker_id: 1, map_address: "800 Rene Levesque, Montreal, H3G 5K4", lon: 0.0, lat: 0.0, price: 3550>} missing required keys: [:id]
The test in spec/views/events/index.html.erb_spec.rb:
require 'rails_helper'
RSpec.describe "events/index.html.erb", :type => :view do
let!(:speaker) { FactoryGirl.create(:speaker) }
let!(:events) { FactoryGirl.create_list(:event, 10, speaker_id: speaker.id ) }
it "should display all the events in a matrix" do
assign(:events, events)
render
puts response
end
end
I don't understand why if I have all the actions pointing to index, the error points to :action=>"show".
If I delete the two let statements or take the ! from them, the test passes and the headers and titles of the index page are displayed (with no events, of course).
The index.html.erb makes a call to helper functions to display the events in a matrix as follows:
<!-- app/views/events/index.html.erb -->
...
<% if Event.future.count == 0 %>
<br><%= t(:staytuned) %>
<% else %>
<h2><%= t(:futureevents).mb_chars.upcase %></h2>
<%= raw(display_matrix_events(Event.future)) %>
<% end %>
...
which is one of the code segments that should be called, as the factory is creating events in the future:
#spec/facories/event.rb
require 'faker'
FactoryGirl.define do
factory :event do
....
the_date { Faker::Date.forward(30) }
....
end
end
The code works fine when running on the browser.
Here is the stack trace:
Failures:
1) events/index.html.erb should display all the events in a matrix
Failure/Error: render
ActionView::Template::Error:
No route matches {:action=>"show", :controller=>"events", :format=>nil, :id=>nil, :locale=>#<Event id: 1, name: "Columbus Goodwin", description: "[\"Quam est officia adipisci.\", \"Molestiae cumque vo...", image: "event.jpg", location: nil, created_at: "2015-08-09 20:22:27", updated_at: "2015-08-09 20:22:27", whenwhen: "2015-09-08 04:00:00", speaker_id: 1, map_address: "800 Rene Levesque, Montreal, H3G 5K4", lon: 0.0, lat: 0.0, price: 3550>} missing required keys: [:id]
# /home/mancilla/.rvm/gems/ruby-2.0.0-p451/gems/actionpack-4.0.4/lib/action_dispatch/journey/formatter.rb:39:in `generate'
# /home/mancilla/.rvm/gems/ruby-2.0.0-p451/gems/actionpack-4.0.4/lib/action_dispatch/routing/route_set.rb:595:in `generate'
# /home/mancilla/.rvm/gems/ruby-2.0.0-p451/gems/actionpack-4.0.4/lib/action_dispatch/routing/route_set.rb:625:in `generate'
# /home/mancilla/.rvm/gems/ruby-2.0.0-p451/gems/actionpack-4.0.4/lib/action_dispatch/routing/route_set.rb:661:in `url_for'
# /home/mancilla/.rvm/gems/ruby-2.0.0-p451/gems/actionpack-4.0.4/lib/action_dispatch/routing/url_for.rb:155:in `url_for'
# /home/mancilla/.rvm/gems/ruby-2.0.0-p451/gems/actionpack-4.0.4/lib/action_view/routing_url_for.rb:83:in `url_for'
# /home/mancilla/.rvm/gems/ruby-2.0.0-p451/gems/turbolinks-2.5.3/lib/turbolinks/xhr_url_for.rb:12:in `url_for_with_xhr_referer'
# /home/mancilla/.rvm/gems/ruby-2.0.0-p451/gems/actionpack-4.0.4/lib/action_dispatch/routing/route_set.rb:228:in `call'
# /home/mancilla/.rvm/gems/ruby-2.0.0-p451/gems/actionpack-4.0.4/lib/action_dispatch/routing/route_set.rb:268:in `block (2 levels) in define_url_helper'
# /home/mancilla/.rvm/gems/ruby-2.0.0-p451/gems/actionpack-4.0.4/lib/action_dispatch/routing/polymorphic_routes.rb:129:in `polymorphic_url'
# /home/mancilla/.rvm/gems/ruby-2.0.0-p451/gems/actionpack-4.0.4/lib/action_dispatch/routing/polymorphic_routes.rb:135:in `polymorphic_path'
# /home/mancilla/.rvm/gems/ruby-2.0.0-p451/gems/actionpack-4.0.4/lib/action_view/routing_url_for.rb:87:in `url_for'
# /home/mancilla/.rvm/gems/ruby-2.0.0-p451/gems/turbolinks-2.5.3/lib/turbolinks/xhr_url_for.rb:12:in `url_for_with_xhr_referer'
# /home/mancilla/.rvm/gems/ruby-2.0.0-p451/gems/actionpack-4.0.4/lib/action_view/helpers/url_helper.rb:180:in `link_to'
# ./app/views/events/_speaker_in_event_index.html.erb:6:in `_app_views_events__speaker_in_event_index_html_erb__148285862205269253_56219800'
# /home/mancilla/.rvm/gems/ruby-2.0.0-p451/gems/actionpack-4.0.4/lib/action_view/template.rb:143:in `block in render'
# /home/mancilla/.rvm/gems/ruby-2.0.0-p451/gems/activesupport-4.0.4/lib/active_support/notifications.rb:159:in `block in instrument'
# /home/mancilla/.rvm/gems/ruby-2.0.0-p451/gems/activesupport-4.0.4/lib/active_support/notifications/instrumenter.rb:20:in `instrument'
# /home/mancilla/.rvm/gems/ruby-2.0.0-p451/gems/activesupport-4.0.4/lib/active_support/notifications.rb:159:in `instrument'
# /home/mancilla/.rvm/gems/ruby-2.0.0-p451/gems/actionpack-4.0.4/lib/action_view/template.rb:141:in `render'
# /home/mancilla/.rvm/gems/ruby-2.0.0-p451/gems/actionpack-4.0.4/lib/action_view/renderer/partial_renderer.rb:306:in `render_partial'
# /home/mancilla/.rvm/gems/ruby-2.0.0-p451/gems/actionpack-4.0.4/lib/action_view/renderer/partial_renderer.rb:279:in `block in render'
# /home/mancilla/.rvm/gems/ruby-2.0.0-p451/gems/actionpack-4.0.4/lib/action_view/renderer/abstract_renderer.rb:38:in `block in instrument'
# /home/mancilla/.rvm/gems/ruby-2.0.0-p451/gems/activesupport-4.0.4/lib/active_support/notifications.rb:159:in `block in instrument'
# /home/mancilla/.rvm/gems/ruby-2.0.0-p451/gems/activesupport-4.0.4/lib/active_support/notifications/instrumenter.rb:20:in `instrument'
# /home/mancilla/.rvm/gems/ruby-2.0.0-p451/gems/activesupport-4.0.4/lib/active_support/notifications.rb:159:in `instrument'
# /home/mancilla/.rvm/gems/ruby-2.0.0-p451/gems/actionpack-4.0.4/lib/action_view/renderer/abstract_renderer.rb:38:in `instrument'
# /home/mancilla/.rvm/gems/ruby-2.0.0-p451/gems/actionpack-4.0.4/lib/action_view/renderer/partial_renderer.rb:278:in `render'
# /home/mancilla/.rvm/gems/ruby-2.0.0-p451/gems/actionpack-4.0.4/lib/action_view/renderer/renderer.rb:47:in `render_partial'
# /home/mancilla/.rvm/gems/ruby-2.0.0-p451/gems/actionpack-4.0.4/lib/action_view/helpers/rendering_helper.rb:27:in `render'
# /home/mancilla/.rvm/gems/ruby-2.0.0-p451/gems/actionpack-4.0.4/lib/action_view/test_case.rb:198:in `render'
# ./app/helpers/events_helper.rb:21:in `display_matrix_events'
# ./app/views/events/index.html.erb:18:in `_app_views_events_index_html_erb__3520103631403717635_53363480'
# /home/mancilla/.rvm/gems/ruby-2.0.0-p451/gems/actionpack-4.0.4/lib/action_view/template.rb:143:in `block in render'
# /home/mancilla/.rvm/gems/ruby-2.0.0-p451/gems/activesupport-4.0.4/lib/active_support/notifications.rb:159:in `block in instrument'
# /home/mancilla/.rvm/gems/ruby-2.0.0-p451/gems/activesupport-4.0.4/lib/active_support/notifications/instrumenter.rb:20:in `instrument'
# /home/mancilla/.rvm/gems/ruby-2.0.0-p451/gems/activesupport-4.0.4/lib/active_support/notifications.rb:159:in `instrument'
# /home/mancilla/.rvm/gems/ruby-2.0.0-p451/gems/actionpack-4.0.4/lib/action_view/template.rb:141:in `render'
# /home/mancilla/.rvm/gems/ruby-2.0.0-p451/gems/actionpack-4.0.4/lib/action_view/renderer/template_renderer.rb:49:in `block (2 levels) in render_template'
# /home/mancilla/.rvm/gems/ruby-2.0.0-p451/gems/actionpack-4.0.4/lib/action_view/renderer/abstract_renderer.rb:38:in `block in instrument'
# /home/mancilla/.rvm/gems/ruby-2.0.0-p451/gems/activesupport-4.0.4/lib/active_support/notifications.rb:159:in `block in instrument'
# /home/mancilla/.rvm/gems/ruby-2.0.0-p451/gems/activesupport-4.0.4/lib/active_support/notifications/instrumenter.rb:20:in `instrument'
# /home/mancilla/.rvm/gems/ruby-2.0.0-p451/gems/activesupport-4.0.4/lib/active_support/notifications.rb:159:in `instrument'
# /home/mancilla/.rvm/gems/ruby-2.0.0-p451/gems/actionpack-4.0.4/lib/action_view/renderer/abstract_renderer.rb:38:in `instrument'
# /home/mancilla/.rvm/gems/ruby-2.0.0-p451/gems/actionpack-4.0.4/lib/action_view/renderer/template_renderer.rb:48:in `block in render_template'
# /home/mancilla/.rvm/gems/ruby-2.0.0-p451/gems/actionpack-4.0.4/lib/action_view/renderer/template_renderer.rb:56:in `render_with_layout'
# /home/mancilla/.rvm/gems/ruby-2.0.0-p451/gems/actionpack-4.0.4/lib/action_view/renderer/template_renderer.rb:47:in `render_template'
# /home/mancilla/.rvm/gems/ruby-2.0.0-p451/gems/actionpack-4.0.4/lib/action_view/renderer/template_renderer.rb:17:in `render'
# /home/mancilla/.rvm/gems/ruby-2.0.0-p451/gems/actionpack-4.0.4/lib/action_view/renderer/renderer.rb:42:in `render_template'
# /home/mancilla/.rvm/gems/ruby-2.0.0-p451/gems/actionpack-4.0.4/lib/action_view/renderer/renderer.rb:23:in `render'
# /home/mancilla/.rvm/gems/ruby-2.0.0-p451/gems/actionpack-4.0.4/lib/action_view/helpers/rendering_helper.rb:24:in `render'
# /home/mancilla/.rvm/gems/ruby-2.0.0-p451/gems/actionpack-4.0.4/lib/action_view/test_case.rb:198:in `render'
# /home/mancilla/.rvm/gems/ruby-2.0.0-p451/gems/actionpack-4.0.4/lib/action_view/test_case.rb:118:in `render'
# ./spec/views/events/index.html.erb_spec.rb:28:in `block (2 levels) in <top (required)>'
Finished in 2.11 seconds (files took 3.79 seconds to load)
1 example, 1 failure
Failed examples:
rspec ./spec/views/events/index.html.erb_spec.rb:10 # events/index.html.erb should display all the events in a matrix formation
The action index is executed correctly but it has a sequence of renders and method calls which lead to a show action.
The app/views/events/index.html.erb file has a call to a method which renders a partial containing the line
<%= link_to image_tag(e.speaker.image.to_s, width: '150%'), e %>
RSpec interprets e in the link_to as is (i.e. an event object) instead of using its id to build the actual link as Rails does.
Changing e to e.id throws the error:
undefined method `model_name' for Fixnum:Class
in both the browser and RSpec.
Deleting this line make the tests pass, but of course, gives the wrong output.
Related
When I try to run this one specific test, it gives this error. When I try to run all tests, it fails for another reason. How can I run this one test without this error? It's clearly a valid URL.
C:\Users\Chloe\workspace\catalyst_research>rspec ./spec/features/ctdbase_purchase_spec.rb:28
http://ctdbase.org/ # output of puts
1) CTD Purchase Unregistered user visits the CTD product page directly and makes purchase
Failure/Error: visit url
Addressable::URI::InvalidURIError:
Invalid scheme format: 127.0.0.1
# ./spec/support/ctdbase_support.rb:8:in `visit_ctd'
# ./spec/features/ctdbase_purchase_spec.rb:11:in `block (2 levels) in <top (required)>'
Here is the code
ctdbase_support.rb
def visit_ctd(path)
url = "#{CTD_URL}#{path.starts_with?('/') ? '' : '/'}#{path}"
puts url
visit url # line 8
end
ctdbase_purchase_spec.rb
before do
delete_downloads
visit_ctd '/' # line 11
Here is the full backtrace:
C:\Users\Chloe\workspace\catalyst_research>rspec -b ./spec/features/ctdbase_purchase_spec.rb:28
...
1) CTD Purchase Unregistered user visits the CTD product page directly and makes purchase
Failure/Error: visit "#{CTD_URL}#{path.starts_with?('/') ? '' : '/'}#{path}"
Addressable::URI::InvalidURIError:
Invalid scheme format: 127.0.0.1
# C:/ruby24/lib/ruby/gems/2.4.0/gems/addressable-2.5.2/lib/addressable/uri.rb:874:in `scheme='
# C:/ruby24/lib/ruby/gems/2.4.0/gems/addressable-2.5.2/lib/addressable/uri.rb:799:in `block in initialize'
# C:/ruby24/lib/ruby/gems/2.4.0/gems/addressable-2.5.2/lib/addressable/uri.rb:2355:in `defer_validation'
# C:/ruby24/lib/ruby/gems/2.4.0/gems/addressable-2.5.2/lib/addressable/uri.rb:796:in `initialize'
# C:/ruby24/lib/ruby/gems/2.4.0/gems/addressable-2.5.2/lib/addressable/uri.rb:136:in `new'
# C:/ruby24/lib/ruby/gems/2.4.0/gems/addressable-2.5.2/lib/addressable/uri.rb:136:in `parse'
# C:/ruby24/lib/ruby/gems/2.4.0/gems/addressable-2.5.2/lib/addressable/template.rb:413:in `match'
# C:/ruby24/lib/ruby/gems/2.4.0/gems/webmock-3.0.1/lib/webmock/request_pattern.rb:151:in `block in matches?'
# C:/ruby24/lib/ruby/gems/2.4.0/gems/webmock-3.0.1/lib/webmock/request_pattern.rb:151:in `any?'
# C:/ruby24/lib/ruby/gems/2.4.0/gems/webmock-3.0.1/lib/webmock/request_pattern.rb:151:in `matches?'
# C:/ruby24/lib/ruby/gems/2.4.0/gems/webmock-3.0.1/lib/webmock/request_pattern.rb:33:in `matches?'
# C:/ruby24/lib/ruby/gems/2.4.0/gems/webmock-3.0.1/lib/webmock/stub_registry.rb:58:in `block in request_stub_for'
# C:/ruby24/lib/ruby/gems/2.4.0/gems/webmock-3.0.1/lib/webmock/stub_registry.rb:57:in `each'
# C:/ruby24/lib/ruby/gems/2.4.0/gems/webmock-3.0.1/lib/webmock/stub_registry.rb:57:in `detect'
# C:/ruby24/lib/ruby/gems/2.4.0/gems/webmock-3.0.1/lib/webmock/stub_registry.rb:57:in `request_stub_for'
# C:/ruby24/lib/ruby/gems/2.4.0/gems/webmock-3.0.1/lib/webmock/stub_registry.rb:50:in `response_for_request'
# C:/ruby24/lib/ruby/gems/2.4.0/gems/webmock-3.0.1/lib/webmock/http_lib_adapters/net_http.rb:79:in `request'
# C:/ruby24/lib/ruby/2.4.0/net/http.rb:1165:in `get'
# C:/ruby24/lib/ruby/gems/2.4.0/gems/capybara-2.16.1/lib/capybara/server.rb:82:in `block in responsive?'
# C:/ruby24/lib/ruby/gems/2.4.0/gems/webmock-3.0.1/lib/webmock/http_lib_adapters/net_http.rb:123:in `start_without_connect'
# C:/ruby24/lib/ruby/gems/2.4.0/gems/webmock-3.0.1/lib/webmock/http_lib_adapters/net_http.rb:150:in `start'
# C:/ruby24/lib/ruby/2.4.0/net/http.rb:608:in `start'
# C:/ruby24/lib/ruby/gems/2.4.0/gems/capybara-2.16.1/lib/capybara/server.rb:82:in `responsive?'
# C:/ruby24/lib/ruby/gems/2.4.0/gems/capybara-2.16.1/lib/capybara/server.rb:98:in `boot'
# C:/ruby24/lib/ruby/gems/2.4.0/gems/capybara-2.16.1/lib/capybara/session.rb:88:in `initialize'
# C:/ruby24/lib/ruby/gems/2.4.0/gems/capybara-2.16.1/lib/capybara.rb:304:in `new'
# C:/ruby24/lib/ruby/gems/2.4.0/gems/capybara-2.16.1/lib/capybara.rb:304:in `current_session'
# C:/ruby24/lib/ruby/gems/2.4.0/gems/capybara-2.16.1/lib/capybara/dsl.rb:45:in `page'
# C:/ruby24/lib/ruby/gems/2.4.0/gems/capybara-2.16.1/lib/capybara/dsl.rb:50:in `block (2 levels) in <module:DSL>'
# C:/ruby24/lib/ruby/gems/2.4.0/gems/rspec-rails-3.7.2/lib/rspec/rails/example/feature_example_group.rb:29:in `visit'
# ./spec/support/ctdbase_support.rb:6:in `visit_ctd'
# ./spec/features/ctdbase_purchase_spec.rb:11:in `block (2 levels) in <top (required)>'
# C:/ruby24/lib/ruby/gems/2.4.0/gems/rspec-core-3.7.0/lib/rspec/core/example.rb:447:in `instance_exec'
# C:/ruby24/lib/ruby/gems/2.4.0/gems/rspec-core-3.7.0/lib/rspec/core/example.rb:447:in `instance_exec'
# C:/ruby24/lib/ruby/gems/2.4.0/gems/rspec-core-3.7.0/lib/rspec/core/hooks.rb:350:in `run'
# C:/ruby24/lib/ruby/gems/2.4.0/gems/rspec-core-3.7.0/lib/rspec/core/hooks.rb:509:in `block in run_owned_hooks_for'
# C:/ruby24/lib/ruby/gems/2.4.0/gems/rspec-core-3.7.0/lib/rspec/core/hooks.rb:508:in `each'
# C:/ruby24/lib/ruby/gems/2.4.0/gems/rspec-core-3.7.0/lib/rspec/core/hooks.rb:508:in `run_owned_hooks_for'
# C:/ruby24/lib/ruby/gems/2.4.0/gems/rspec-core-3.7.0/lib/rspec/core/hooks.rb:595:in `block in run_example_hooks_for'
# C:/ruby24/lib/ruby/gems/2.4.0/gems/rspec-core-3.7.0/lib/rspec/core/hooks.rb:594:in `reverse_each'
# C:/ruby24/lib/ruby/gems/2.4.0/gems/rspec-core-3.7.0/lib/rspec/core/hooks.rb:594:in `run_example_hooks_for'
# C:/ruby24/lib/ruby/gems/2.4.0/gems/rspec-core-3.7.0/lib/rspec/core/hooks.rb:464:in `run'
# C:/ruby24/lib/ruby/gems/2.4.0/gems/rspec-core-3.7.0/lib/rspec/core/example.rb:494:in `run_before_example'
# C:/ruby24/lib/ruby/gems/2.4.0/gems/rspec-core-3.7.0/lib/rspec/core/example.rb:253:in `block in run'
# C:/ruby24/lib/ruby/gems/2.4.0/gems/rspec-core-3.7.0/lib/rspec/core/example.rb:500:in `block in with_around_and_singleton_context_hooks'
# C:/ruby24/lib/ruby/gems/2.4.0/gems/rspec-core-3.7.0/lib/rspec/core/example.rb:457:in `block in with_around_example_hooks'
# C:/ruby24/lib/ruby/gems/2.4.0/gems/rspec-core-3.7.0/lib/rspec/core/hooks.rb:466:in `block in run'
# C:/ruby24/lib/ruby/gems/2.4.0/gems/rspec-core-3.7.0/lib/rspec/core/hooks.rb:606:in `block in run_around_example_hooks_for'
# C:/ruby24/lib/ruby/gems/2.4.0/gems/rspec-core-3.7.0/lib/rspec/core/example.rb:342:in `call'
# C:/ruby24/lib/ruby/gems/2.4.0/gems/rspec-rails-3.7.2/lib/rspec/rails/adapters.rb:127:in `block (2 levels) in <module:MinitestLifecycleAdapter>'
# C:/ruby24/lib/ruby/gems/2.4.0/gems/rspec-core-3.7.0/lib/rspec/core/example.rb:447:in `instance_exec'
# C:/ruby24/lib/ruby/gems/2.4.0/gems/rspec-core-3.7.0/lib/rspec/core/example.rb:447:in `instance_exec'
# C:/ruby24/lib/ruby/gems/2.4.0/gems/rspec-core-3.7.0/lib/rspec/core/hooks.rb:375:in `execute_with'
# C:/ruby24/lib/ruby/gems/2.4.0/gems/rspec-core-3.7.0/lib/rspec/core/hooks.rb:608:in `block (2 levels) in run_around_example_hooks_for'
# C:/ruby24/lib/ruby/gems/2.4.0/gems/rspec-core-3.7.0/lib/rspec/core/example.rb:342:in `call'
# C:/ruby24/lib/ruby/gems/2.4.0/gems/rspec-core-3.7.0/lib/rspec/core/hooks.rb:609:in `run_around_example_hooks_for'
# C:/ruby24/lib/ruby/gems/2.4.0/gems/rspec-core-3.7.0/lib/rspec/core/hooks.rb:466:in `run'
# C:/ruby24/lib/ruby/gems/2.4.0/gems/rspec-core-3.7.0/lib/rspec/core/example.rb:457:in `with_around_example_hooks'
# C:/ruby24/lib/ruby/gems/2.4.0/gems/rspec-core-3.7.0/lib/rspec/core/example.rb:500:in `with_around_and_singleton_context_hooks'
# C:/ruby24/lib/ruby/gems/2.4.0/gems/rspec-core-3.7.0/lib/rspec/core/example.rb:251:in `run'
# C:/ruby24/lib/ruby/gems/2.4.0/gems/rspec-core-3.7.0/lib/rspec/core/example_group.rb:628:in `block in run_examples'
# C:/ruby24/lib/ruby/gems/2.4.0/gems/rspec-core-3.7.0/lib/rspec/core/example_group.rb:624:in `map'
# C:/ruby24/lib/ruby/gems/2.4.0/gems/rspec-core-3.7.0/lib/rspec/core/example_group.rb:624:in `run_examples'
# C:/ruby24/lib/ruby/gems/2.4.0/gems/rspec-core-3.7.0/lib/rspec/core/example_group.rb:590:in `run'
# C:/ruby24/lib/ruby/gems/2.4.0/gems/rspec-core-3.7.0/lib/rspec/core/runner.rb:118:in `block (3 levels) in run_specs'
# C:/ruby24/lib/ruby/gems/2.4.0/gems/rspec-core-3.7.0/lib/rspec/core/runner.rb:118:in `map'
# C:/ruby24/lib/ruby/gems/2.4.0/gems/rspec-core-3.7.0/lib/rspec/core/runner.rb:118:in `block (2 levels) in run_specs'
# C:/ruby24/lib/ruby/gems/2.4.0/gems/rspec-core-3.7.0/lib/rspec/core/configuration.rb:1896:in `with_suite_hooks'
# C:/ruby24/lib/ruby/gems/2.4.0/gems/rspec-core-3.7.0/lib/rspec/core/runner.rb:113:in `block in run_specs'
# C:/ruby24/lib/ruby/gems/2.4.0/gems/rspec-core-3.7.0/lib/rspec/core/reporter.rb:79:in `report'
# C:/ruby24/lib/ruby/gems/2.4.0/gems/rspec-core-3.7.0/lib/rspec/core/runner.rb:112:in `run_specs'
# C:/ruby24/lib/ruby/gems/2.4.0/gems/rspec-core-3.7.0/lib/rspec/core/runner.rb:87:in `run'
# C:/ruby24/lib/ruby/gems/2.4.0/gems/rspec-core-3.7.0/lib/rspec/core/runner.rb:71:in `run'
# C:/ruby24/lib/ruby/gems/2.4.0/gems/rspec-core-3.7.0/lib/rspec/core/runner.rb:45:in `invoke'
# C:/ruby24/lib/ruby/gems/2.4.0/gems/rspec-core-3.7.0/exe/rspec:4:in `<top (required)>'
# C:/ruby24/bin/rspec:23:in `load'
# C:/ruby24/bin/rspec:23:in `<main>'
Seems this project is using WebMock. This is the webmock.rb file
spec/config/webmock.rb
require 'webmock/rspec'
WebMock.allow_net_connect!
GOOGLE_STUB_WITHOUT_IP = {
"city" => "Mountain View",
"region" => "California",
"region_code" => "CA",
"country" => "US",
"country_name" => "United States",
"continent_code" => "NA",
"postal" => "94035",
"latitude" => 37.386,
"longitude" => -122.0838,
"timezone" => "America/Los_Angeles",
"utc_offset" => "-0700",
"country_calling_code" => "+1",
"currency" => "USD",
"languages" => "en-US,es-US,haw,fr",
"asn" => "AS15169",
"org" => "Google LLC"
}
uri_template =
Addressable::Template.new "https://ipapi.co/{ip_address}/json/"
WebMock.stub_request(:get, uri_template).to_return { |request|
{
# FIXME: This should dynamically return the ip address
# body: GOOGLE_STUB_WITHOUT_IP.merge('ip' => request.ip_address)
body: GOOGLE_STUB_WITHOUT_IP.merge('ip' => '8.8.8.8').to_json,
headers: { 'Content-Type' => 'application/json' }
}
}
You're running into a bug in the combination of WebMock and Addressable - https://github.com/bblimke/webmock/issues/489 and https://github.com/bblimke/webmock/pull/739. The problem is that WebMock ends up creating variations of URLs to compare to templates, and ends up doing something like 127.0.0.1:<some_port>/__identify when Capybara starts the app. The proper solution is for WebMock to fix the URLs it's sending to Addressable, but you may be able to workaround it temporarily by setting Capybara.server_host='localhost' which will cause the URL variation to end up as localhost:<some_port>/__identify and Addressable will see localhost as a possible scheme (which could be valid). It's a hack, but it may hold you over until WebMock gets fixed. Another option would be to stop using URL templates with WebMock and use regex instead.
I have the following setup in a rails app:
class Song < ActiveRecord::Base
after_commit :update_singable_index
belongs_to :singable, polymorphic: true
def update_singable_index
singable.update_index if singable.respond_to? :update_index
end
end
I've redacted the actual class heavily. Please let me know if any more information is needed.
But basically the Song model has a polymorphic association with a bunch of models. Some of them have elasticsearch indices and some of them don't. As a result some of them will accept the update_index message, while others will throw an NoMethodError: undefined method 'update_index'
The sing factory looks like this:
FactoryGirl.define do
factory :song do
album { create(:album) }
end
trait :for_updatable do
updatable_type 'Updatable'
association :singable, factory: :updatable
end
trait :for_unupdatable do
updatable_type 'Unupdatable'
association :singable, factory: :unupdatable
end
end
Then in the test I have the following setup:
RSpec.describe Song, type: :model do
describe '.update_index_of_updatable' do
it 'updates Updatable\'s index' do
song = create(:song, :for_updatable)
updatable = song.singable
expect(updatable).to receive(:update_index)
song.save
end
it 'doesn\'t attempt to update an Unupdatable\'s index' do
song = create(:song, :for_unupdatable)
unupdatable = song.singable
expect(unupdatable).not_to receive(:update_index)
song.save
end
end
end
The first test does indeed pass. The second test on the other hand fails
1) Song.update_singable_index doesn't attempt to update an Unupdatable's index
Failure/Error: update.save
(#<Unupdatable:0x0000000b39be98>).update_index(no args)
expected: 0 times with any arguments
received: 1 time
# ./app/models/song.rb:103:in `update_index_of_updatable'
# /usr/local/rvm/gems/ruby-2.2.3/gems/activesupport-4.2.3/lib/active_support/callbacks.rb:430:in `block in make_lambda'
# /usr/local/rvm/gems/ruby-2.2.3/gems/activesupport-4.2.3/lib/active_support/callbacks.rb:261:in `call'
# /usr/local/rvm/gems/ruby-2.2.3/gems/activesupport-4.2.3/lib/active_support/callbacks.rb:261:in `block in simple'
# /usr/local/rvm/gems/ruby-2.2.3/gems/activesupport-4.2.3/lib/active_support/callbacks.rb:504:in `call'
# /usr/local/rvm/gems/ruby-2.2.3/gems/activesupport-4.2.3/lib/active_support/callbacks.rb:504:in `block in call'
# /usr/local/rvm/gems/ruby-2.2.3/gems/activesupport-4.2.3/lib/active_support/callbacks.rb:504:in `each'
# /usr/local/rvm/gems/ruby-2.2.3/gems/activesupport-4.2.3/lib/active_support/callbacks.rb:504:in `call'
# /usr/local/rvm/gems/ruby-2.2.3/gems/activesupport-4.2.3/lib/active_support/callbacks.rb:88:in `run_callbacks'
# /usr/local/rvm/gems/ruby-2.2.3/gems/bugsnag-2.8.12/lib/bugsnag/rails/active_record_rescue.rb:8:in `run_callbacks'
# /usr/local/rvm/gems/ruby-2.2.3/gems/activerecord-4.2.3/lib/active_record/transactions.rb:314:in `committed!'
# /usr/local/rvm/gems/ruby-2.2.3/gems/activerecord-4.2.3/lib/active_record/connection_adapters/abstract/transaction.rb:89:in `commit_records'
# /usr/local/rvm/gems/ruby-2.2.3/gems/test_after_commit-0.4.1/lib/test_after_commit.rb:47:in `test_commit_records'
# /usr/local/rvm/gems/ruby-2.2.3/gems/test_after_commit-0.4.1/lib/test_after_commit.rb:23:in `block in transaction_with_transactional_fixtures'
# /usr/local/rvm/gems/ruby-2.2.3/gems/activerecord-4.2.3/lib/active_record/connection_adapters/abstract/database_statements.rb:213:in `block in transaction'
# /usr/local/rvm/gems/ruby-2.2.3/gems/activerecord-4.2.3/lib/active_record/connection_adapters/abstract/transaction.rb:184:in `within_new_transaction'
# /usr/local/rvm/gems/ruby-2.2.3/gems/activerecord-4.2.3/lib/active_record/connection_adapters/abstract/database_statements.rb:213:in `transaction'
# /usr/local/rvm/gems/ruby-2.2.3/gems/test_after_commit-0.4.1/lib/test_after_commit.rb:9:in `transaction_with_transactional_fixtures'
# /usr/local/rvm/gems/ruby-2.2.3/gems/activerecord-4.2.3/lib/active_record/transactions.rb:220:in `transaction'
# /usr/local/rvm/gems/ruby-2.2.3/gems/activerecord-4.2.3/lib/active_record/transactions.rb:348:in `with_transaction_returning_status'
# /usr/local/rvm/gems/ruby-2.2.3/gems/activerecord-4.2.3/lib/active_record/transactions.rb:286:in `block in save'
# /usr/local/rvm/gems/ruby-2.2.3/gems/activerecord-4.2.3/lib/active_record/transactions.rb:301:in `rollback_active_record_state!'
# /usr/local/rvm/gems/ruby-2.2.3/gems/activerecord-4.2.3/lib/active_record/transactions.rb:285:in `save'
# ./spec/models/song_spec.rb:141:in `block (3 levels) in <top (required)>'
# ./spec/rails_helper.rb:417:in `block (2 levels) in <top (required)>'
# ./spec/rails_helper.rb:192:in `block (2 levels) in <top (required)>'
# /usr/local/rvm/gems/ruby-2.2.3/gems/rspec-retry-0.4.2/lib/rspec/retry.rb:52:in `block (3 levels) in apply'
# /usr/local/rvm/gems/ruby-2.2.3/gems/rspec-retry-0.4.2/lib/rspec/retry.rb:43:in `times'
# /usr/local/rvm/gems/ruby-2.2.3/gems/rspec-retry-0.4.2/lib/rspec/retry.rb:43:in `block (2 levels) in apply'
You didn't show your :for_unupdatable trait, but it must be the flawed part here (meaning, a singable of a song with this trait still responds to update_index).
So, you should fix that.
Alternatively, provide a dummy implementation of that method for non-ES-capable models.
class ElasticSearchSingable
# your regular update_index
end
class SimpleSingable
def update_index
# do nothing here,
# purpose of this empty method is conformance to common API
end
end
I'm tying to test my Rails app with Rspec, but I'm getting a no implicit conversion of Symbol into Integer error without any apparent reason. Based on the traceback I get I think the problem is related to Mongo/Mongoid, however, I can't figure out what it is exactly. The code runs perfectly in production. The error happens only when testing.
Brief look at the model without the other methods:
class Card
include Mongoid::Document
field :front, type: String
field :back, type: String
field :level, type: Integer, default: 1
field :review_date, type: DateTime, default: DateTime.now
has_many :card_statistic, dependent: :destroy
belongs_to :topic
belongs_to :user
validates :front, :back, :level, presence: true
validates :topic, presence: { is: true, message: "must belong to a topic." }
validates :user, presence: { is: true, message: "must belong to a user." }
validates :level, numericality: { only_integer: true, greater_than: 0 }
end
One function in the model that triggers the error:
def self.reset(card)
card.update(level: 1)
end
The test code:
it "puts the given card in level 1" do
card = create(:card)
Card.correct card
card.reload
Card.correct card
card.reload
expect(card.level).to eq(3)
card.reset
card.reload
expect(card.level).to eq(1)
end
Then, the traceback of the error I get:
1) Card puts the given card in level 1
Failure/Error: Card.reset card
TypeError:
no implicit conversion of Symbol into Integer
# /home/huesitos/.rvm/gems/ruby-2.2.0/gems/mongo-2.0.4/lib/mongo/server_selector.rb:56:in `[]'
# /home/huesitos/.rvm/gems/ruby-2.2.0/gems/mongo-2.0.4/lib/mongo/server_selector.rb:56:in `get'
# /home/huesitos/.rvm/gems/ruby-2.2.0/gems/mongo-2.0.4/lib/mongo/client.rb:170:in `read_preference'
# /home/huesitos/.rvm/gems/ruby-2.2.0/gems/mongo-2.0.4/lib/mongo/collection/view/readable.rb:318:in `default_read'
# /home/huesitos/.rvm/gems/ruby-2.2.0/gems/mongo-2.0.4/lib/mongo/collection/view/readable.rb:251:in `read'
# /home/huesitos/.rvm/gems/ruby-2.2.0/gems/mongo-2.0.4/lib/mongo/collection/view/iterable.rb:38:in `each'
# /home/huesitos/.rvm/gems/ruby-2.2.0/bundler/gems/mongoid-c61547d5ed15/lib/mongoid/query_cache.rb:207:in `each'
# /home/huesitos/.rvm/gems/ruby-2.2.0/bundler/gems/mongoid-c61547d5ed15/lib/mongoid/contextual/mongo.rb:230:in `first'
# /home/huesitos/.rvm/gems/ruby-2.2.0/bundler/gems/mongoid-c61547d5ed15/lib/mongoid/contextual/mongo.rb:230:in `block (2 levels) in first'
# /home/huesitos/.rvm/gems/ruby-2.2.0/bundler/gems/mongoid-c61547d5ed15/lib/mongoid/contextual/mongo.rb:562:in `with_sorting'
# /home/huesitos/.rvm/gems/ruby-2.2.0/bundler/gems/mongoid-c61547d5ed15/lib/mongoid/contextual/mongo.rb:229:in `block in first'
# /home/huesitos/.rvm/gems/ruby-2.2.0/bundler/gems/mongoid-c61547d5ed15/lib/mongoid/contextual/mongo.rb:474:in `try_cache'
# /home/huesitos/.rvm/gems/ruby-2.2.0/bundler/gems/mongoid-c61547d5ed15/lib/mongoid/contextual/mongo.rb:228:in `first'
# /home/huesitos/.rvm/gems/ruby-2.2.0/bundler/gems/mongoid-c61547d5ed15/lib/mongoid/contextual.rb:20:in `first'
# /home/huesitos/.rvm/gems/ruby-2.2.0/bundler/gems/mongoid-c61547d5ed15/lib/mongoid/relations/builders/referenced/in.rb:20:in `build'
# /home/huesitos/.rvm/gems/ruby-2.2.0/bundler/gems/mongoid-c61547d5ed15/lib/mongoid/relations/accessors.rb:43:in `create_relation'
# /home/huesitos/.rvm/gems/ruby-2.2.0/bundler/gems/mongoid-c61547d5ed15/lib/mongoid/relations/accessors.rb:26:in `__build__'
# /home/huesitos/.rvm/gems/ruby-2.2.0/bundler/gems/mongoid-c61547d5ed15/lib/mongoid/relations/accessors.rb:104:in `block (2 levels) in get_relation'
# /home/huesitos/.rvm/gems/ruby-2.2.0/bundler/gems/mongoid-c61547d5ed15/lib/mongoid/threaded/lifecycle.rb:130:in `_loading'
# /home/huesitos/.rvm/gems/ruby-2.2.0/bundler/gems/mongoid-c61547d5ed15/lib/mongoid/relations/accessors.rb:100:in `block in get_relation'
# /home/huesitos/.rvm/gems/ruby-2.2.0/bundler/gems/mongoid-c61547d5ed15/lib/mongoid/threaded/lifecycle.rb:89:in `_building'
# /home/huesitos/.rvm/gems/ruby-2.2.0/bundler/gems/mongoid-c61547d5ed15/lib/mongoid/relations/accessors.rb:99:in `get_relation'
# /home/huesitos/.rvm/gems/ruby-2.2.0/bundler/gems/mongoid-c61547d5ed15/lib/mongoid/relations/accessors.rb:187:in `block in getter'
# /home/huesitos/.rvm/gems/ruby-2.2.0/bundler/gems/mongoid-c61547d5ed15/lib/mongoid/validatable.rb:79:in `read_attribute_for_validation'
# /home/huesitos/.rvm/gems/ruby-2.2.0/gems/activemodel-4.2.0/lib/active_model/validator.rb:149:in `block in validate'
# /home/huesitos/.rvm/gems/ruby-2.2.0/gems/activemodel-4.2.0/lib/active_model/validator.rb:148:in `each'
# /home/huesitos/.rvm/gems/ruby-2.2.0/gems/activemodel-4.2.0/lib/active_model/validator.rb:148:in `validate'
# /home/huesitos/.rvm/gems/ruby-2.2.0/gems/activesupport-4.2.0/lib/active_support/callbacks.rb:450:in `public_send'
# /home/huesitos/.rvm/gems/ruby-2.2.0/gems/activesupport-4.2.0/lib/active_support/callbacks.rb:450:in `block in make_lambda'
# /home/huesitos/.rvm/gems/ruby-2.2.0/gems/activesupport-4.2.0/lib/active_support/callbacks.rb:189:in `call'
# /home/huesitos/.rvm/gems/ruby-2.2.0/gems/activesupport-4.2.0/lib/active_support/callbacks.rb:189:in `block in simple'
# /home/huesitos/.rvm/gems/ruby-2.2.0/gems/activesupport-4.2.0/lib/active_support/callbacks.rb:190:in `call'
# /home/huesitos/.rvm/gems/ruby-2.2.0/gems/activesupport-4.2.0/lib/active_support/callbacks.rb:190:in `block in simple'
# /home/huesitos/.rvm/gems/ruby-2.2.0/gems/activesupport-4.2.0/lib/active_support/callbacks.rb:190:in `call'
# /home/huesitos/.rvm/gems/ruby-2.2.0/gems/activesupport-4.2.0/lib/active_support/callbacks.rb:190:in `block in simple'
# /home/huesitos/.rvm/gems/ruby-2.2.0/gems/activesupport-4.2.0/lib/active_support/callbacks.rb:92:in `call'
# /home/huesitos/.rvm/gems/ruby-2.2.0/gems/activesupport-4.2.0/lib/active_support/callbacks.rb:92:in `_run_callbacks'
# /home/huesitos/.rvm/gems/ruby-2.2.0/gems/activesupport-4.2.0/lib/active_support/callbacks.rb:734:in `_run_validate_callbacks'
# /home/huesitos/.rvm/gems/ruby-2.2.0/gems/activemodel-4.2.0/lib/active_model/validations.rb:395:in `run_validations!'
# /home/huesitos/.rvm/gems/ruby-2.2.0/gems/activemodel-4.2.0/lib/active_model/validations/callbacks.rb:113:in `block in run_validations!'
# /home/huesitos/.rvm/gems/ruby-2.2.0/gems/activesupport-4.2.0/lib/active_support/callbacks.rb:88:in `call'
# /home/huesitos/.rvm/gems/ruby-2.2.0/gems/activesupport-4.2.0/lib/active_support/callbacks.rb:88:in `_run_callbacks'
# /home/huesitos/.rvm/gems/ruby-2.2.0/gems/activesupport-4.2.0/lib/active_support/callbacks.rb:734:in `_run_validation_callbacks'
# /home/huesitos/.rvm/gems/ruby-2.2.0/gems/activemodel-4.2.0/lib/active_model/validations/callbacks.rb:113:in `run_validations!'
# /home/huesitos/.rvm/gems/ruby-2.2.0/gems/activemodel-4.2.0/lib/active_model/validations.rb:334:in `valid?'
# /home/huesitos/.rvm/gems/ruby-2.2.0/bundler/gems/mongoid-c61547d5ed15/lib/mongoid/validatable.rb:97:in `valid?'
# /home/huesitos/.rvm/gems/ruby-2.2.0/gems/activemodel-4.2.0/lib/active_model/validations.rb:371:in `invalid?'
# /home/huesitos/.rvm/gems/ruby-2.2.0/bundler/gems/mongoid-c61547d5ed15/lib/mongoid/persistable/updatable.rb:114:in `prepare_update'
# /home/huesitos/.rvm/gems/ruby-2.2.0/bundler/gems/mongoid-c61547d5ed15/lib/mongoid/persistable/updatable.rb:139:in `update_document'
# /home/huesitos/.rvm/gems/ruby-2.2.0/bundler/gems/mongoid-c61547d5ed15/lib/mongoid/persistable/savable.rb:25:in `save'
# /home/huesitos/.rvm/gems/ruby-2.2.0/bundler/gems/mongoid-c61547d5ed15/lib/mongoid/persistable/updatable.rb:52:in `update'
# ./app/models/card.rb:57:in `reset'
# ./spec/models/card_spec.rb:32:in `block (2 levels) in <top (required)>'
The error is also triggered when testing the controllers. Even doing a get :index throws the error. Thanks in advance for your help.
Apparently, there where some issues with the very last builds of Mongoid and I was pulling the code directly from the repo without specifying a stable version. So the solution was to either downgrade the version to 4.0 or use the 5.0 beta build explicitly.
Either
gem 'mongoid', '~> 5.0.0.beta', github: 'mongoid/mongoid'
or
gem 'mongoid', '~> 4'
instead of
gem 'mongoid', '~> 5', github: 'mongoid/mongoid'
Created a simple rake task to find Bikes for Sale in the USA. It works when i want to extract heading details but when I add other parameters like location, price , etc and save it to the Posts db I get error Can't cast Hash to string
Neils-MacBook-Pro-2:bike_scraper neilpatel$ rake scraper:scrape
rake aborted!
TypeError: can't cast Hash to string
/usr/local/rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.1.7/lib/active_record/connection_adapters/abstract/quoting.rb:76:in `type_cast'
/usr/local/rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.1.7/lib/active_record/connection_adapters/sqlite3_adapter.rb:261:in `type_cast'
/usr/local/rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.1.7/lib/active_record/connection_adapters/sqlite3_adapter.rb:295:in `block in exec_query'
/usr/local/rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.1.7/lib/active_record/connection_adapters/sqlite3_adapter.rb:294:in `map'
/usr/local/rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.1.7/lib/active_record/connection_adapters/sqlite3_adapter.rb:294:in `exec_query'
/usr/local/rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.1.7/lib/active_record/connection_adapters/abstract/database_statements.rb:68:in `exec_insert'
/usr/local/rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.1.7/lib/active_record/connection_adapters/abstract/database_statements.rb:95:in `insert'
/usr/local/rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.1.7/lib/active_record/connection_adapters/abstract/query_cache.rb:14:in `insert'
/usr/local/rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.1.7/lib/active_record/relation.rb:64:in `insert'
/usr/local/rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.1.7/lib/active_record/persistence.rb:503:in `_create_record'
/usr/local/rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.1.7/lib/active_record/attribute_methods/dirty.rb:87:in `_create_record'
/usr/local/rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.1.7/lib/active_record/callbacks.rb:306:in `block in _create_record'
/usr/local/rvm/gems/ruby-2.0.0-p247/gems/activesupport-4.1.7/lib/active_support/callbacks.rb:82:in `run_callbacks'
/usr/local/rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.1.7/lib/active_record/callbacks.rb:306:in `_create_record'
/usr/local/rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.1.7/lib/active_record/timestamp.rb:57:in `_create_record'
/usr/local/rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.1.7/lib/active_record/persistence.rb:483:in `create_or_update'
/usr/local/rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.1.7/lib/active_record/callbacks.rb:302:in `block in create_or_update'
/usr/local/rvm/gems/ruby-2.0.0-p247/gems/activesupport-4.1.7/lib/active_support/callbacks.rb:82:in `run_callbacks'
/usr/local/rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.1.7/lib/active_record/callbacks.rb:302:in `create_or_update'
/usr/local/rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.1.7/lib/active_record/persistence.rb:103:in `save'
/usr/local/rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.1.7/lib/active_record/validations.rb:51:in `save'
/usr/local/rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.1.7/lib/active_record/attribute_methods/dirty.rb:21:in `save'
/usr/local/rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.1.7/lib/active_record/transactions.rb:268:in `block (2 levels) in save'
/usr/local/rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.1.7/lib/active_record/transactions.rb:329:in `block in with_transaction_returning_status'
/usr/local/rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.1.7/lib/active_record/connection_adapters/abstract/database_statements.rb:201:in `block in transaction'
/usr/local/rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.1.7/lib/active_record/connection_adapters/abstract/database_statements.rb:209:in `within_new_transaction'
/usr/local/rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.1.7/lib/active_record/connection_adapters/abstract/database_statements.rb:201:in `transaction'
/usr/local/rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.1.7/lib/active_record/transactions.rb:208:in `transaction'
/usr/local/rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.1.7/lib/active_record/transactions.rb:326:in `with_transaction_returning_status'
/usr/local/rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.1.7/lib/active_record/transactions.rb:268:in `block in save'
/usr/local/rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.1.7/lib/active_record/transactions.rb:283:in `rollback_active_record_state!'
/usr/local/rvm/gems/ruby-2.0.0-p247/gems/activerecord-4.1.7/lib/active_record/transactions.rb:267:in `save'
/Users/neilpatel/Desktop/Rails/bike_scraper/lib/tasks/scraper.rake:53:in `block (3 levels) in <top (required)>'
/Users/neilpatel/Desktop/Rails/bike_scraper/lib/tasks/scraper.rake:38:in `each'
/Users/neilpatel/Desktop/Rails/bike_scraper/lib/tasks/scraper.rake:38:in `block (2 levels) in <top (required)>'
Tasks: TOP => scraper:scrape
(See full trace by running task with --trace)
scraper.rake
namespace :scraper do
desc "Fetch Craigslist posts from 3Taps"
task scrape: :environment do
require 'open-uri' #ruby gem
require 'JSON'
# Set API token and URL
auth_token = "fadddddddd4a23a1cc86e0d"
polling_url = "http://polling.3taps.com/poll"
# Specify request parameters
params = {
auth_token: auth_token,
anchor: 2109915749,
source: "CRAIG",
category_group: "SSSS",
category: "SBIK",
'location.country' => "USA",
retvals: "location,external_url,heading,body,timestamp,price,images,annotations"
}
# Prepare API request
uri = URI.parse(polling_url)
uri.query = URI.encode_www_form(params)
# Submit request -hitting enter
result = JSON.parse(open(uri).read)
# Display results to screen (put string - display information)
#puts result["postings"].second["location"]
#store results in database
# Create new Post
result["postings"].each do |posting|
# Create new Post
#post = Post.new
#post.heading = posting["heading"]
#post.body = posting["body"]
#post.price = posting["price"]
#post.state = posting["location"]
#post.external_url = posting["external_url"]
#post.timestamp = posting["timestamp"]
# Save Post
#post.save
end
end
desc "TODO"
task destroy_all_posts: :environment do
end
end
Posts.db
class CreatePosts < ActiveRecord::Migration
def change
create_table :posts do |t|
t.string :heading
t.text :body
t.decimal :price
t.string :state
t.string :external_url
t.string :timestamp
t.timestamps
end
end
end
The error is coming from your #post.save line, so one of those fields that came back in posting is a Hash, not a String as you expect. It won't be price, so just check all the others by putsing them.
Update
I just ran your code, it's posting["location"] that's a Hash
Further Update
Looks like you just want the state out of that hash, ie.
#post.state = posting["location"]["state"]
I can do a very simple integration test using capybara to test that a page renders correctly. Unfortunately, the tests can pass even if there is an error, if the test happens to find the required content in the error page.
Example test:
scenario 'goes to "new booking" page' do
visit '/bookings/new'
expect(page).to have_content('Driver')
end
Here's an example of the output for a failed test, that only happened to fail because the tested string was not in the error page:
Failure/Error: expect(page).to have_content('Invited')
expected to find text "Invited" in "TypeError at /bookings/86703360-4d92-4b79-bdfd-bbfbf843143e =========================================================== > no implicit conversion of nil into String app/models/vehicle.rb, line 11 ------------------------------ ``` ruby 6 has_many :drivers, through: :workables 7 8 has_and_belongs_to_many :job_types 9 10 def name_with_user > 11 name + \" \" + name_of_user 12 end 13 14 def name_with_user_and_jobs 15 name + \" \" + name_of_user + \" jobs\" 16 end ``` App backtrace ------------- - app/models/vehicle.rb:11:in `name_with_user' - app/views/bookings/show.html.erb:17:in `_app_views_bookings_show_html_erb__3294924100850065400_70296497966640' - app/controllers/bookings_controller.rb:115:in `show' - spec/features/booking_spec.rb:18:in `block (2 levels) in ' Full backtrace -------------- - app/models/vehicle.rb:11:in `name_with_user' - app/views/bookings/show.html.erb:17:in `_app_views_bookings_show_html_erb__3294924100850065400_70296497966640' - actionpack (4.0.8) lib/action_view/template.rb:143:in `block in render' - activesupport (4.0.8) lib/active_support/notifications.rb:161:in `instrument' - actionpack (4.0.8) lib/action_view/template.rb:141:in `render' - actionpack (4.0.8) lib/action_view/renderer/template_renderer.rb:49:in `block (2 levels) in render_template' - actionpack (4.0.8) lib/action_view/renderer/abstract_renderer.rb:38:in `block in instrument' - activesupport (4.0.8) lib/active_support/notifications.rb:159:in `block in instrument'
You can wrap the text in span/div and test for the text in selector, for eg:
have_css("#status", text: "Invited") #not tested
http://www.rubydoc.info/gems/capybara/0.4.0/Capybara/Node/Matchers
I think you may want to use within. Example:
within('.some-css-class') { expect(page).to have_content 'some content' }
This way you can narrow your search to a particular div or anything else using css selectors.