Sidekiq tries to run in test environment - ruby-on-rails

I have a very strange issue: Sidekiq tries to establish connection to Redis in test environment.
I'm using sidekiq 3.2.2 with rspec-sidekiq 2.0.0.beta on rails 4.1.4 with ruby 2.1.2
Here is my test
require 'rails_helper'
describe FetchWorker::FetchWorker do
it 'runs in fetch queue' do
expect(subject).to be_processed_in :fetch
end
it 'not retry when it fails' do
expect(subject).to be_retryable false
end
it 'enqueues the new job' do
FactoryGirl.create(:history, running: false)
FetchWorker::FetchWorker.perform_async 'FetchWorker', true
expect(FetchWorker::FetchWorker).to have_enqueued_job('FetchWorker', true)
end # enqueue job
describe '#perform' do
let!(:settleddate) { (Time.now - 2.minutes).iso8601(10) }
let!(:lock_date) { (Time.now - 5.minutes).iso8601(10) }
context 'success' do
before do
FactoryGirl.create(:history, running: false, NEW_TRANSACTIONS: lock_date)
FactoryGirl.create(:abp_transaction, settleddate: settleddate, settledyn: 1, transactiontypeid: 1, accountid: 10966)
end
it 'perform worker' do
user = FactoryGirl.build_stubbed(:user, account_id: 10966)
worker = FetchWorker::FetchWorker.new
worker.perform
expect(user.transactions.count).to eq(1)
end # it perform worker
end # context success
context 'fail' do
it 'perform worker' do
FactoryGirl.create(:history, running: false, NEW_TRANSACTIONS: lock_date)
FactoryGirl.create(:abp_transaction, settleddate: settleddate, settledyn: 1, transactiontypeid: 1, accountid: 10966)
user = FactoryGirl.build_stubbed(:user, account_id: 10966)
worker = FetchWorker::FetchWorker.new
allow(worker).to receive(:process_transactions).and_raise('some error')
expect{ worker.perform }.to raise_error
end # it perform worker
end # context fail
end # describe perform
end # describe FetchWorker
If I try to run bundle exec rspec with working redis, I'm getting pretty nice output:
retgoat#ubuntu:/media/sf_offside/agent_system$ bundle exec rspec
[rspec-sidekiq] WARNING! Sidekiq will *NOT* process jobs in this environment. See https://github.com/philostler/rspec-sidekiq/wiki/FAQ-&-Troubleshooting
2014-08-22T11:35:52.377Z 5140 TID-4ehb4 INFO: Fetching transactions from 2014-08- 22T18:30:52.3447179170+07:00.
2014-08-22T11:35:52.394Z 5140 TID-4ehb4 INFO: Lock released.
2014-08-22T11:35:52.451Z 5140 TID-4ehb4 INFO: Fetching transactions from 2014-08- 22T18:30:52.4160675140+07:00.
Finished in 29.88 seconds (files took 17.24 seconds to load)
119 examples, 0 failures
Coverage report generated for RSpec to /media/sf_offside/agent_system/coverage. 872 / 873 LOC (99.89%) covered.
But if I try to run tests with stopped redis I'm getting this:
retgoat#ubuntu:/media/sf_offside/agent_system$ bundle exec rspec
Coverage report generated for RSpec to /media/sf_offside/agent_system/coverage. 377 / 855 LOC (44.09%) covered.
/home/retgoat/.rvm/gems/ruby-2.1.2/gems/redis-3.1.0/lib/redis/client.rb:309:in `rescue in establish_connection': Error connecting to Redis on 127.0.0.1:6379 (ECONNREFUSED) (Redis::CannotConnectError)
from /home/retgoat/.rvm/gems/ruby-2.1.2/gems/redis-3.1.0/lib/redis/client.rb:304:in `establish_connection'
from /home/retgoat/.rvm/gems/ruby-2.1.2/gems/redis-3.1.0/lib/redis/client.rb:85:in `block in connect'
from /home/retgoat/.rvm/gems/ruby-2.1.2/gems/redis-3.1.0/lib/redis/client.rb:266:in `with_reconnect'
from /home/retgoat/.rvm/gems/ruby-2.1.2/gems/redis-3.1.0/lib/redis/client.rb:84:in `connect'
from /home/retgoat/.rvm/gems/ruby-2.1.2/gems/redis-3.1.0/lib/redis/client.rb:326:in `ensure_connected'
from /home/retgoat/.rvm/gems/ruby-2.1.2/gems/redis-3.1.0/lib/redis/client.rb:197:in `block in process'
from /home/retgoat/.rvm/gems/ruby-2.1.2/gems/redis-3.1.0/lib/redis/client.rb:279:in `logging'
from /home/retgoat/.rvm/gems/ruby-2.1.2/gems/redis-3.1.0/lib/redis/client.rb:196:in `process'
from /home/retgoat/.rvm/gems/ruby-2.1.2/gems/redis-3.1.0/lib/redis/client.rb:102:in `call'
from /home/retgoat/.rvm/gems/ruby-2.1.2/gems/redis-3.1.0/lib/redis.rb:784:in `block in get'
from /home/retgoat/.rvm/gems/ruby-2.1.2/gems/redis-3.1.0/lib/redis.rb:37:in `block in synchronize'
from /home/retgoat/.rvm/rubies/ruby-2.1.2/lib/ruby/2.1.0/monitor.rb:211:in `mon_synchronize'
from /home/retgoat/.rvm/gems/ruby-2.1.2/gems/redis-3.1.0/lib/redis.rb:37:in `synchronize'
from /home/retgoat/.rvm/gems/ruby-2.1.2/gems/redis-3.1.0/lib/redis.rb:783:in `get'
from /home/retgoat/.rvm/gems/ruby-2.1.2/gems/sidetiq- 0.6.1/lib/sidetiq/schedulable.rb:65:in `get_schedulable_key'
from /home/retgoat/.rvm/gems/ruby-2.1.2/gems/sidetiq-0.6.1/lib/sidetiq/schedulable.rb:45:in `recurrence'
from /media/sf_offside/agent_system/app/workers/fetch_worker.rb:6:in `<class:FetchWorker>'
from /media/sf_offside/agent_system/app/workers/fetch_worker.rb:2:in `<module:FetchWorker>'
from /media/sf_offside/agent_system/app/workers/fetch_worker.rb:1:in `<top (required)>'
from /home/retgoat/.rvm/gems/ruby-2.1.2/gems/activesupport-4.1.4/lib/active_support/dependencies.rb:247:in `require'
from /home/retgoat/.rvm/gems/ruby-2.1.2/gems/activesupport-4.1.4/lib/active_support/dependencies.rb:247:in `block in require'
from /home/retgoat/.rvm/gems/ruby-2.1.2/gems/activesupport-4.1.4/lib/active_support/dependencies.rb:232:in `load_dependency'
from /home/retgoat/.rvm/gems/ruby-2.1.2/gems/activesupport-4.1.4/lib/active_support/dependencies.rb:247:in `require'
from /home/retgoat/.rvm/gems/ruby-2.1.2/gems/activesupport-4.1.4/lib/active_support/dependencies.rb:348:in `require_or_load'
from /home/retgoat/.rvm/gems/ruby-2.1.2/gems/activesupport-4.1.4/lib/active_support/dependencies.rb:480:in `load_missing_constant'
from /home/retgoat/.rvm/gems/ruby-2.1.2/gems/activesupport-4.1.4/lib/active_support/dependencies.rb:180:in `const_missing'
from /media/sf_offside/agent_system/spec/workers/fetch_worker_spec.rb:3:in `<top (required)>'
from /home/retgoat/.rvm/gems/ruby-2.1.2/gems/activesupport-4.1.4/lib/active_support/dependencies.rb:241:in `load'
from /home/retgoat/.rvm/gems/ruby-2.1.2/gems/activesupport-4.1.4/lib/active_support/dependencies.rb:241:in `block in load'
from /home/retgoat/.rvm/gems/ruby-2.1.2/gems/activesupport-4.1.4/lib/active_support/dependencies.rb:232:in `load_dependency'
from /home/retgoat/.rvm/gems/ruby-2.1.2/gems/activesupport-4.1.4/lib/active_support/dependencies.rb:241:in `load'
from /home/retgoat/.rvm/gems/ruby-2.1.2/gems/rspec-core-3.0.4/lib/rspec/core/configuration.rb:1058:in `block in load_spec_files'
from /home/retgoat/.rvm/gems/ruby-2.1.2/gems/rspec-core-3.0.4/lib/rspec/core/configuration.rb:1058:in `each'
from /home/retgoat/.rvm/gems/ruby-2.1.2/gems/rspec-core-3.0.4/lib/rspec/core/configuration.rb:1058:in `load_spec_files'
from /home/retgoat/.rvm/gems/ruby-2.1.2/gems/rspec-core-3.0.4/lib/rspec/core/runner.rb:97:in `setup'
from /home/retgoat/.rvm/gems/ruby-2.1.2/gems/rspec-core-3.0.4/lib/rspec/core/runner.rb:85:in `run'
from /home/retgoat/.rvm/gems/ruby-2.1.2/gems/rspec-core-3.0.4/lib/rspec/core/runner.rb:70:in `run'
from /home/retgoat/.rvm/gems/ruby-2.1.2/gems/rspec-core- 3.0.4/lib/rspec/core/runner.rb:38:in `invoke'
from /home/retgoat/.rvm/gems/ruby-2.1.2/gems/rspec-core-3.0.4/exe/rspec:4:in `<top (required)>'
from /home/retgoat/.rvm/gems/ruby-2.1.2/bin/rspec:23:in `load'
from /home/retgoat/.rvm/gems/ruby-2.1.2/bin/rspec:23:in `<main>'
from /home/retgoat/.rvm/gems/ruby-2.1.2/bin/ruby_executable_hooks:15:in `eval'
from /home/retgoat/.rvm/gems/ruby-2.1.2/bin/ruby_executable_hooks:15:in `<main>'
I just have no idea why it happens.
I'm also opened a new issue in sidekiq on Github https://github.com/mperham/sidekiq/issues/1907
Could you please help with that?
Many thanks.
UPDATE
As #infused mentioned there is a bug: https://github.com/tobiassvn/sidetiq/issues/103
Here is the workaround:
recurrence { minutely(5) } unless Rails.env.test?
Worked like a charm! :)
Many thanks

