# How should I test this?

**URL:** <https://www.ruby-forum.com/t/how-should-i-test-this/167291>\
**Category:** Rails\
**Created:** [May 6, 2009, 1:37pm UTC](https://www.ruby-forum.com/t/how-should-i-test-this/167291 "2009-05-06T13:37:39Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![dr\_gavin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c37758/32.png) [@dr\_gavin](https://www.ruby-forum.com/u/dr_gavin)\
**Post date:** [May 6, 2009, 1:37pm UTC](https://www.ruby-forum.com/t/how-should-i-test-this/167291/1 "2009-05-06T13:37:39Z")

</div>

Hi all

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](http://mydomain.com)”

def after\_create(post)  
Net::HTTP.get(‘[www.google.com](http://www.google.com)’, ‘/ping?sitemap=’ + URI.escape  
(sitemap\_url))  
end  
end

How could I write a test for this?  
Also - how could I disable the actual request while running in test  
mode?

Thanks

Gavin

---

<div class="post-metadata">

**Author:** ![dr\_gavin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c37758/32.png) [@dr\_gavin](https://www.ruby-forum.com/u/dr_gavin)\
**Post date:** [May 6, 2009, 3:20pm UTC](https://www.ruby-forum.com/t/how-should-i-test-this/167291/2 "2009-05-06T15:20:17Z")

</div>

Gavin wrote:

> include ActionController::UrlWriter  
> default\_url\_options[:host] = “[mydomain.com](http://mydomain.com)”
> 
> def after\_create(post)  
> Net::HTTP.get(‘[www.google.com](http://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](http://flea.sourceforge.net/resume.html)

---

<div class="post-metadata">

**Author:** ![dr\_gavin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c37758/32.png) [@dr\_gavin](https://www.ruby-forum.com/u/dr_gavin)\
**Post date:** [May 6, 2009, 5:58pm UTC](https://www.ruby-forum.com/t/how-should-i-test-this/167291/3 "2009-05-06T17:58:50Z")

</div>

> 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”.)

Would this apply to model\_observers too?

---

<div class="post-metadata">

**Author:** ![dr\_gavin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c37758/32.png) [@dr\_gavin](https://www.ruby-forum.com/u/dr_gavin)\
**Post date:** [May 6, 2009, 8:20pm UTC](https://www.ruby-forum.com/t/how-should-i-test-this/167291/4 "2009-05-06T20:20:13Z")

</div>

Gavin wrote:

> > 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”.)
> 
> Would this apply to model\_observers too?

Yes; MVC theory suggests observers are controllers. We all wish  
observers did  
not exist, and controllers simply watched everything! Observers are just  
exposed  
plumbing…

–  
Phlip  
[http://flea.sourceforge.net/resume.html](http://flea.sourceforge.net/resume.html)

---

<div class="post-metadata">

**Author:** ![dr\_gavin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c37758/32.png) [@dr\_gavin](https://www.ruby-forum.com/u/dr_gavin)\
**Post date:** [May 6, 2009, 8:38pm UTC](https://www.ruby-forum.com/t/how-should-i-test-this/167291/5 "2009-05-06T20:38:26Z")

</div>

So instead of sending an email after a user is created (for example)  
from the observer, It would be better to have the controller send it  
or call a model method to send it?
