How about spec'ing links?<br><br><%= link_to 'Create new game', new_games_path %><br><br> it "should have a create games link for admin" do<br> template.stub!(:logged_in?).and_return(true)<br>
template.stub!(:admin?).and_return(true)<br> template.should have_tag('a','Create new game')<br> render "/games/index.rhtml"<br> end <br><br>It says that it didn't show up<br><br>1)<br>
'/games/index.rhtml should have a create games link for admin' FAILED<br>Expected at least 1 elements, found 0.<br><false> is not true.<br>./spec/views/games/index.rhtml_spec.rb:70:<br><br>Also all specs have a problem with the named route
<br>
<br>
ActionView::TemplateError in '/games/index.rhtml should render a list of games for authenticated users'<br>
undefined local variable or method `new_games_path' for #<#<Class:0x32bd2a4>:0x32bb5bc><br><br><br>I am I suppose to stub the named route somehow?<br>since a link_to generates an anchor tag shouldn't of my spec have passed?
<br><br><div><span class="gmail_quote">On 10/1/07, <b class="gmail_sendername">David Chelimsky</b> <<a href="mailto:dchelimsky@gmail.com">dchelimsky@gmail.com</a>> wrote:</span><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
On 10/1/07, Andrew WC Brown <<a href="mailto:omen.king@gmail.com">omen.king@gmail.com</a>> wrote:<br>> It didn't know what controller was, should it not know it what it is by<br>> default or do I have to assign a controller at the top of my spec?
<br><br>Try template instead, or @controller.<br><br>The controller used in view specs is a generic controller that ships<br>w/ rspec_on_rails, not the controller that is mapped to the view.<br><br>><br>><br>> On 10/1/07, Andrew WC Brown <
<a href="mailto:omen.king@gmail.com">omen.king@gmail.com</a>> wrote:<br>> > The was really helpful, thanks David!<br>> ><br>> > "There is no simple answer to your question. If anyone offers you one,
<br>> > treat it with a grain of salt."<br>> ><br>> > The game I'm specing actually has an attribute called grains_of_salt.<br>> > No Lie.<br>> ><br>> ><br>> ><br>> >
<br>> > On 10/1/07, David Chelimsky < <a href="mailto:dchelimsky@gmail.com">dchelimsky@gmail.com</a>> wrote:<br>> > > On 10/1/07, Andrew WC Brown <<a href="mailto:omen.king@gmail.com">omen.king@gmail.com
</a>> wrote:<br>> > > > I'm trying to spec a view but haven't done much view specing.<br>> > > ><br>> > > > This view render different partials depending on authentication of the
<br>> user:<br>> > > > annon, admin, player<br>> > > > So I I'll write if conditionals in the view with the partials<br>> > > ><br>> > > ><br>> > > > it "should render signup propaganda for annon users trying to view
<br>> games"<br>> > > > do<br>> > > > render "/games/index.rhtml"<br>> > > > @logged_in?.should eql(false)<br>> > > > response.should<br>> render_template('_signup_propaganda')
<br>> > > > end<br>> > > ><br>> > > > Now for my partial I know it'll be wrapped all in a div with a<br>> > > > class="signup_propaganda"<br>> > > > Should I be testing for that instead? Can I write expectations for
<br>> partials<br>> > > > similar to above?<br>> > > ><br>> > > > When your specing views are you testing for the outputted results?<br>> > > ><br>> > > > it "should render signup propaganda for annon users trying to view
<br>> games"<br>> > > > do<br>> > > > render "/games/index.rhtml"<br>> > > > @logged_in?.should eql(false)<br>> > > > response.should have_tag(div, "class=/"signup_propaganda/"")
<br>> > > > end<br>> > > ><br>> > > > How should I be writing my spec?<br>> > ><br>> > > There is no simple answer to your question. If anyone offers you one,<br>> > > treat it with a grain of salt.
<br>> > ><br>> > > Coding by example is a process. If you're doing it right, the examples<br>> > > are going to change as you progress. So in this case, I might start<br>> > > like this:
<br>> > ><br>> > > it "should render signup propaganda for annon users trying to view<br>> games" do<br>> > > controller.stub!(:logged_in?).and_return(false)<br>> > > render "/games/index.rhtml"
<br>> > > response.should have_tag('div.signup_propaganda')<br>> > > end<br>> > ><br>> > > The code to make this pass could just be:<br>> > ><br>> > > <div class='signup_propoganda'/>
<br>> > ><br>> > > At this point I'd want to add an example about what a logged in user<br>> > > sees to force the conditional:<br>> > ><br>> > > it "should NOT render signup propaganda for logged in users trying to
<br>> > > view games" do<br>> > > controller.stub!(:logged_in?).and_return(true)<br>> > > render "/games/index.rhtml"<br>> > > response.should_not have_tag('div.signup_propaganda'
)<br>> > > end<br>> > ><br>> > > leading to this code:<br>> > ><br>> > > <% if logged_in? %><br>> > > <div class='signup_propoganda'/><br>> > > <% end %>
<br>> > ><br>> > > At some point down the line I might decide to extract the div to a<br>> > > partial. At *that* point, I should be able to do so without changing<br>> > > the example. Once the partial has been extracted, then comes the
<br>> > > question about what to do with the example, and the answer will depend<br>> > > on a few things.<br>> > ><br>> > > If the partial is only ever used in this one template, and requires no
<br>> > > additional setup, and the only reason I extracted it was to clean up<br>> > > the template, I might leave things as/is.<br>> > ><br>> > > Most of the time, however, I'd change the examples to expect that the
<br>> > > partial gets rendered. First, I'd create a new example for the partial<br>> > > itself and move anything from the old example that was specific to the<br>> > > content inside that partial. Only after that's done and all examples
<br>> > > are passing, I'd change the original examples to look like this:<br>> > ><br>> > > it "should render signup propaganda for annon users trying to view<br>> games" do<br>
> > > controller.stub!(:logged_in?).and_return(false)<br>> > > template.expect_render(:partial => 'signup_propoganda')<br>> > > render "/games/index.rhtml"<br>> > > end
<br>> > ><br>> > > it "should NOT render signup propaganda for logged in users trying to<br>> > > view games" do<br>> > > controller.stub!(:logged_in?).and_return(true)<br>> > >
template.expect_render (:partial => 'signup_propoganda').never<br>> > > render "/games/index.rhtml"<br>> > > end<br>> > ><br>> > > HTH,<br>> > > David<br>
> > ><br>> > > ><br>> > > > _______________________________________________<br>> > > > rspec-users mailing list<br>> > > > <a href="mailto:rspec-users@rubyforge.org">
rspec-users@rubyforge.org</a><br>> > > > <a href="http://rubyforge.org/mailman/listinfo/rspec-users">http://rubyforge.org/mailman/listinfo/rspec-users</a><br>> > > ><br>> > > _______________________________________________
<br>> > > rspec-users mailing list<br>> > > <a href="mailto:rspec-users@rubyforge.org">rspec-users@rubyforge.org</a><br>> > > <a href="http://rubyforge.org/mailman/listinfo/rspec-users">http://rubyforge.org/mailman/listinfo/rspec-users
</a><br>> > ><br>> ><br>> ><br>><br>> _______________________________________________<br>> rspec-users mailing list<br>> <a href="mailto:rspec-users@rubyforge.org">rspec-users@rubyforge.org
</a><br>> <a href="http://rubyforge.org/mailman/listinfo/rspec-users">http://rubyforge.org/mailman/listinfo/rspec-users</a><br>><br>_______________________________________________<br>rspec-users mailing list<br><a href="mailto:rspec-users@rubyforge.org">
rspec-users@rubyforge.org</a><br><a href="http://rubyforge.org/mailman/listinfo/rspec-users">http://rubyforge.org/mailman/listinfo/rspec-users</a><br></blockquote></div><br><br>