`<top (required)>': undefined method `“raf_ebooks' for main:Object (NoMethodError) - - ruby

When trying to run my twitter_ebooks bot using ruby run.rb I get this error:
Faraday::Builder is now Faraday::RackBuilder.
/Users/Rafi/Desktop/my_ebooks/bots.rb:14:in `<top (required)>': undefined method `“raf_ebooks' for main:Object (NoMethodError)
from run.rb:33:in `require_relative'
from run.rb:33:in `<main>'
Here is the git repo, of the bot I'm using, if that helps: https://github.com/mispy/twitter_ebooks
Here is what is inside the bots.rb file (I have removed the auth tokens and consumer key etc for security): http://pastebin.com/gYqcMYhM
What does this error mean and how can I fix it? Thanks.
EDIT: I tried running it again after restarting got pretty much the same error except instead of my_ebooks/bots.rb:14 it says my_ebooks/bots.rb:13
Faraday::Builder is now Faraday::RackBuilder.
/Users/Rafi/Desktop/my_ebooks/bots.rb:13:in `<top (required)>': undefined local variable or method `“raf_ebooks”' for main:Object (NameError)
from run.rb:33:in `require_relative'
from run.rb:33:in `<main>'

You have curly quotation marks that Ruby doesn't pick up. This happens when you use word processors that try to get fancy.
TWITTER_USERNAME = “raf_ebooks" # Ebooks account username
TEXT_MODEL_NAME = “raf_ebooks" # This should be the name of the text model
Notice the broken syntax highlighting. They should be "normal" straight quotes:
TWITTER_USERNAME = "raf_ebooks" # Ebooks account username
TEXT_MODEL_NAME = "raf_ebooks" # This should be the name of the text model
Much better.
Get yourself a decent editor. TextEdit is not suitable for coding. TextMate and Sublime Text are popular commercial editors. GitHub's Atom is a free editor, pretty nice too.

The opening quote of the TWITTER_USERNAME value isn't a "regular" quote, it's some sort of smart quote. Hence Ruby isn't recognising it as a string, and is getting rather confused.

Related

Using Rspec should receive

I am following a tutorial and have been getting some errors, I believe some of the language is outdated with Rspec and have messed around alot to try and fix this but have not been able to.
The error i get is
NoMethodError:
undefined method `should_recieve' for #<Panda:0x007f9cd45c6458>
# ./spec/zookeeper_spec.rb:9:in `block (2 levels) in <top (required)>'
The code in question is
describe Zookeeper do
it "should be able to feed the bamboo to the pandas" do
panda = Panda.new
panda.should_recieve(:eat).with(:bamboo)
Zookeeper.new.feed(food: :bamboo, to: panda)
end
I have realised that should_recieve should actually be should_receive and this has resolved the problem. I then replaced should_receive with and expectation and to receive
describe Zookeeper do
it "should be able to feed the bamboo to the pandas" do
panda = Panda.new
expect(panda).to receive(:eat).with(:bamboo)
Zookeeper.new.feed(food: :bamboo, to: panda)
end

Rspec tests not running because of initializer Yaml files

I'm integrating tests with Rspec into quite a large / developed app. When I run the test I'm getting very odd errors from some of my initializers.
For example, my carrierwave.rb loads a yaml file, when I run the test command I get:
carrierwave.rb:11:in `block in <top (required)>': undefined method `[]' for nil:NilClass
which is referring to the second line of code below:
fog_config = YAML::load_file(Rails.root.join 'config/fog.yml')[Rails.env.to_s]
config.fog_directory = fog_config['directory']
Momentarily if I remove that line of code in carrierwave, omniauth starts to complain too:
omniauth.rb:4:in `block in <top (required)>': undefined method `symbolize_keys' for nil:NilClass (NoMethodError)
Again, another yaml file:
fb_config = YAML::load_file(Rails.root.join 'config/fb_app_version.yml')[Rails.env].symbolize_keys
I'm guessing the solution is to change my yaml files or include something in the tests, but I'm not sure what. Any ideas would be most appreciated, thanks.
It looks like you need to include test entries in the Yaml files in question.
For example, your config/fog.yml probably looks something like this:
production:
directory: the_prod_directory
other_keys: ...
development:
directory: the_dev_directory
other_keys: ...
You need to add a test key:
test:
directory: the_test_dir
other_keys: appropriate values

RSpec shorter backtrace output for test failures

