[rspec-users] how to spec views

Andrew WC Brown omen.king at gmail.com
Mon Oct 1 09:08:22 EDT 2007


The was really helpful, thanks David!

"There is no simple answer to your question. If anyone offers you one,
treat it with a grain of salt."

The game I'm specing actually has an attribute called grains_of_salt.
No Lie.


On 10/1/07, David Chelimsky <dchelimsky at gmail.com> wrote:
>
> On 10/1/07, Andrew WC Brown <omen.king at gmail.com> wrote:
> > I'm trying to spec a view but haven't done much view specing.
> >
> > This view render different partials depending on authentication of the
> user:
> > annon, admin, player
> > So I I'll write if conditionals in the view with the partials
> >
> >
> >   it "should render signup propaganda for annon users trying to view
> games"
> > do
> >     render "/games/index.rhtml"
> >     @logged_in?.should eql(false)
> >     response.should render_template('_signup_propaganda')
> >   end
> >
> > Now for my partial I know it'll be wrapped all in a div with a
> > class="signup_propaganda"
> > Should I be testing for that instead? Can I write expectations for
> partials
> > similar to above?
> >
> >  When your specing views are you testing for the outputted results?
> >
> >   it "should render signup propaganda for annon users trying to view
> games"
> > do
> >      render "/games/index.rhtml"
> >      @logged_in?.should eql(false)
> >      response.should have_tag(div, "class=/"signup_propaganda/"")
> >    end
> >
> > How should I be writing my spec?
>
> There is no simple answer to your question. If anyone offers you one,
> treat it with a grain of salt.
>
> Coding by example is a process. If you're doing it right, the examples
> are going to change as you progress. So in this case, I might start
> like this:
>
> it "should render signup propaganda for annon users trying to view games"
> do
>   controller.stub!(:logged_in?).and_return(false)
>   render "/games/index.rhtml"
>   response.should have_tag('div.signup_propaganda')
> end
>
> The code to make this pass could just be:
>
> <div class='signup_propoganda'/>
>
> At this point I'd want to add an example about what a logged in user
> sees to force the conditional:
>
> it "should NOT render signup propaganda for logged in users trying to
> view games" do
>   controller.stub!(:logged_in?).and_return(true)
>   render "/games/index.rhtml"
>   response.should_not have_tag('div.signup_propaganda')
> end
>
> leading to this code:
>
> <% if logged_in? %>
>   <div class='signup_propoganda'/>
> <% end %>
>
> At some point down the line I might decide to extract the div to a
> partial. At *that* point, I should be able to do so without changing
> the example. Once the partial has been extracted, then comes the
> question about what to do with the example, and the answer will depend
> on a few things.
>
> If the partial is only ever used in this one template, and requires no
> additional setup, and the only reason I extracted it was to clean up
> the template, I might leave things as/is.
>
> Most of the time, however, I'd change the examples to expect that the
> partial gets rendered. First, I'd create a new example for the partial
> itself and move anything from the old example that was specific to the
> content inside that partial. Only after that's done and all examples
> are passing, I'd change the original examples to look like this:
>
> it "should render signup propaganda for annon users trying to view games"
> do
>   controller.stub!(:logged_in?).and_return(false)
>   template.expect_render(:partial => 'signup_propoganda')
>   render "/games/index.rhtml"
> end
>
> it "should NOT render signup propaganda for logged in users trying to
> view games" do
>   controller.stub!(:logged_in?).and_return(true)
>   template.expect_render(:partial => 'signup_propoganda').never
>   render "/games/index.rhtml"
> end
>
> HTH,
> David
>
> >
> > _______________________________________________
> > rspec-users mailing list
> > rspec-users at rubyforge.org
> > http://rubyforge.org/mailman/listinfo/rspec-users
> >
> _______________________________________________
> rspec-users mailing list
> rspec-users at rubyforge.org
> http://rubyforge.org/mailman/listinfo/rspec-users
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://rubyforge.org/pipermail/rspec-users/attachments/20071001/2a90a6da/attachment-0001.html 


More information about the rspec-users mailing list