Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/21.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 在Mocha中解压类方法_Ruby_Mocking_Stub_Ruby Mocha - Fatal编程技术网

Ruby 在Mocha中解压类方法

Ruby 在Mocha中解压类方法,ruby,mocking,stub,ruby-mocha,Ruby,Mocking,Stub,Ruby Mocha,对于特定的测试,我想更改类方法的返回值 我可以通过调用MyClass.expects(:method).returns(:myvalue)来获得正确的行为。完成测试后,如何停止这种行为 Mocha中有一个unsub方法,但它似乎只对实例方法有效,而对类方法无效。您使用的Mocha版本号是多少 这适用于MRI/mocha 0.9.12: class T def self.hello "hi" end end T.hello # => "hi" T.expects(:hell

对于特定的测试,我想更改类方法的返回值

我可以通过调用MyClass.expects(:method).returns(:myvalue)来获得正确的行为。完成测试后,如何停止这种行为


Mocha中有一个
unsub
方法,但它似乎只对实例方法有效,而对类方法无效。

您使用的Mocha版本号是多少

这适用于MRI/mocha 0.9.12:

class T
  def self.hello
    "hi"
  end
end

T.hello # => "hi"
T.expects(:hello).returns("hello")
T.hello # => "hello"
T.unstub(:hello)
T.hello # => "hi"
T.expects(:hi).returns("world")
T.hi    # => "world"
T.unstub(:hi)
T.hi    # => NoMethodError: undefined method ....

你用的是什么版本的摩卡咖啡

这适用于MRI/mocha 0.9.12:

class T
  def self.hello
    "hi"
  end
end

T.hello # => "hi"
T.expects(:hello).returns("hello")
T.hello # => "hello"
T.unstub(:hello)
T.hello # => "hi"
T.expects(:hi).returns("world")
T.hi    # => "world"
T.unstub(:hi)
T.hi    # => NoMethodError: undefined method ....

这就是诀窍:我用的是摩卡-0.9.8。我误读了0.9.12文档,认为它们只是实例,并将我的错误消息误解为对此的确认。谢谢。这就是诀窍:我用的是摩卡-0.9.8。我误读了0.9.12文档,认为它们只是实例,并将我的错误消息误解为对此的确认。谢谢