I'm using RSpec (latest Version, 2.12.2) to test a small Ruby class I'm working on. My problem is that when an RSpec test fails, the test output seems incredibly verbose, and shows a huge list of error messages, almost what seems to be a full backtrace. This means I have to scroll up to see the actual error message and the top of the trace.
I believe by default RSpec is supposed to do this but it doesn't seem to be doing it for me. For example, if I run rspec spec/my_spec.rb:132 (just run one test that's on L132), I get this output:
Failure/Error: #f.has_changed?("test").should be_true
expected: true value
got: false
# /Users/JackFranklin/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/rspec-expectations-2.12.1/lib/rspec/expectations/fail_with.rb:33:in `fail_with'
# /Users/JackFranklin/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/rspec-expectations-2.12.1/lib/rspec/expectations/handler.rb:33:in `handle_matcher'
# /Users/JackFranklin/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/rspec-expectations-2.12.1/lib/rspec/expectations/syntax.rb:53:in `should'
# /Users/JackFranklin/Dropbox/Sites/rubygems/filefixtures/spec/filefixtures_spec.rb:137:in `block (4 levels) in <top (required)>'
# /Users/JackFranklin/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/rspec-core-2.12.2/lib/rspec/core/example.rb:114:in `instance_eval'
# /Users/JackFranklin/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/rspec-core-2.12.2/lib/rspec/core/example.rb:114:in `block in run'
# /Users/JackFranklin/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/rspec-core-2.12.2/lib/rspec/core/example.rb:254:in `with_around_each_hooks'
# /Users/JackFranklin/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/rspec-core-2.12.2/lib/rspec/core/example.rb:111:in `run'
# /Users/JackFranklin/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/rspec-core-2.12.2/lib/rspec/core/example_group.rb:388:in `block in run_examples'
# /Users/JackFranklin/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/rspec-core-2.12.2/lib/rspec/core/example_group.rb:384:in `map'
# /Users/JackFranklin/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/rspec-core-2.12.2/lib/rspec/core/example_group.rb:384:in `run_examples'
# /Users/JackFranklin/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/rspec-core-2.12.2/lib/rspec/core/example_group.rb:369:in `run'
# /Users/JackFranklin/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/rspec-core-2.12.2/lib/rspec/core/example_group.rb:370:in `block in run'
# /Users/JackFranklin/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/rspec-core-2.12.2/lib/rspec/core/example_group.rb:370:in `map'
# /Users/JackFranklin/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/rspec-core-2.12.2/lib/rspec/core/example_group.rb:370:in `run'
# /Users/JackFranklin/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/rspec-core-2.12.2/lib/rspec/core/example_group.rb:370:in `block in run'
# /Users/JackFranklin/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/rspec-core-2.12.2/lib/rspec/core/example_group.rb:370:in `map'
# /Users/JackFranklin/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/rspec-core-2.12.2/lib/rspec/core/example_group.rb:370:in `run'
# /Users/JackFranklin/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/rspec-core-2.12.2/lib/rspec/core/command_line.rb:28:in `block (2 levels) in run'
# /Users/JackFranklin/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/rspec-core-2.12.2/lib/rspec/core/command_line.rb:28:in `map'
# /Users/JackFranklin/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/rspec-core-2.12.2/lib/rspec/core/command_line.rb:28:in `block in run'
# /Users/JackFranklin/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/rspec-core-2.12.2/lib/rspec/core/reporter.rb:34:in `report'
# /Users/JackFranklin/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/rspec-core-2.12.2/lib/rspec/core/command_line.rb:25:in `run'
# /Users/JackFranklin/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/rspec-core-2.12.2/lib/rspec/core/runner.rb:80:in `run'
# /Users/JackFranklin/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/rspec-core-2.12.2/lib/rspec/core/runner.rb:17:in `block in autorun'
Which, as you can see, is absolutely massive. I've not got any RSpec config files around that could be altering the configuration passed to RSpec.
Has anyone seen this before? I've tried Googling but searches have been fruitless.
Edit: I then set up config to make sure it was applying the default backtrace cleaning:
RSpec.configure do |config|
# RSpec automatically cleans stuff out of backtraces;
# sometimes this is annoying when trying to debug something e.g. a gem
config.backtrace_clean_patterns = [
/\/lib\d*\/ruby\//,
/bin\//,
/gems/,
/spec\/spec_helper\.rb/,
/lib\/rspec\/(core|expectations|matchers|mocks)/
]
end
But this doesn't make a difference. Looking at the output, it should be having most of it filtered out but it seems like the configuration isn't being applied?
Edit again:
In my config, I can even run cleaned_from_backtrace?(line) to see if a line should be cleaned. This returns true:
config.cleaned_from_backtrace?("/Users/JackFranklin/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/rspec-expectations-2.12.1/lib/rspec/expectations/fail_with.rb:33:in `fail_with'")
But the actual output in the terminal remains the same!
Edit 3:
I am running RSpec with the command rspec, and nothing more. The project's .rspec file contains:
--color
--format progress
And there is no ~/.rspec file either that could be applying settings.
So after a bit more chatting with the RSpec folk on Github, I managed to hunt down the problem.
The problem was that by default, RSpec removes lines from the backtrace that match the Regex /gems/. I had my project within a folder with gems in the name: ~/Dropbox/rubygems/myproject, so every line of the backtrace was being removed, and when that happens, RSpec sensibly shows you the entire backtrace.
That explains the behaviour I was seeing.
It's always something silly like that. I hope if anyone else does this, perhaps this answer will save them some time.
Looks like you're running RSpec with -b, for a full backtrace. Normally, RSpec won't show its own internal backtrace like that, even when your test fails badly (i.e. throws an exception rather than just failing an assertion). If you're not explicitly running it with -b or --backtrace, check and make sure that you don't have that set in a .rspec config file, or that your IDE or whatever isn't passing it.
As Jim said, there's a -b or --backtrace option that will enable the full backtrace. Bear in mind that it's not just the .rspec file that could be triggering it; there's also ~/.rspec (for a developer's preferred options).
The other thing to bear in mind is that a formatter can spit out whatever portion of the backtrace that it wants. The formatters expose a simple way for any 3rd-party formatter to respect the backtrace-filtering configuration, but it you're using a 3rd-party formatter there is no guarantee it is using that properly. Are you using one of the built-in rspec formatters?
Finally, if it's not one of those things, I'm out of ideas. I'd have to have an example to play with to answer your question. Can you come up with a reproducible example (in a gist, hopefully)? I try to hang out in the rspec channel of irc.freenode.net regularly, so maybe you can catch me there and we can do some troubleshooting that way.
From version 3.4.0 / 2015-11-11 it is possible to filter backtrace by option:
RSpec.configure do |config|
config.filter_rails_from_backtrace!
end

