undefined局部变量或方法`mock'for main:Object

时间:2021-08-31 23:53:10

I am using Mocha for testing.

我正在使用Mocha进行测试。

2.1.5 :047 > require 'mocha'
  => false 

AFAIK it means mocha is already loaded and should be fine. Anyway, when mocking or stubbing:

AFAIK意味着摩卡已经加载,应该没问题。无论如何,当嘲笑或抄袭时:

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?

模拟是否应该在IRB中起作用?

1 个解决方案

#1


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:

在包含Mocha :: API之前,我遇到了同样的错误。这将包括适当的Mocha方法到您的对象,模块,类类,并为您的irb会话添加错过的功能:

$ 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) >

#1


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:

在包含Mocha :: API之前,我遇到了同样的错误。这将包括适当的Mocha方法到您的对象,模块,类类,并为您的irb会话添加错过的功能:

$ 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) >