[rspec-users] [Rails] rails view helpers, BDD, and what to mock

J. B. Rainsberger me at jbrains.ca
Sun Sep 19 05:53:10 EDT 2010


On Fri, Sep 17, 2010 at 16:48, Doug E. <doug at emeryit.com> wrote:


> I'm trying to understand BDD and proper testing technique. I'm testing
> a rails view helper method that checks user roles to see if a link
> should be shown. I'm using rails 2.3.8 and rspec version 1.3.0.  My
> helper looks like this:
>
> # welcome_helper.rb
> module WelcomeHelper
>  def manage_samples_link
>  return nil if current_user.blank?
>  if current_user.role?(:submitter) ||
>      current_user.role?(:reviewer) ||
>      current_user.role?(:admin)
>    link_to "Manage Samples", manage_samples_path
>  end
>  end
> end
>

Your helper method depends on the user currently logged in, but hardwires
itself to how you have implemented "current user". I'd prefer to make the
dependency explicit, not just because that makes checking the behavior
easier.

module WelcomeHelper
  def manage_samples_link
    manage_samples_link_for_user(current_user)
  end
end

I think you'll find stubbing the :user parameter here must easier than
stubbing Rails' #current_user method.
-- 
J. B. (Joe) Rainsberger :: http://www.jbrains.ca ::
http://blog.thecodewhisperer.com
Diaspar Software Services :: http://www.diasparsoftware.com
Author, JUnit Recipes
2005 Gordon Pask Award for contribution to Agile practice :: Agile 2010:
Learn. Practice. Explore.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://rubyforge.org/pipermail/rspec-users/attachments/20100919/59ff1b1a/attachment-0001.html>


More information about the rspec-users mailing list