Ruby NoMethodError in Redmine from script/runner

I have some Ruby code I need to execute in the Redmine Ruby app in order to enable a module in all projects at once.
Surprisingly, ruby does access the Projects, but raises a NoMethodError when accessing a few particular methods inside each of the "Project" objects.
Here is the code:
Project.find(:all).each do |project|
print "Enabling modules for project '#{project.identifier}' ... "
puts project.methods.sort # this does print "enabled_module_names"
puts project.enabled_module_names
end
This fails with:
hostname:/srv/apps/redmine# script/runner vendor/plugins/customplugin/lib/enable_modules.rb
/var/lib/gems/1.8/gems/activerecord-2.3.5/lib/active_record/attribute_methods.rb:260:in `method_missing': undefined method `enabled_module_names' for #<Project:0x7f28985c1cb0> (NoMethodError)
from vendor/plugins/customplugin/lib/enable_modules.rb:14
from vendor/plugins/customplugin/lib/enable_modules.rb:7:in `each'
from vendor/plugins/customplugin/lib/enable_modules.rb:7
from /usr/lib/ruby/1.8/rubygems/custom_require.rb:31:in `eval'
from /var/lib/gems/1.8/gems/rails-2.3.5/lib/commands/runner.rb:46
from /usr/lib/ruby/1.8/rubygems/custom_require.rb:31:in `gem_original_require'
from /usr/lib/ruby/1.8/rubygems/custom_require.rb:31:in `require'
from script/runner:3
I have scratched my head a lot, but I can't grasp why would the code find the "Project" symbol but not the methods within, especially as "project.methods" does indeed list "enabled_module_names".
Any help most welcome.
Are you sure that enable_module_names is an instance method?
Is it within Project.instance_methods?
Edit (summary of the comments below):
In earlier versions, you have to use following:
enabled_module_names = project.enabled_modules.collect(&:name)
The getter is present in later versions only (see rev.4460 for details of this change)

Digest::MD5 in Ruby 1.9.3

I'm running into something weird here.
I have an "authenticator" that relies on ND5 to hash a certain string we match as password. The problem when I run my tests is this:
NoMethodError: undefined method `md5' for #<CASServer::Authenticators::Billing:0x007fd8e6c906a0>
./models/authenticators/billing.rb:63:in `validate'
./routes/login.rb:166:in `block (2 levels) in <class:Server>'
./routes/login.rb:158:in `each'
./routes/login.rb:158:in `block in <class:Server>'
(eval):2:in `click_button'
./features/step_definitions/when_steps.rb:32:in `/^I enter "(.*)" as username and the generated username password and log in$/'
./features/rubycas.login.feature:14:in `When I enter "username" as username and the generated username password and log in'
So basically he does not recognize the MD5 as part of the Digest library.
This problem occurs when running the tests in the IDE, as well as in the IRB console:
1.9.3-p125 :001 > require "digest/md5" and Digest::MD5("test")
NoMethodError: undefined method `MD5' for Digest:Module
However, when I run the following:
[root#DCUDEV01 /home/morn/rubycas/current]# ruby
require "digest/md5" and Digest::MD5("test")
I receive no errors, dumps or exceptions. Ruby just accepts it.
What am I missing in order to get this MD5 stuff working?
Digest::MD5 is not a method but a module. Try
Digest::MD5.digest("test")
I found it confusing, it doesn't seem like .digest is correct. I can't say it is wrong either...
Commenter #reconbot got it right, IMO, but I saw the comment only after coming back to improve this QA, I think a comment isn't visible enough.
http://ruby-doc.org/stdlib-2.4.0/libdoc/digest/rdoc/Digest/MD5.html
Below is an example usage for md5 hasing for gibbon usage, the mailchimp gem.
md5_hashed_email_address = Digest::MD5.hexdigest("john.doe#example.com")

Resources