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><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>> 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 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 games"<br>> do
<br>> render "/games/index.rhtml"<br>> @logged_in?.should eql(false)<br>> response.should 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 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 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 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 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></blockquote></div><br>