The problem is that you are using sidetiq which has a bug which causes it to try to connect when it shouldn't.

Related

Update crontab (Whenever gem)

By executing the whenever --update-crontab command, the following error returns:
/usr/lib/ruby/vendor_ruby/whenever/cron.rb:132:in `parse_as_string': Couldn't parse: 31557600 (ArgumentError)
from /usr/lib/ruby/vendor_ruby/whenever/cron.rb:112:in `parse_time'
from /usr/lib/ruby/vendor_ruby/whenever/cron.rb:50:in `time_in_cron_syntax'
from /usr/lib/ruby/vendor_ruby/whenever/cron.rb:42:in `output'
from /usr/lib/ruby/vendor_ruby/whenever/cron.rb:36:in `block (2 levels) in output'
from /usr/lib/ruby/vendor_ruby/whenever/cron.rb:35:in `each'
from /usr/lib/ruby/vendor_ruby/whenever/cron.rb:35:in `block in output'
from /usr/lib/ruby/vendor_ruby/whenever/cron.rb:34:in `each'
from /usr/lib/ruby/vendor_ruby/whenever/cron.rb:34:in `output'
from /usr/lib/ruby/vendor_ruby/whenever/job_list.rb:151:in `block (2 levels) in cron_jobs'
from /usr/lib/ruby/vendor_ruby/whenever/job_list.rb:147:in `each'
from /usr/lib/ruby/vendor_ruby/whenever/job_list.rb:147:in `block in cron_jobs'
from /usr/lib/ruby/vendor_ruby/whenever/job_list.rb:146:in `each'
from /usr/lib/ruby/vendor_ruby/whenever/job_list.rb:146:in `cron_jobs'
from /usr/lib/ruby/vendor_ruby/whenever/job_list.rb:65:in `generate_cron_output'
from /usr/lib/ruby/vendor_ruby/whenever.rb:10:in `cron'
from /usr/lib/ruby/vendor_ruby/whenever/command_line.rb:54:in `whenever_cron'
from /usr/lib/ruby/vendor_ruby/whenever/command_line.rb:106:in `updated_crontab'
from /usr/lib/ruby/vendor_ruby/whenever/command_line.rb:35:in `run'
from /usr/lib/ruby/vendor_ruby/whenever/command_line.rb:6:in `execute'
from /usr/bin/whenever:41:in `<main>'
My schedule.rb file:
every 1.year do
rake "maintenances:renew"
end
Any ideas?
If you're having problems with 1.year method, you can use the two following alternatives.
1 - Shortcuts
every :year do
rake "maintenances:renew"
end
2 - Expressions
every '0 0 1 12 *' do
rake "maintenances:renew"
end
If you want to learn more about these options, check the official documentation.
To test expressions, I recommend crontab.guru

Configuration of Rspec terminal output

I am a beginner with Rspec, and I found out my terminal output like this.
It's really in a mess and hard to understand test result.
Unlike the output in official tutorial.
Should I install some tools or modify some configuration?
Update
zombie.rb
class Zombie
attr_accessor :name
def initialize
#name = 'Error_Ash'
end
end
zombie_spec.rb
require "spec_helper"
require "zombie"
#give Class
describe Zombie do
# example
it "is named Class_Ash"
zombie = Zombie.new
zombie.name.should == "Ash"
end
error msg
Coda:rspec_pra Coda$ rspec spec/lib/zombie_spec.rb --format doc
/Users/Coda/.rvm/gems/ruby-2.1.3#rails416/gems/rspec-support-3.4.1/lib/rspec/support.rb:87:in `block in <module:Support>': expected: "Ash" (RSpec::Expectations::ExpectationNotMetError)
got: "Error_Ash" (using ==)
from /Users/Coda/.rvm/gems/ruby-2.1.3#rails416/gems/rspec-support-3.4.1/lib/rspec/support.rb:96:in `call'
from /Users/Coda/.rvm/gems/ruby-2.1.3#rails416/gems/rspec-support-3.4.1/lib/rspec/support.rb:96:in `notify_failure'
from /Users/Coda/.rvm/gems/ruby-2.1.3#rails416/gems/rspec-expectations-3.4.0/lib/rspec/expectations/fail_with.rb:27:in `fail_with'
from /Users/Coda/.rvm/gems/ruby-2.1.3#rails416/gems/rspec-expectations-3.4.0/lib/rspec/matchers/built_in/operators.rb:71:in `fail_with_message'
from /Users/Coda/.rvm/gems/ruby-2.1.3#rails416/gems/rspec-expectations-3.4.0/lib/rspec/matchers/built_in/operators.rb:106:in `__delegate_operator'
from /Users/Coda/.rvm/gems/ruby-2.1.3#rails416/gems/rspec-expectations-3.4.0/lib/rspec/matchers/built_in/operators.rb:91:in `eval_match'
from /Users/Coda/.rvm/gems/ruby-2.1.3#rails416/gems/rspec-expectations-3.4.0/lib/rspec/matchers/built_in/operators.rb:51:in `block in use_custom_matcher_or_delegate'
from /Users/Coda/Desktop/code/ruby_pra/rspec_pra/spec/lib/zombie_spec.rb:11:in `block in <top (required)>'
from /Users/Coda/.rvm/gems/ruby-2.1.3#rails416/gems/rspec-core-3.4.4/lib/rspec/core/example_group.rb:385:in `module_exec'
from /Users/Coda/.rvm/gems/ruby-2.1.3#rails416/gems/rspec-core-3.4.4/lib/rspec/core/example_group.rb:385:in `subclass'
from /Users/Coda/.rvm/gems/ruby-2.1.3#rails416/gems/rspec-core-3.4.4/lib/rspec/core/example_group.rb:255:in `block in define_example_group_method'
from /Users/Coda/.rvm/gems/ruby-2.1.3#rails416/gems/rspec-core-3.4.4/lib/rspec/core/dsl.rb:43:in `block in expose_example_group_alias'
from /Users/Coda/.rvm/gems/ruby-2.1.3#rails416/gems/rspec-core-3.4.4/lib/rspec/core/dsl.rb:82:in `block (2 levels) in expose_example_group_alias_globally'
from /Users/Coda/Desktop/code/ruby_pra/rspec_pra/spec/lib/zombie_spec.rb:6:in `<top (required)>'
from /Users/Coda/.rvm/gems/ruby-2.1.3#rails416/gems/rspec-core-3.4.4/lib/rspec/core/configuration.rb:1361:in `load'
from /Users/Coda/.rvm/gems/ruby-2.1.3#rails416/gems/rspec-core-3.4.4/lib/rspec/core/configuration.rb:1361:in `block in load_spec_files'
from /Users/Coda/.rvm/gems/ruby-2.1.3#rails416/gems/rspec-core-3.4.4/lib/rspec/core/configuration.rb:1359:in `each'
from /Users/Coda/.rvm/gems/ruby-2.1.3#rails416/gems/rspec-core-3.4.4/lib/rspec/core/configuration.rb:1359:in `load_spec_files'
from /Users/Coda/.rvm/gems/ruby-2.1.3#rails416/gems/rspec-core-3.4.4/lib/rspec/core/runner.rb:106:in `setup'
from /Users/Coda/.rvm/gems/ruby-2.1.3#rails416/gems/rspec-core-3.4.4/lib/rspec/core/runner.rb:92:in `run'
from /Users/Coda/.rvm/gems/ruby-2.1.3#rails416/gems/rspec-core-3.4.4/lib/rspec/core/runner.rb:78:in `run'
from /Users/Coda/.rvm/gems/ruby-2.1.3#rails416/gems/rspec-core-3.4.4/lib/rspec/core/runner.rb:45:in `invoke'
from /Users/Coda/.rvm/gems/ruby-2.1.3#rails416/gems/rspec-core-3.4.4/exe/rspec:4:in `<top (required)>'
from /Users/Coda/.rvm/gems/ruby-2.1.3#rails416/bin/rspec:22:in `load'
from /Users/Coda/.rvm/gems/ruby-2.1.3#rails416/bin/rspec:22:in `<main>'
Coda:rspec_pra Coda$
If there is nothing in your spec_helper that somehow overrides the --format switch (which I do not even know if it's possible) then this should give you readable tests that pass:
zombie.rb exactly as you posted,
zombie_spec
require "spec_helper"
require "zombie"
describe Zombie do
it "is named Ash" do
zombie = Zombie.new
expect(zombie.name).to eq "Ash"
end
end
and command rspec spec/lib/zombie_spec.rb -f d which is short for --format documentation
EDIT*: Oh sorry, the Error you posted is indeed an RSpec error... Its just not formated, as you said x.x
try the --tty flag maybe?
rspec spec/lib/zombie_spec.rb --tty -f d

Error message when running "Rails S" & "$ bundle exec rspec spec/requests/static_pages_spec.rb"

This is what I have in my spec/requests/static_pages_spec.rb file
require 'spec_helper'
describe "Static pages" do
subject { page }
describe "Home page" do
before { visit root_path }
it { should have_content('Sample App') }
it { should have_title(full_title('')) }
it { should_not have_title('| Home') }
end
describe "Help page" do
before { visit help_path }
it { should have_content('Help') }
it { should have_title(full_title('Help')) }
end
describe "About page" do
before { visit about_path }
it { should have_content('About') }
it { should have_title(full_title('About Us')) }
end
describe "Contact page" do
before { visit contact_path }
it { should have_content('Contact') }
it { should have_title(full_title('Contact')) }
end
end
This is the error message I get when I run: bundle exec rspec spec/requests/static_pages_spec.rb
SECURITY WARNING: No secret option provided to Rack::Session::Cookie.
This poses a security threat. It is strongly recommended that you
provide a secret to prevent exploits that may be possible from crafted
cookies. This will not be supported in future versions of Rack, and
future versions will even invalidate your existing user cookies.
Called from: /usr/local/rvm/gems/ruby-2.0.0-p247/gems/actionpack-3.2.1/lib/action_dispatch/middleware/session/abstract_store.rb:28:in `initialize'.
Rack::File headers parameter replaces cache_control after Rack 1.5.
/usr/local/rvm/gems/ruby-2.0.0-p247/gems/actionpack-3.2.1/lib/action_dispatch/routing/mapper.rb:258:in `merge': no implicit conversion of String into Hash (TypeError)
from /usr/local/rvm/gems/ruby-2.0.0-p247/gems/actionpack-3.2.1/lib/action_dispatch/routing/mapper.rb:258:in `root'
from /usr/local/rvm/gems/ruby-2.0.0-p247/gems/actionpack-3.2.1/lib/action_dispatch/routing/mapper.rb:1315:in `root'
from /Users/StevenW/Documents/Code/rails_projects/sample_app/config/routes.rb:2:in `block in <top (required)>'
from /usr/local/rvm/gems/ruby-2.0.0-p247/gems/actionpack-3.2.1/lib/action_dispatch/routing/route_set.rb:272:in `instance_exec'
from /usr/local/rvm/gems/ruby-2.0.0-p247/gems/actionpack-3.2.1/lib/action_dispatch/routing/route_set.rb:272:in `eval_block'
from /usr/local/rvm/gems/ruby-2.0.0-p247/gems/actionpack-3.2.1/lib/action_dispatch/routing/route_set.rb:249:in `draw'
from /Users/StevenW/Documents/Code/rails_projects/sample_app/config/routes.rb:1:in `<top (required)>'
from /usr/local/rvm/gems/ruby-2.0.0-p247/gems/activesupport-3.2.1/lib/active_support/dependencies.rb:245:in `load'
from /usr/local/rvm/gems/ruby-2.0.0-p247/gems/activesupport-3.2.1/lib/active_support/dependencies.rb:245:in `block in load'
from /usr/local/rvm/gems/ruby-2.0.0-p247/gems/activesupport-3.2.1/lib/active_support/dependencies.rb:236:in `load_dependency'
from /usr/local/rvm/gems/ruby-2.0.0-p247/gems/activesupport-3.2.1/lib/active_support/dependencies.rb:245:in `load'
from /usr/local/rvm/gems/ruby-2.0.0-p247/gems/railties-3.2.1/lib/rails/application/routes_reloader.rb:40:in `block in load_paths'
from /usr/local/rvm/gems/ruby-2.0.0-p247/gems/railties-3.2.1/lib/rails/application/routes_reloader.rb:40:in `each'
from /usr/local/rvm/gems/ruby-2.0.0-p247/gems/railties-3.2.1/lib/rails/application/routes_reloader.rb:40:in `load_paths'
from /usr/local/rvm/gems/ruby-2.0.0-p247/gems/railties-3.2.1/lib/rails/application/routes_reloader.rb:16:in `reload!'
from /usr/local/rvm/gems/ruby-2.0.0-p247/gems/railties-3.2.1/lib/rails/application/routes_reloader.rb:26:in `block in updater'
from /usr/local/rvm/gems/ruby-2.0.0-p247/gems/activesupport-3.2.1/lib/active_support/file_update_checker.rb:78:in `call'
from /usr/local/rvm/gems/ruby-2.0.0-p247/gems/activesupport-3.2.1/lib/active_support/file_update_checker.rb:78:in `execute'
from /usr/local/rvm/gems/ruby-2.0.0-p247/gems/railties-3.2.1/lib/rails/application/routes_reloader.rb:27:in `updater'
from /usr/local/rvm/gems/ruby-2.0.0-p247/gems/railties-3.2.1/lib/rails/application/routes_reloader.rb:7:in `execute_if_updated'
from /usr/local/rvm/gems/ruby-2.0.0-p247/gems/railties-3.2.1/lib/rails/application/finisher.rb:66:in `block in <module:Finisher>'
from /usr/local/rvm/gems/ruby-2.0.0-p247/gems/railties-3.2.1/lib/rails/initializable.rb:30:in `instance_exec'
from /usr/local/rvm/gems/ruby-2.0.0-p247/gems/railties-3.2.1/lib/rails/initializable.rb:30:in `run'
from /usr/local/rvm/gems/ruby-2.0.0-p247/gems/railties-3.2.1/lib/rails/initializable.rb:55:in `block in run_initializers'
from /usr/local/rvm/gems/ruby-2.0.0-p247/gems/railties-3.2.1/lib/rails/initializable.rb:54:in `each'
from /usr/local/rvm/gems/ruby-2.0.0-p247/gems/railties-3.2.1/lib/rails/initializable.rb:54:in `run_initializers'
from /usr/local/rvm/gems/ruby-2.0.0-p247/gems/railties-3.2.1/lib/rails/application.rb:136:in `initialize!'
from /usr/local/rvm/gems/ruby-2.0.0-p247/gems/railties-3.2.1/lib/rails/railtie/configurable.rb:30:in `method_missing'
from /Users/StevenW/Documents/Code/rails_projects/sample_app/config/environment.rb:5:in `<top (required)>'
from /Users/StevenW/Documents/Code/rails_projects/sample_app/spec/spec_helper.rb:3:in `require'
from /Users/StevenW/Documents/Code/rails_projects/sample_app/spec/spec_helper.rb:3:in `<top (required)>'
from /Users/StevenW/Documents/Code/rails_projects/sample_app/spec/requests/static_pages_spec.rb:1:in `require'
from /Users/StevenW/Documents/Code/rails_projects/sample_app/spec/requests/static_pages_spec.rb:1:in `<top (required)>'
from /usr/local/rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.14.5/lib/rspec/core/configuration.rb:896:in `load'
from /usr/local/rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.14.5/lib/rspec/core/configuration.rb:896:in `block in load_spec_files'
from /usr/local/rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.14.5/lib/rspec/core/configuration.rb:896:in `each'
from /usr/local/rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.14.5/lib/rspec/core/configuration.rb:896:in `load_spec_files'
from /usr/local/rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.14.5/lib/rspec/core/command_line.rb:22:in `run'
from /usr/local/rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.14.5/lib/rspec/core/runner.rb:80:in `run'
from /usr/local/rvm/gems/ruby-2.0.0-p247/gems/rspec-core-2.14.5/lib/rspec/core/runner.rb:17:in `block in autorun'
I read in another post to avoid the Security Warning, however, I'm not sure what to do about the rest of the errors underneath. Your help is greatly appreciated.
The top line of the stack trace:
/usr/local/rvm/gems/ruby-2.0.0-p247/gems/actionpack-3.2.1
/lib/action_dispatch/routing/mapper.rb:258:in `merge':
no implicit conversion of String into Hash (TypeError)
This makes me think you have something invalid in your routes. Does anything work?
The root directive in routes.rb can receive either:
A string (path) - root 'pages#main'
A Hash (options) - root to: 'pages#main' (which is exactly like root {:to => 'pages#main'})
A string and a Hash (path and options) - root 'pages#main', some_other: option
Any other syntax will throw an exception such as the OP has seen.
Examples of such errors:
root 'pages#main', 'pages#index'
root 'pages#main', []
root 'pages#main', %w{ my_option: 'my_test' } # <= this is an array, not a hash...
I managed to solve the problem for myself. Not sure what exactly was wrong, but after upgrading to Rails 4.0 and Ruby 2.0 I started my app from scratch (not sure if this is a viable option for you or if you are working through a tutorial as well) and I'm no longer getting the error.
I know this isn't a detailed response or an explanation but if you just want to get things up and running I hope this helps!
I believe you are setting your routes incorrectly in your config/routes.rb file. Also are you using any gems, such as shoulda?
Here is a link on how to set up root paths in rails: http://guides.rubyonrails.org/routing.html#using-root

Rails Mongoid / MongoHD / Heroku error "need to login" when saving to database

I have an error that appears to be connected to these two links:
https://groups.google.com/forum/#!msg/mongoid/L6ESKP8QDpg/gmEyqejdAEEJ
https://github.com/mongoid/moped/pull/59
See console log below. I am running with Mongoid/MongoHQ, when I create an object (apparently any database object) I get this "need to login" error. Note however that the database object IS created and is in the database after the error.
Is there some way I can fix this myself, or perhaps it's something that MongoHQ needs to fix?
$ heroku run console
irb(main):004:0> Request.create!(:from_user => User.first, :to_user => User.first)
Moped::Errors::OperationFailure: The operation: #<Moped::Protocol::Command
#length=69
#request_id=7
#response_to=0
#op_code=2004
#flags=[]
#full_collection_name="admin.$cmd"
#skip=0
#limit=-1
#selector={:getlasterror=>1, :safe=>true}
#fields=nil>
failed with error "need to login"
from /app/vendor/bundle/ruby/1.9.1/gems/moped-1.2.0/lib/moped/node.rb:74:in `block in command'
from /app/vendor/bundle/ruby/1.9.1/gems/moped-1.2.0/lib/moped/node.rb:521:in `[]'
from /app/vendor/bundle/ruby/1.9.1/gems/moped-1.2.0/lib/moped/node.rb:521:in `block (3 levels) in flush'
from /app/vendor/bundle/ruby/1.9.1/gems/moped-1.2.0/lib/moped/node.rb:520:in `map'
from /app/vendor/bundle/ruby/1.9.1/gems/moped-1.2.0/lib/moped/node.rb:520:in `block (2 levels) in flush'
from /app/vendor/bundle/ruby/1.9.1/gems/moped-1.2.0/lib/moped/node.rb:123:in `ensure_connected'
from /app/vendor/bundle/ruby/1.9.1/gems/moped-1.2.0/lib/moped/node.rb:516:in `block in flush'
from /app/vendor/bundle/ruby/1.9.1/gems/moped-1.2.0/lib/moped/node.rb:531:in `logging'
from /app/vendor/bundle/ruby/1.9.1/gems/moped-1.2.0/lib/moped/node.rb:515:in `flush'
from /app/vendor/bundle/ruby/1.9.1/gems/moped-1.2.0/lib/moped/node.rb:286:in `pipeline'
from /app/vendor/bundle/ruby/1.9.1/gems/moped-1.2.0/lib/moped/session/context.rb:58:in `block in insert'
from /app/vendor/bundle/ruby/1.9.1/gems/moped-1.2.0/lib/moped/session/context.rb:109:in `block in with_node'
from /app/vendor/bundle/ruby/1.9.1/gems/moped-1.2.0/lib/moped/cluster.rb:150:in `block in with_primary'
from /app/vendor/bundle/ruby/1.9.1/gems/moped-1.2.0/lib/moped/node.rb:168:in `ensure_primary'
from /app/vendor/bundle/ruby/1.9.1/gems/moped-1.2.0/lib/moped/cluster.rb:149:in `with_primary'
from /app/vendor/bundle/ruby/1.9.1/gems/moped-1.2.0/lib/moped/session/context.rb:108:in `with_node'
... 13 levels...
from /app/vendor/bundle/ruby/1.9.1/gems/activesupport-3.2.8/lib/active_support/callbacks.rb:405:in `__run_callback'
from /app/vendor/bundle/ruby/1.9.1/gems/activesupport-3.2.8/lib/active_support/callbacks.rb:385:in `_run_save_callbacks'
from /app/vendor/bundle/ruby/1.9.1/gems/activesupport-3.2.8/lib/active_support/callbacks.rb:81:in `run_callbacks'
from /app/vendor/bundle/ruby/1.9.1/gems/mongoid-3.0.4/lib/mongoid/callbacks.rb:100:in `run_callbacks'
from /app/vendor/bundle/ruby/1.9.1/gems/mongoid-3.0.4/lib/mongoid/persistence/insertion.rb:23:in `prepare'
from /app/vendor/bundle/ruby/1.9.1/gems/mongoid-3.0.4/lib/mongoid/persistence/operations/insert.rb:26:in `persist'
from /app/vendor/bundle/ruby/1.9.1/gems/mongoid-3.0.4/lib/mongoid/persistence.rb:50:in `insert'
from /app/vendor/bundle/ruby/1.9.1/gems/mongoid-3.0.4/lib/mongoid/persistence.rb:251:in `block in create!'
from /app/vendor/bundle/ruby/1.9.1/gems/mongoid-3.0.4/lib/mongoid/threaded/lifecycle.rb:173:in `_creating'
from /app/vendor/bundle/ruby/1.9.1/gems/mongoid-3.0.4/lib/mongoid/persistence.rb:249:in `create!'
from (irb):4
from /app/vendor/bundle/ruby/1.9.1/gems/railties-3.2.8/lib/rails/commands/console.rb:47:in `start'
from /app/vendor/bundle/ruby/1.9.1/gems/railties-3.2.8/lib/rails/commands/console.rb:8:in `start'
from /app/vendor/bundle/ruby/1.9.1/gems/railties-3.2.8/lib/rails/commands.rb:41:in `<top (required)>'
from script/rails:6:in `require'
from script/rails:6:in `<main>'irb(main):005:0>
I am having the exact same problem. This is not exactly a solution but you can work around the problem by setting the 'safe' option in mongoid.yml to false. It would look similar to this:
production:
sessions:
default:
uri: <%= ENV['MONGOHQ_URL'] %>
options:
skip_version_check: true
safe: false
It turns out the problem is that MongoHQ upgraded their MongoDB and it introduced an incompatibility with mongoid's stable branch.
Chris Winslett fixed the bug and durran pulled it into mongoid master. But it was not included in the stable branch when I posted this stackoverflow.
Solutions:
I solved the problem by updating my gemfile to pull mongoid from github.
durran has now included it in stable-1.2.0 so you should get the fix if you gem update.

Ruby/Rails continuous automated testing with notifications and html output?

I'm a newbie in ruby/rails, currently doing the railstutorial book as a part of a university course.
I'm using ubuntu 11.04, ruby 1.9.2p290, and Rails 3.0.9, watchr (0.7), rspec (2.6.0), rspec-formatter-webkit (2.1.3) and spork (0.9.0.rc).
On github I came across this gem, and I thought it would be very nice to use it.
So basically what I'm trying to achieve is
continuous automated testing with notifications if finished, and also to generate the nice html output out of it.
So what I tried is that I wrote this watchr script:
require 'nokogiri'
require 'open-uri'
watch("spec/.*/*_spec.rb") do |match|
run_spec match[0]
end
watch("app/(.*/.*).rb") do |match|
run_spec %{spec/#{match[1]}_spec.rb}
end
def run_spec(file)
unless File.exist?(file)
puts "#{file} does not exist"
return
end
puts "Running #{file}"
result = `rspec -r rspec/core/formatters/webkit -f RSpec::Core::Formatters::WebKit #{file}`
File.open('out.html', 'w') do |f|
f.puts result
end
notify(result)
puts "DONE"
end
def send_notify title, msg, img, pri='low', time=5000
`notify-send -i #{img} -u #{pri} -t #{time} '#{msg}'`
end
def notify(result)
output = result
doc = Nokogiri::HTML(result)
doc.xpath('//div[#id = "summary"]').each do |node|
output = node.text.split.join(" ")
end
folder = "~/Pictures/autotest/"
if output =~ /([123456789]|[\d]{2,})\sfailed/
send_notify "FAIL:", "#{output}", folder+"rails_fail.png", 'critical', 20000
elsif output =~ /[1-9]\d*\spending?/
send_notify "PENDING:", "#{output}", folder+"rails_pending.png", 'normal', 15000
else
send_notify "PASS:", "#{output}", folder+"rails_ok.png"
end
end
handling the notifications, and generating the output while testing.
If I run this with watchr, everything is nice and working, however the tests are taking a long time of course.
The problem comes in here: if I try to use spork, to speed the tests up, I get a bunch of errors, starting with a LoadError:
Exception encountered: #<LoadError: no such file to load -- rspec/core/formatters/webkit>
backtrace:
/usr/local/ruby/lib/ruby/gems/1.9.1/gems/activesupport-3.0.9/lib/active_support/dependencies.rb:239:in `require'
/usr/local/ruby/lib/ruby/gems/1.9.1/gems/activesupport-3.0.9/lib/active_support/dependencies.rb:239:in `block in require'
/usr/local/ruby/lib/ruby/gems/1.9.1/gems/activesupport-3.0.9/lib/active_support/dependencies.rb:225:in `block in load_dependency'
/usr/local/ruby/lib/ruby/gems/1.9.1/gems/activesupport-3.0.9/lib/active_support/dependencies.rb:596:in `new_constants_in'
/usr/local/ruby/lib/ruby/gems/1.9.1/gems/activesupport-3.0.9/lib/active_support/dependencies.rb:225:in `load_dependency'
/usr/local/ruby/lib/ruby/gems/1.9.1/gems/activesupport-3.0.9/lib/active_support/dependencies.rb:239:in `require'
/usr/local/ruby/lib/ruby/gems/1.9.1/gems/rspec-core-2.6.4/lib/rspec/core/configuration.rb:247:in `block in requires='
/usr/local/ruby/lib/ruby/gems/1.9.1/gems/rspec-core-2.6.4/lib/rspec/core/configuration.rb:247:in `map'
/usr/local/ruby/lib/ruby/gems/1.9.1/gems/rspec-core-2.6.4/lib/rspec/core/configuration.rb:247:in `requires='
/usr/local/ruby/lib/ruby/gems/1.9.1/gems/rspec-core-2.6.4/lib/rspec/core/configuration_options.rb:21:in `block in configure'
/usr/local/ruby/lib/ruby/gems/1.9.1/gems/rspec-core-2.6.4/lib/rspec/core/configuration_options.rb:20:in `each'
/usr/local/ruby/lib/ruby/gems/1.9.1/gems/rspec-core-2.6.4/lib/rspec/core/configuration_options.rb:20:in `configure'
/usr/local/ruby/lib/ruby/gems/1.9.1/gems/rspec-core-2.6.4/lib/rspec/core/command_line.rb:17:in `run'
/usr/local/ruby/lib/ruby/gems/1.9.1/gems/rspec-core-2.6.4/lib/rspec/monkey/spork/test_framework/rspec.rb:5:in `run_tests'
/usr/local/ruby/lib/ruby/gems/1.9.1/gems/spork-0.9.0.rc/lib/spork/run_strategy/forking.rb:13:in `block in run'
/usr/local/ruby/lib/ruby/gems/1.9.1/gems/spork-0.9.0.rc/lib/spork/forker.rb:21:in `block in initialize'
/usr/local/ruby/lib/ruby/gems/1.9.1/gems/spork-0.9.0.rc/lib/spork/forker.rb:18:in `fork'
/usr/local/ruby/lib/ruby/gems/1.9.1/gems/spork-0.9.0.rc/lib/spork/forker.rb:18:in `initialize'
/usr/local/ruby/lib/ruby/gems/1.9.1/gems/spork-0.9.0.rc/lib/spork/run_strategy/forking.rb:9:in `new'
/usr/local/ruby/lib/ruby/gems/1.9.1/gems/spork-0.9.0.rc/lib/spork/run_strategy/forking.rb:9:in `run'
/usr/local/ruby/lib/ruby/gems/1.9.1/gems/spork-0.9.0.rc/lib/spork/server.rb:47:in `run'
/usr/local/ruby/lib/ruby/1.9.1/drb/drb.rb:1558:in `perform_without_block'
/usr/local/ruby/lib/ruby/1.9.1/drb/drb.rb:1518:in `perform'
/usr/local/ruby/lib/ruby/1.9.1/drb/drb.rb:1592:in `block (2 levels) in main_loop'
/usr/local/ruby/lib/ruby/1.9.1/drb/drb.rb:1588:in `loop'
/usr/local/ruby/lib/ruby/1.9.1/drb/drb.rb:1588:in `block in main_loop'
which of course don't really say anything to me, being a total newbie.
Is there a way to overcome this (or of course produce result in any other way)?
Any help is greatly appreciated :)
You might have more luck running the RSpec runner directly rather than shelling out, e.g.,
require 'rspec/core'
require 'rspec/core/formatters/webkit'
# ...then in #run_spec
puts "Running #{file}"
File.open( 'out.html', 'w' ) do |f|
argv = [ '-f', 'RSpec::Core::Formatters::WebKit', file ]
RSpec::Core::Runner.run( argv, f )
end
notify(result)
puts "DONE"
The ActiveSupport monkey(freedom)-patched 'Kernel.require' seems to be the thing that's different in the three scenarios you mentioned.
If you figure out why ActiveSupport can't find the library, I'd be happy to try to work around it in the WebKit formatter.

Resources