Home Ruby Any_instance,stub,mocha
Post
Cancel

Ruby Any_instance,stub,mocha

refer to
https://stackoverflow.com/questions/3760691/what-is-the-use-of-any-instance-method-in-rails

# 表示 所有的Product 的实例,在运行 save 方法后,都需要返回false 
Product.any_instance.stubs(:save).returns(false)
product_1 = Product.new
assert_equal false, product_1.save
product_2 = Product.new
assert_equal false, product_2.save

也就是说,可以给某个方法加上钩子,永远修改返回值。

完整例子:

This post is licensed under CC BY 4.0 by the author.