[rspec-users] Testing lib code

s.ross cwdinfo at gmail.com
Tue Dec 30 15:30:30 EST 2008

On Dec 30, 2008, at 11:02 AM, Peter Jaros wrote:

> On Tue, Dec 30, 2008 at 12:50 PM, s.ross <cwdinfo at gmail.com> wrote:
>> This question has, I'm sure, been asked and answered hundreds of  
>> times, but
>> I was unable to turn up anything in Google. Here's the issue: I  
>> have some
>> code in a Rails app in the lib/ directory that affects how views are
>> rendered. Specifically, it checks CSS and Javascript files for  
>> existence and
>> some application-specific stuff, then injects them into the right  
>> part of
>> the HTML that's rendered.
> To be honest, this smells funny to me.  Why is something in lib/
> affecting how views are rendered?
> Or does it need to be called or "activated" by something in the
> controller layer to take effect?  That would make sense, but it would
> be a bit tricky to spec.  I'm not sure there's enough information here
> about how it functions to give a good suggestion.
> Try being more specific (with us and with yourself) in describing the
> behavior of the objects which do this injection.  That might lead to
> you to a way to test that behavior.  Poor specs, as well as difficulty
> in writing specs, are often the result of not clearly defining what
> you want the behavior of the object to be.

I pasted the actual code on http://pastie.org/349188. The idea behind  
it is:

1. By using javascript_include_local_tag in your default layout (in  
the HEAD section), this will look for javascripts named for the  
controller and also for controller_action
2. Same for stylesheets with stylesheet_link_local_tag
3. By using inject_javascript in a view or a controller, you can  
"queue" a javascript include until render time. At render time, the  
injected_javascript helper renders it wherever that appears in the  
view (again, normally in the default layout's HEAD section).
4. Same for CSS using inject_css and injected_css

I though I had spec'ed this by side effect, as was suggested, by just  
making sure one or another of my controllers worked right. It turns  
out I hadn't gotten that quite right and I decided to dedicate a set  
of specs explicitly to this instead of burying them in with other stuff.

So... the question is how to get a request object so it's possible to  
explain and then verify the behavior.



More information about the rspec-users mailing list