[rspec-users] how to spec views

Andrew WC Brown omen.king at gmail.com
Mon Oct 1 21:34:13 EDT 2007


I think I asked to quickly.
I'm guessing its

template.expect_render(:partial => 'games', :collection => @games)

On 10/1/07, Andrew WC Brown <omen.king at gmail.com> wrote:
>
> How about specing partial collections?
>
>   it "should render a list of games for authenticated users" do
>    template.stub!(:logged_in?).and_return(true)
>    template.expect_render(:partial => 'games')
>    render "/games/index.rhtml"
>   end
>
> <%= render :partial => 'games', :collection => @games %>
>
> This fails as soon as I add it as a  collection
>
> On 10/1/07, David Chelimsky <dchelimsky at gmail.com> wrote:
> >
> > On 10/1/07, Andrew WC Brown <omen.king at gmail.com> wrote:
> > > Specing Image Tags?
> > >
> > >   it "should show a propaganda image" do
> > >    render "/games/index.rhtml"
> > >    response.should have_tag('img','signup_propaganda.gif')
> > >   end
> > >
> > > The following doesn't work. Im guess have_tag isnt the best approach
> > to test
> > > them.
> > > Should I just use have_text and look for the img and
> > signup_propaganda.gif?
> >
> > have_tag wraps assert_select, which is part of the rails testing API.
> > You can find docs on it here:
> >
> >
> > http://api.rubyonrails.com/classes/ActionController/Assertions/SelectorAssertions.html#M000208
> >
> > For your example you can do this:
> >
> > response.should have_tag('img[src=?]','signup_propaganda.gif')
> >
> > Cheers,
> > David
> >
> >
> > >
> > >
> > > On 10/1/07, Andrew WC Brown <omen.king at gmail.com > wrote:
> > > > I solved it.
> > > >
> > > > The two problems were that I named my paths wrong and I was calling
> > > template instead of repsonse
> > > >
> > > >   it "should have a create games link for admin" do
> > > >    template.stub!(:logged_in?).and_return(true)
> > > >    template.stub!(:admin?).and_return(true)
> > > >    render "/games/index.rhtml"
> > > >    response.should have_tag('a','Create new game')
> > > >   end
> > > >
> > > > I'm stlll getting used to the whole spec'ing view things.
> > > > Its good to know my only problem is minor typo's
> > > >
> > > > I love RSPEC
> > > >
> > > >
> > > >
> > > > On 10/1/07, Andrew WC Brown < omen.king at gmail.com > wrote:
> > > > > Well I figured out why it didn't understand what the route was.
> > > > >
> > > > > it was new_games_path when it should have been new_game_path.
> > > > >
> > > > > Still not sure about the anchor tag
> > > > >
> > > > >
> > > > >
> > > > > On 10/1/07, Andrew WC Brown <omen.king at gmail.com > wrote:
> > > > > > I had reinstalled the plugin yesterday but I reinstalled it and
> > its
> > > revision 2680
> > > > > > It still gives me the error.
> > > > > >
> > > > > >
> > > > > >
> > > > > > On 10/1/07, David Chelimsky < dchelimsky at gmail.com> wrote:
> > > > > > > On 10/1/07, Andrew WC Brown < omen.king at gmail.com> wrote:
> > > > > > > > 1.0.9
> > > > > > >
> > > > > > > That's not been released, so you must be working from trunk. I
> > don't
> > > > > > > think, however, you have the latest trunk because I *think*
> > this has
> > > > > > > been fixed.
> > > > > > >
> > > > > > > Try updating (per
> > > > > > >
> > > http://rspec.rubyforge.org/documentation/rails/install.html
> > > near the
> > > > > > > bottom) and see if this problem goes away.
> > > > > > >
> > > > > > > Cheers,
> > > > > > > David
> > > > > > >
> > > > > > > >
> > > > > > > >
> > > > > > > > On 10/1/07, David Chelimsky <dchelimsky at gmail.com > wrote:
> > > > > > > > > On 10/1/07, Andrew WC Brown < omen.king at gmail.com> wrote:
> > > > > > > > > > How about spec'ing links?
> > > > > > > > > >
> > > > > > > > > > <%= link_to 'Create new game', new_games_path %>
> > > > > > > > > >
> > > > > > > > > >   it "should have a create games link for admin" do
> > > > > > > > > >    template.stub!(:logged_in?).and_return(true)
> > > > > > > > > >     template.stub!(:admin?).and_return(true)
> > > > > > > > > >     template.should have_tag('a','Create new game')
> > > > > > > > > >    render "/games/index.rhtml"
> > > > > > > > > >   end
> > > > > > > > > >
> > > > > > > > > > It says that it didn't show up
> > > > > > > > > >
> > > > > > > > > > 1)
> > > > > > > > > >  '/games/index.rhtml should have a create games link for
> >
> > > admin' FAILED
> > > > > > > > > > Expected at least 1 elements, found 0.
> > > > > > > > > > <false> is not true.
> > > > > > > > > > ./spec/views/games/index.rhtml_spec.rb:70:
> > > > > > > > > >
> > > > > > > > > > Also all specs have a problem with the named route
> > > > > > > > > >
> > > > > > > > > >  ActionView::TemplateError in '/games/index.rhtml should
> >
> > > render a list
> > > > > > > > of
> > > > > > > > > > games for authenticated users'
> > > > > > > > > >  undefined local variable or method `new_games_path' for
> >
> > > > > > > > > > #<#<Class:0x32bd2a4>:0x32bb5bc>
> > > > > > > > > >
> > > > > > > > > >
> > > > > > > > > > I am I suppose to stub the named route somehow?
> > > > > > > > > > since a link_to generates an anchor tag shouldn't of my
> > spec
> > > have
> > > > > > > > passed?
> > > > > > > > >
> > > > > > > > > What version of rspec/rspec_on_rails are you using?
> > > > > > > > >
> > > > > > > > > >
> > > > > > > > > >
> > > > > > > > > > On 10/1/07, David Chelimsky < dchelimsky at gmail.com>
> > wrote:
> > > > > > > > > > > On 10/1/07, Andrew WC Brown <omen.king at gmail.com >
> > wrote:
> > > > > > > > > > > > It didn't know what controller was, should it not
> > know it
> > > what it is
> > > > > > > > by
> > > > > > > > > > > > default or do I have to assign a controller at the
> > top of
> > > my spec?
> > > > > > > > > > >
> > > > > > > > > > > Try template instead, or @controller.
> > > > > > > > > > >
> > > > > > > > > > > The controller used in view specs is a generic
> > controller
> > > that ships
> > > > > > > > > > > w/ rspec_on_rails, not the controller that is mapped
> > to the
> > > view.
> > > > > > > > > > >
> > > > > > > > > > > >
> > > > > > > > > > > >
> > > > > > > > > > > > On 10/1/07, Andrew WC Brown < omen.king at gmail.com >
> > wrote:
> > > > > > > > > > > > > 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
> > > > > > > > > > > > > >
> > > > > > > > > > > > >
> > > > > > > > > > > > >
> > > > > > > > > > > >
> > > > > > > > > > > >
> > > _______________________________________________
> > > > > > > > > > > > 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
> > > > > > > > > > >
> > > > > > > > > >
> > > > > > > > > >
> > > > > > > > > >
> > > > > > > > > >
> > > _______________________________________________
> > > > > > > > > > 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
> > > > > > > > >
> > > > > > > >
> > > > > > > >
> > > > > > > >
> > > > > > > > _______________________________________________
> > > > > > > > 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
> > > > > > >
> > > > > >
> > > > > >
> > > > >
> > > >
> > >
> > >
> > > _______________________________________________
> > > 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
> >
>
>


-- 
Monsterbox Productions
putting small businesses on-line

1319 Victoria Avenue East
Thunder Bay, Ontario P7C 1C3
Canada

Andrew WC Brown
web-developer and owner
andrew at monsterboxpro.com
P: 807-626-9009
F: 807-624-2705
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://rubyforge.org/pipermail/rspec-users/attachments/20071001/5659b695/attachment-0001.html 


More information about the rspec-users mailing list