Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby 未定义的局部变量或方法“mock';对于main:Object_Ruby_Unit Testing - Fatal编程技术网

Ruby 未定义的局部变量或方法“mock';对于main:Object

Ruby 未定义的局部变量或方法“mock';对于main:Object,ruby,unit-testing,Ruby,Unit Testing,我正在使用它进行测试 2.1.5 :047 > require 'mocha' => false 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/

我正在使用它进行测试

2.1.5 :047 > require 'mocha'
  => false 
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>'
2.1.5:048>模拟
NameError:main:对象的未定义局部变量或方法“mock”
来自(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\u tasks.rb:69:在“控制台”中
from/home/dawid/.rvm/gems/ruby-2.1.5/gems/railties-4.1.10/lib/rails/commands/commands\u tasks.rb:40:in“run\u command!”
from/home/dawid/.rvm/gems/ruby-2.1.5/gems/railties-4.1.10/lib/rails/commands.rb:17:in`'
from./test/dummy/bin/rails:4:in'require'
from./test/dummy/bin/rails:4:in`'
2.1.5:049>Github.expects:repos
NoMethodError:Github:Module的未定义方法“expects”
from/home/dawid/.rvm/gems/ruby-2.1.5/gems/github_api-0.12.3/lib/github_api.rb:56:in'method_missing'
来自(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\u tasks.rb:69:在“控制台”中
from/home/dawid/.rvm/gems/ruby-2.1.5/gems/railties-4.1.10/lib/rails/commands/commands\u tasks.rb:40:in“run\u command!”
from/home/dawid/.rvm/gems/ruby-2.1.5/gems/railties-4.1.10/lib/rails/commands.rb:17:in`'
from./test/dummy/bin/rails:4:in'require'
from./test/dummy/bin/rails:4:in`'
2.1.5:051>存根
NameError:未定义的局部变量或main:对象的方法“stub”
来自(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\u tasks.rb:69:在“控制台”中
from/home/dawid/.rvm/gems/ruby-2.1.5/gems/railties-4.1.10/lib/rails/commands/commands\u tasks.rb:40:in“run\u command!”
from/home/dawid/.rvm/gems/ruby-2.1.5/gems/railties-4.1.10/lib/rails/commands.rb:17:in`'
from./test/dummy/bin/rails:4:in'require'
from./test/dummy/bin/rails:4:in`'

模拟是否应该在IRB中工作?

在我加入
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) >
$irb
欢迎来到IRB。您正在使用ruby 2.2.0p0(2014-12-25修订版49005)[x86_64-linux]。玩得开心;)
>>要求“摩卡/api”#=>正确
>>包括Mocha::API#=>对象
>>模拟#=>#
>>存根#=>#
>>Object.expected:something#=>#

它也不起作用。无论如何,只给出代码示例是一种解释问题的糟糕方式。