Gavin wrote:
> In my app I'd like to send a request to google sitemaps indexer after
> a new post is created.
>
> This would take place in the post_observer and would look something
> like this:
>
> require 'net/http'
> require 'uri'
> class PostObserver < ActiveRecord::Observer
>
> include ActionController::UrlWriter
> default_url_options[:host] = "mydomain.com"
>
> def after_create(post)
> Net::HTTP.get('www.google.com', '/ping?sitemap=' + URI.escape
> (sitemap_url))
> end
> end
>
> How could I write a test for this?
Firstly, after_create should not call the service directly - it should call a
method in a GoogleApi module that calls the service. (See the thread "no sql in
the controller guideline".)
> Also - how could I disable the actual request while running in test
> mode?
Use Mocha to introduce a mock.
Call the real service one time and p the result out. Copy the result into the
test code and configure a test to mock either the Net::HTTP or the GoogleApi.
The mock will return your recorded result.
--
Phlip
http://flea.sourceforge.net/resume.html
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby
on Rails: Talk" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---