<html><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; ">
<div>btw, to answer the same question for member actions (such as show), you can stub or assert the association proxy class</div><div><br class="webkit-block-placeholder"></div><div><span class="Apple-tab-span" style="white-space:pre">        </span>@article = current_user.articles.find(params[:id])<br class="webkit-block-placeholder"></div><div><br class="webkit-block-placeholder"></div><div>then,</div><div><br class="webkit-block-placeholder"></div><div>it "should find article scoped by current_user" do</div><div><span class="Apple-tab-span" style="white-space:pre">        </span>article = mock_model(Article)<br class="webkit-block-placeholder"></div><div><span class="Apple-tab-span" style="white-space:pre">        </span>current_user.should_receive(:articles).and_return(Article)<br class="webkit-block-placeholder"></div><div><span class="Apple-tab-span" style="white-space:pre">        </span>Article.should_receive(:find).and_return(article)<br class="webkit-block-placeholder"></div><div><span class="Apple-tab-span" style="white-space:pre">        </span>get :show, :id => article.id<br class="webkit-block-placeholder"></div><div>end</div><div><br class="webkit-block-placeholder"></div><div>this will ensure that your controller is not calling Article.find unscoped</div><div><br class="webkit-block-placeholder"></div><div>linoj</div><div><br><div><br><div><div>On Dec 3, 2007, at 5:44 AM, Daniel N wrote:</div><br class="Apple-interchange-newline"><blockquote type="cite">Assuming that there is a call like this in your controller<div><br class="webkit-block-placeholder"></div><div>@articles = current_user.articles</div><div><br class="webkit-block-placeholder"></div><div>One way to do this is to stub out the controller.current_user to return a mock object of the current_user</div><div><br class="webkit-block-placeholder"></div><div>Then put an expectation on the current user that it's articles method gets called. (return a mocked collection of articles) </div><div><br class="webkit-block-placeholder"></div><div>Then check that @articles is set to the returned mocked collection of articles from current_user.articles</div><div><br class="webkit-block-placeholder"></div><div> phew...</div><div><br class="webkit-block-placeholder"></div><div>Ok So one way you might write this could be (This is untested...)</div><div><br class="webkit-block-placeholder"></div><div>it "should scope the articles to the currrent_user" do </div><div><br></div><div> user = mock_model(User)</div><div> articles = [mock_model(Article)]</div><div><br class="webkit-block-placeholder"></div><div> controller.stub!(:current_user).and_return(user)</div><div> user.should_receive (:articles).and_return(articles)</div><div> </div><div> get :index</div><div> </div><div> assigns[:articles].should == articles</div><div><br class="webkit-block-placeholder"></div><div>end</div><div><br class="webkit-block-placeholder"> </div><div>Like I said though, that's not tested itself. If that's not exactly right... it's along the right track of an option that can work.</div><div><br class="webkit-block-placeholder"></div><div>HTH</div> <div>Daniel</div><div><br><div class="gmail_quote">On Dec 3, 2007 9:07 PM, Stefan Magnus Landrĝ <<a href="mailto:stefan.landro@gmail.com">stefan.landro@gmail.com</a>> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;"> Typically, I'd write a method in your user model that returns the user's articles:<br><br>class User do<br><br> def find_articles_for_user<br> Article.find(:all, :conditions => ['userid = ?', id)<br> end <br><br>end<br><br>Then you'd use a mock in your controller spec, and make sure you test that your method is being called.<br><br>On the other hand, the user model should be tested directly against the db.<br><br>HTH, <br><br>Stefan<br><br><div><span class="gmail_quote">2007/12/3, Fischer, Daniel <<a href="mailto:daniel@helpmebuyacar.org" target="_blank">daniel@helpmebuyacar.org</a>>:</span><blockquote class="gmail_quote" style="border-left:1px solid rgb(204, 204, 204);margin:0pt 0pt 0pt 0.8ex;padding-left:1ex"> <div><div></div><div class="Wj3C7c"> Let's say you're using the restful_authentication plugin.<div><br></div><div>You have a model called articles. On the index action of the articlescontroller you simply want to spec out that it'll scope the results to the ownership of the current_user. </div><div><br></div><div>It should NOT include any articles other than the articles that user owns.</div><div><br></div><div>How would you properly spec this out? </div><div><br></div><div>Thanks for the help!</div></div></div> <br>_______________________________________________<br>rspec-users mailing list<br><a href="mailto:rspec-users@rubyforge.org" target="_blank">rspec-users@rubyforge.org</a><br><a href="http://rubyforge.org/mailman/listinfo/rspec-users" target="_blank"> http://rubyforge.org/mailman/listinfo/rspec-users</a><br></blockquote></div><font color="#888888"><br><br clear="all"><br>-- <br>Bekk Open Source<br><a href="http://boss.bekk.no" target="_blank">http://boss.bekk.no</a> </font><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" target="_blank"> http://rubyforge.org/mailman/listinfo/rspec-users</a><br></blockquote></div><br></div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">_______________________________________________</div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">rspec-users mailing list</div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "><a href="mailto:rspec-users@rubyforge.org">rspec-users@rubyforge.org</a></div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "><a href="http://rubyforge.org/mailman/listinfo/rspec-users">http://rubyforge.org/mailman/listinfo/rspec-users</a></div> </blockquote></div><br></div></div></body></html>