开发者

What is the use of any_instance method in Rails

开发者 https://www.devze.com 2023-01-17 12:16 出处:网络
I see the following line in one of t开发者_运维问答he test files in rails. It has a method called as any_instance. What is its use? Can someone please explain

I see the following line in one of t开发者_运维问答he test files in rails. It has a method called as any_instance. What is its use? Can someone please explain

http = Net::HTTP.new(Person.site.host, Person.site.port)
ActiveResource::Connection.any_instance.expects(:http).returns(http)
http.expects(:request).returns(ActiveResource::Response.new(""))

Thanks


any_instance is a Mocha method. From the doc page:

Returns a mock object which will detect calls to any instance of this class.

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
0

精彩评论

暂无评论...
验证码 换一张
取 消