Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the use of any_instance method in Rails

I see the following line in one of the 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

like image 720
bragboy Avatar asked Sep 21 '10 13:09

bragboy


1 Answers

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
like image 152
Daniel Vandersluis Avatar answered Sep 30 '22 12:09

Daniel Vandersluis