I am using Mocha for testing.
2.1.5 :047 > require 'mocha'
=> false
AFAIK it means mocha is already loaded and should be fine. Anyway, when mocking or stubbing:
2.1.5 :048 > mock
NameError: undefined local variable or method `mock' for main:Object
from (irb):48
from /home/dawid/.rvm/gems/ruby-2.1.5/gems/railties-4.1.10/lib/rails/commands/console.rb:90:in `start'
from /home/dawid/.rvm/gems/ruby-2.1.5/gems/railties-4.1.10/lib/rails/commands/console.rb:9:in `start'
from /home/dawid/.rvm/gems/ruby-2.1.5/gems/railties-4.1.10/lib/rails/commands/commands_tasks.rb:69:in `console'
from /home/dawid/.rvm/gems/ruby-2.1.5/gems/railties-4.1.10/lib/rails/commands/commands_tasks.rb:40:in `run_command!'
from /home/dawid/.rvm/gems/ruby-2.1.5/gems/railties-4.1.10/lib/rails/commands.rb:17:in `<top (required)>'
from ./test/dummy/bin/rails:4:in `require'
from ./test/dummy/bin/rails:4:in `<main>'
2.1.5 :049 > Github.expects :repos
NoMethodError: undefined method `expects' for Github:Module
from /home/dawid/.rvm/gems/ruby-2.1.5/gems/github_api-0.12.3/lib/github_api.rb:56:in `method_missing'
from (irb):49
from /home/dawid/.rvm/gems/ruby-2.1.5/gems/railties-4.1.10/lib/rails/commands/console.rb:90:in `start'
from /home/dawid/.rvm/gems/ruby-2.1.5/gems/railties-4.1.10/lib/rails/commands/console.rb:9:in `start'
from /home/dawid/.rvm/gems/ruby-2.1.5/gems/railties-4.1.10/lib/rails/commands/commands_tasks.rb:69:in `console'
from /home/dawid/.rvm/gems/ruby-2.1.5/gems/railties-4.1.10/lib/rails/commands/commands_tasks.rb:40:in `run_command!'
from /home/dawid/.rvm/gems/ruby-2.1.5/gems/railties-4.1.10/lib/rails/commands.rb:17:in `<top (required)>'
from ./test/dummy/bin/rails:4:in `require'
from ./test/dummy/bin/rails:4:in `<main>'
2.1.5 :051 > stub
NameError: undefined local variable or method `stub' for main:Object
from (irb):51
from /home/dawid/.rvm/gems/ruby-2.1.5/gems/railties-4.1.10/lib/rails/commands/console.rb:90:in `start'
from /home/dawid/.rvm/gems/ruby-2.1.5/gems/railties-4.1.10/lib/rails/commands/console.rb:9:in `start'
from /home/dawid/.rvm/gems/ruby-2.1.5/gems/railties-4.1.10/lib/rails/commands/commands_tasks.rb:69:in `console'
from /home/dawid/.rvm/gems/ruby-2.1.5/gems/railties-4.1.10/lib/rails/commands/commands_tasks.rb:40:in `run_command!'
from /home/dawid/.rvm/gems/ruby-2.1.5/gems/railties-4.1.10/lib/rails/commands.rb:17:in `<top (required)>'
from ./test/dummy/bin/rails:4:in `require'
from ./test/dummy/bin/rails:4:in `<main>'
Does mocking should work in IRB or not?
I had the same error before I include Mocha::API. This will include appropriate Mocha's methods to your Object, Module, Class classes and add missed functionality to your irb session:
$ irb
Welcome to IRB. You are using ruby 2.2.0p0 (2014-12-25 revision 49005) [x86_64-linux]. Have fun ;)
>> require 'mocha/api' #=> true
>> include Mocha::API #=> Object
>> mock #=> #<Mock:0x2da1ce8>
>> stub #=> #<Mock:0x2d6a680>
>> Object.expects :something #=> #<Expectation:0x256e878 expected exactly once, not yet invoked: Object.something(any_parameters) >
Related
I was trying to add my custom implementation for ActiveRecordss find_in_batches method. First, I tried to monkeypatch the corresponding module:
module ActiveRecord
module Batches
def find_in_batches2
end
end
end
Task.find_in_batches2 do |group|
end
But ruby said:
NoMethodError: undefined method `find_in_batches2' for Task (call 'Task.connection' to establish a connection):Class
/home/yuri/.gem/ruby/2.1.5/gems/activerecord-4.2.0/lib/active_record/dynamic_matchers.rb:26:in `method_missing'
/srv/http/tm/Rakefile:15:in `<top (required)>'
/home/yuri/.gem/ruby/2.1.5/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:268:in `load'
/home/yuri/.gem/ruby/2.1.5/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:268:in `block in load'
/home/yuri/.gem/ruby/2.1.5/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:240:in `load_dependency'
/home/yuri/.gem/ruby/2.1.5/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:268:in `load'
/home/yuri/.gem/ruby/2.1.5/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:268:in `load'
/home/yuri/.gem/ruby/2.1.5/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:268:in `block in load'
/home/yuri/.gem/ruby/2.1.5/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:240:in `load_dependency'
/home/yuri/.gem/ruby/2.1.5/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:268:in `load'
-e:1:in `<main>'
Then I tried following this answer, to no avail. What am I doing wrong and why is it so hard?
Here is the solution:
ActiveRecord::Batches.module_eval do
def find_in_batches2
end
end
ActiveRecord::Querying.module_eval do
delegate :find_in_batches2, :to => :all
end
Don't forget to implement find_in_batches2.
Before anything, I know 1.9.1 (1.9.1-p243 specifically) is old and broken, but we have an app with a pretty strict dependency on it.
First off, is there a list of rspec-supported rubies?
Secondly, when putting anything in a describe block in a spec, I get an error uninitialized constant BasicObject::Class (NameError).
With this spec:
require_relative 'rspec_basic_object_test'
describe RspecBasicObjectTest do
before :each do
#obj = RspecBasicObjectTest.new
end
it 'returns something' do
#obj.foo.should == "foo"
end
end
and this ruby file:
class RspecBasicObjectTest
attr_reader :foo
def initialize
#foo = "foo"
end
end
I get:
dlampa#ubuntu:~/code/mine$ rspec rspec_basic_object_test_spec.rb
/home/dlampa/.rvm/gems/ruby-1.9.1-p243#rspec-test/gems/rspec-mocks-2.14.6/lib/rspec/mocks/syntax.rb:74:in `block in enable_should': uninitialized constant BasicObject::Class (NameError)
from /home/dlampa/.rvm/gems/ruby-1.9.1-p243#rspec-test/gems/rspec-mocks-2.14.6/lib/rspec/mocks/syntax.rb:28:in `class_eval'
from /home/dlampa/.rvm/gems/ruby-1.9.1-p243#rspec-test/gems/rspec-mocks-2.14.6/lib/rspec/mocks/syntax.rb:28:in `enable_should'
from /home/dlampa/.rvm/gems/ruby-1.9.1-p243#rspec-test/gems/rspec-mocks-2.14.6/lib/rspec/mocks/configuration.rb:34:in `syntax='
from /home/dlampa/.rvm/gems/ruby-1.9.1-p243#rspec-test/gems/rspec-mocks-2.14.6/lib/rspec/mocks/configuration.rb:52:in `<module:Mocks>'
from /home/dlampa/.rvm/gems/ruby-1.9.1-p243#rspec-test/gems/rspec-mocks-2.14.6/lib/rspec/mocks/configuration.rb:2:in `<module:RSpec>'
from /home/dlampa/.rvm/gems/ruby-1.9.1-p243#rspec-test/gems/rspec-mocks-2.14.6/lib/rspec/mocks/configuration.rb:1:in `<top (required)>'
from /home/dlampa/.rvm/rubies/ruby-1.9.1-p243/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
from /home/dlampa/.rvm/rubies/ruby-1.9.1-p243/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
from /home/dlampa/.rvm/gems/ruby-1.9.1-p243#rspec-test/gems/rspec-mocks-2.14.6/lib/rspec/mocks/framework.rb:34:in `<top (required)>'
from /home/dlampa/.rvm/rubies/ruby-1.9.1-p243/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
from /home/dlampa/.rvm/rubies/ruby-1.9.1-p243/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
from /home/dlampa/.rvm/gems/ruby-1.9.1-p243#rspec-test/gems/rspec-mocks-2.14.6/lib/rspec/mocks.rb:1:in `<top (required)>'
from /home/dlampa/.rvm/rubies/ruby-1.9.1-p243/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
from /home/dlampa/.rvm/rubies/ruby-1.9.1-p243/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
from /home/dlampa/.rvm/gems/ruby-1.9.1-p243#rspec-test/gems/rspec-core-2.14.8/lib/rspec/core/mocking/with_rspec.rb:1:in `<top (required)>'
from /home/dlampa/.rvm/rubies/ruby-1.9.1-p243/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
from /home/dlampa/.rvm/rubies/ruby-1.9.1-p243/lib/ruby/site_ruby/1.9.1/rubygems/custom_require.rb:36:in `require'
from /home/dlampa/.rvm/gems/ruby-1.9.1-p243#rspec-test/gems/rspec-core-2.14.8/lib/rspec/core/configuration.rb:388:in `mock_with'
from /home/dlampa/.rvm/gems/ruby-1.9.1-p243#rspec-test/gems/rspec-core-2.14.8/lib/rspec/core/configuration.rb:278:in `mock_framework'
from /home/dlampa/.rvm/gems/ruby-1.9.1-p243#rspec-test/gems/rspec-core-2.14.8/lib/rspec/core/configuration.rb:884:in `configure_mock_framework'
from /home/dlampa/.rvm/gems/ruby-1.9.1-p243#rspec-test/gems/rspec-core-2.14.8/lib/rspec/core/example_group.rb:280:in `ensure_example_groups_are_configured'
from /home/dlampa/.rvm/gems/ruby-1.9.1-p243#rspec-test/gems/rspec-core-2.14.8/lib/rspec/core/example_group.rb:295:in `set_it_up'
from /home/dlampa/.rvm/gems/ruby-1.9.1-p243#rspec-test/gems/rspec-core-2.14.8/lib/rspec/core/example_group.rb:245:in `subclass'
from /home/dlampa/.rvm/gems/ruby-1.9.1-p243#rspec-test/gems/rspec-core-2.14.8/lib/rspec/core/example_group.rb:232:in `describe'
from /home/dlampa/.rvm/gems/ruby-1.9.1-p243#rspec-test/gems/rspec-core-2.14.8/lib/rspec/core/dsl.rb:18:in `describe'
from /home/dlampa/code/mine/rspec_basic_object_test_spec.rb:3:in `<top (required)>'
from /home/dlampa/.rvm/gems/ruby-1.9.1-p243#rspec-test/gems/rspec-core-2.14.8/lib/rspec/core/configuration.rb:896:in `load'
from /home/dlampa/.rvm/gems/ruby-1.9.1-p243#rspec-test/gems/rspec-core-2.14.8/lib/rspec/core/configuration.rb:896:in `block in load_spec_files'
from /home/dlampa/.rvm/gems/ruby-1.9.1-p243#rspec-test/gems/rspec-core-2.14.8/lib/rspec/core/configuration.rb:896:in `each'
from /home/dlampa/.rvm/gems/ruby-1.9.1-p243#rspec-test/gems/rspec-core-2.14.8/lib/rspec/core/configuration.rb:896:in `load_spec_files'
from /home/dlampa/.rvm/gems/ruby-1.9.1-p243#rspec-test/gems/rspec-core-2.14.8/lib/rspec/core/command_line.rb:22:in `run'
from /home/dlampa/.rvm/gems/ruby-1.9.1-p243#rspec-test/gems/rspec-core-2.14.8/lib/rspec/core/runner.rb:80:in `run'
from /home/dlampa/.rvm/gems/ruby-1.9.1-p243#rspec-test/gems/rspec-core-2.14.8/lib/rspec/core/runner.rb:17:in `block in autorun'
Anyone else run into this? Or am I just missing something obvious in rspec's README that says 1.9.1 is old and broken, why are you using it?
My Code in ruby class is. Here i am getting uninitialized constant ActionDispatch (NameError)
DatabaseCleaner.strategy = :transaction
class ActionDispatch::IntegrationTest
# Make the Capybara DSL available in all integration tests
include Capybara::DSL
# Stop ActiveRecord from wrapping tests in transactions
self.use_transactional_fixtures = false
teardown do
DatabaseCleaner.clean # Truncate the database
Capybara.reset_sessions! # Forget the (simulated) browser state
Capybara.use_default_driver
end
end
I ran into the same issue, after upgrading.
This is my stack trace:
uninitialized constant ActionDispatch::IntegrationTest (NameError)
/Users/etagwerker/.rvm/gems/ruby-1.9.3-p327#ombu/gems/cucumber-rails-1.3.1/lib/cucumber/rails/world.rb:9:in `<module:Rails>'
/Users/etagwerker/.rvm/gems/ruby-1.9.3-p327#ombu/gems/cucumber-rails-1.3.1/lib/cucumber/rails/world.rb:8:in `<module:Cucumber>'
/Users/etagwerker/.rvm/gems/ruby-1.9.3-p327#ombu/gems/cucumber-rails-1.3.1/lib/cucumber/rails/world.rb:7:in `<top (required)>'
/Users/etagwerker/.rvm/gems/ruby-1.9.3-p327#ombu/gems/backports-3.3.0/lib/backports/tools.rb:328:in `require'
/Users/etagwerker/.rvm/gems/ruby-1.9.3-p327#ombu/gems/backports-3.3.0/lib/backports/tools.rb:328:in `require_with_backports'
/Users/etagwerker/Projects/om/features/support/env.rb:11:in `<top (required)>'
/Users/etagwerker/.rvm/gems/ruby-1.9.3-p327#ombu/gems/cucumber-1.3.1/lib/cucumber/rb_support/rb_language.rb:122:in `load'
/Users/etagwerker/.rvm/gems/ruby-1.9.3-p327#ombu/gems/cucumber-1.3.1/lib/cucumber/rb_support/rb_language.rb:122:in `load_code_file'
/Users/etagwerker/.rvm/gems/ruby-1.9.3-p327#ombu/gems/cucumber-1.3.1/lib/cucumber/runtime/support_code.rb:180:in `load_file'
/Users/etagwerker/.rvm/gems/ruby-1.9.3-p327#ombu/gems/cucumber-1.3.1/lib/cucumber/runtime/support_code.rb:83:in `block in load_files!'
/Users/etagwerker/.rvm/gems/ruby-1.9.3-p327#ombu/gems/cucumber-1.3.1/lib/cucumber/runtime/support_code.rb:82:in `each'
/Users/etagwerker/.rvm/gems/ruby-1.9.3-p327#ombu/gems/cucumber-1.3.1/lib/cucumber/runtime/support_code.rb:82:in `load_files!'
/Users/etagwerker/.rvm/gems/ruby-1.9.3-p327#ombu/gems/cucumber-1.3.1/lib/cucumber/runtime.rb:183:in `load_step_definitions'
/Users/etagwerker/.rvm/gems/ruby-1.9.3-p327#ombu/gems/cucumber-1.3.1/lib/cucumber/runtime.rb:42:in `run!'
/Users/etagwerker/.rvm/gems/ruby-1.9.3-p327#ombu/gems/cucumber-1.3.1/lib/cucumber/cli/main.rb:47:in `execute!'
/Users/etagwerker/.rvm/gems/ruby-1.9.3-p327#ombu/gems/cucumber-1.3.1/bin/cucumber:13:in `<top (required)>'
script/cucumber:9:in `load'
script/cucumber:9:in `<main>'
I am using rails 3.0.20 and cucumber-rails 1.3.1.
After trying a few things, I tried this:
rails generate cucumber:install
That solved it for me.
I want to extend the faker gem for rails to also generate other random date (in my case computer game names)
#lib/extended_faker.rb
require 'faker'
require 'extended_faker/game'
#lib/extended_faker/game.rb
Module Faker
class Game < Faker::Base
class << self
def name
fetch('game.name')
end
end
end
end
#config/locals/faker_en.yml
en:
faker:
game:
name: ["a", "b", "c"]
#config/application.rb
...
config.autoload_paths += Dir["#{config.root}/lib/**/"]
...
then when i run it in a rails console i get the following
Loading development environment (Rails 3.2.9)
1.9.3p194 :001 > Faker::Game.name
LoadError: Expected /home/enermis/School/Projects/IG/test/lib/extended_faker/game.rb to define Game
from /home/enermis/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.9/lib/active_support/dependencies.rb:503:in `load_missing_constant'
from /home/enermis/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.9/lib/active_support/dependencies.rb:192:in `block in const_missing'
from /home/enermis/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.9/lib/active_support/dependencies.rb:190:in `each'
from /home/enermis/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.9/lib/active_support/dependencies.rb:190:in `const_missing'
from /home/enermis/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.9/lib/active_support/dependencies.rb:514:in `load_missing_constant'
from /home/enermis/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.9/lib/active_support/dependencies.rb:192:in `block in const_missing'
from /home/enermis/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.9/lib/active_support/dependencies.rb:190:in `each'
from /home/enermis/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.9/lib/active_support/dependencies.rb:190:in `const_missing'
from (irb):1
from /home/enermis/.rvm/gems/ruby-1.9.3-p194/gems/railties-3.2.9/lib/rails/commands/console.rb:47:in `start'
from /home/enermis/.rvm/gems/ruby-1.9.3-p194/gems/railties-3.2.9/lib/rails/commands/console.rb:8:in `start'
from /home/enermis/.rvm/gems/ruby-1.9.3-p194/gems/railties-3.2.9/lib/rails/commands.rb:41:in `<top (required)>'
from script/rails:6:in `require'
from script/rails:6:in `<main>'
When i change the lib/extended_faker/game.rb file to this
require 'faker'
require 'extended_faker/game'
include 'extended_faker/item'
include 'extended_faker/team'
i get weird behavior in the console
Loading development environment (Rails 3.2.9)
1.9.3p194 :001 > Faker::Game.name
=> "b"
1.9.3p194 :002 > Faker::Game.name
NameError: uninitialized constant Faker::Game
from (irb):2
from /home/enermis/.rvm/gems/ruby-1.9.3-p194/gems/railties-3.2.9/lib/rails/commands/console.rb:47:in `start'
from /home/enermis/.rvm/gems/ruby-1.9.3-p194/gems/railties-3.2.9/lib/rails/commands/console.rb:8:in `start'
from /home/enermis/.rvm/gems/ruby-1.9.3-p194/gems/railties-3.2.9/lib/rails/commands.rb:41:in `<top (required)>'
from script/rails:6:in `require'
from script/rails:6:in `<main>'
The weird thing i don't understand is that the first time i run the generator, i get a valid result, but the second time around i get an error...
What am i missing?
you shoud put it in
lib/faker/game.rb
starting in the lib directory, rails convention is outermost module name -> in are folder names. Then the actual class/module name is the file name, underscored to the camelcase.
Another example
module Foo
module Bar
class BazParty
def self.hello
puts "hello"
end
end
end
end
would go in lib/foo/bar/baz_party.rb
I found the following problem to use net/sftp:
undefined method `put_file' for #<Net::SFTP::Session:0x00000001b40298>
(NoMethodError)
sftp.rb:17:in `block in <main>': undefined method `put_file' for #<Net::SFTP::Session:0x00000000b70138> (NoMethodError)
from /opt/ruby/lib/ruby/gems/1.9.1/gems/net-sftp-2.0.5/lib/net/sftp/session.rb:939:in `call'
from /opt/ruby/lib/ruby/gems/1.9.1/gems/net-sftp-2.0.5/lib/net/sftp/session.rb:939:in `block in do_version'
from /opt/ruby/lib/ruby/gems/1.9.1/gems/net-sftp-2.0.5/lib/net/sftp/session.rb:939:in `each'
from /opt/ruby/lib/ruby/gems/1.9.1/gems/net-sftp-2.0.5/lib/net/sftp/session.rb:939:in `do_version'
from /opt/ruby/lib/ruby/gems/1.9.1/gems/net-sftp-2.0.5/lib/net/sftp/session.rb:909:in `when_channel_polled'
from /opt/ruby/lib/ruby/gems/1.9.1/gems/net-ssh-2.1.4/lib/net/ssh/connection/channel.rb:311:in `call'
from /opt/ruby/lib/ruby/gems/1.9.1/gems/net-ssh-2.1.4/lib/net/ssh/connection/channel.rb:311:in `process'
from /opt/ruby/lib/ruby/gems/1.9.1/gems/net-ssh-2.1.4/lib/net/ssh/connection/session.rb:214:in `block in preprocess'
from /opt/ruby/lib/ruby/gems/1.9.1/gems/net-ssh-2.1.4/lib/net/ssh/connection/session.rb:214:in `each'
from /opt/ruby/lib/ruby/gems/1.9.1/gems/net-ssh-2.1.4/lib/net/ssh/connection/session.rb:214:in `preprocess'
from /opt/ruby/lib/ruby/gems/1.9.1/gems/net-ssh-2.1.4/lib/net/ssh/connection/session.rb:197:in `process'
from /opt/ruby/lib/ruby/gems/1.9.1/gems/net-ssh-2.1.4/lib/net/ssh/connection/session.rb:161:in `block in loop'
from /opt/ruby/lib/ruby/gems/1.9.1/gems/net-ssh-2.1.4/lib/net/ssh/connection/session.rb:161:in `loop'
from /opt/ruby/lib/ruby/gems/1.9.1/gems/net-ssh-2.1.4/lib/net/ssh/connection/session.rb:161:in `loop'
from /opt/ruby/lib/ruby/gems/1.9.1/gems/net-sftp-2.0.5/lib/net/sftp/session.rb:802:in `loop'
from /opt/ruby/lib/ruby/gems/1.9.1/gems/net-sftp-2.0.5/lib/net/sftp/session.rb:787:in `connect!'
from /opt/ruby/lib/ruby/gems/1.9.1/gems/net-sftp-2.0.5/lib/net/sftp.rb:32:in `start'
from sftp.rb:16:in `<main>'
But the module could be loaded without problem:
2011.07.08|17:12:07~/lin/Ruby>irb
irb(main):001:0> require 'net/ssh'
=> true
irb(main):002:0> require 'net/sftp'
=> true
irb(main):003:0>
By the way, my RUBYLIB is:
2011.07.08|17:15:33~/lin/Ruby>echo $RUBYLIB
/opt/ruby/lib/ruby/1.9.1/
Thanks!
Dan
P.S:
require 'net/ssh'
require 'net/sftp'
host="localhost"
src_file="/etc/services"
dst_file="~/services"
Net::SFTP.start(host, ENV["USER"]) do |sftp|
sftp.put_file(src_file, dst_file)
end
......
Net::SFTP does not have a put_file method. See the full documentation for Net::SFTP.
Did you perhaps mean sftp.upload or sftp.upload!?
Check your version. Net::SFTP 2 doesn't have put_file anymore. Use upload instead.