<br><br><div class="gmail_quote">On Dec 17, 2007 8:43 AM, David Chelimsky &lt;<a href="mailto:dchelimsky@gmail.com">dchelimsky@gmail.com</a>&gt; wrote:<br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
<div class="Ih2E3d">On Dec 16, 2007 11:03 AM, Jonathan Linowes &lt;<a href="mailto:jonathan@parkerhill.com">jonathan@parkerhill.com</a>&gt; wrote:<br>&gt;<br>&gt; btw, to answer the same question for member actions (such as show), you can
<br>&gt; stub or assert the association proxy class<br>&gt;<br>&gt; &nbsp;@article = current_user.articles.find(params[:id])<br>&gt;<br>&gt; then,<br>&gt;<br>&gt; it &quot;should find article scoped by current_user&quot; do<br>
&gt; &nbsp;article = mock_model(Article)<br>&gt; &nbsp;current_user.should_receive(:articles).and_return(Article)<br>&gt; &nbsp;Article.should_receive(:find).and_return(article)<br>&gt; &nbsp;get :show, :id =&gt; <a href="http://article.id" target="_blank">
article.id</a><br>&gt; end<br>&gt;<br>&gt; this will ensure that your controller is not calling Article.find unscoped<br><br></div>I tend to take this one more step. I&#39;m a bit of a demeter zealot, and<br>I&#39;m also usually writing the examples from the code first. So I&#39;d do
<br>something like:<br><br>it &quot;should find the current users articles&quot; do<br> &nbsp;article = mock_model(Article)<br> &nbsp;current_user.should_receive(:find_article).with(&quot;1&quot;).and_return(Article)<br> &nbsp;get :show, :id =&gt; &quot;1&quot;
<br>end</blockquote><div><br>Where is current_user defined here?&nbsp; I&#39;ve always stubbed current_user on the controller object.&nbsp; How are you doing this one?<br><br>&nbsp;</div><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
<br><br>This would lead me to add a find_article method to User:<br><br>describe User do<br> &nbsp;it &quot;should find it&#39;s own articles&quot; do<br> &nbsp; &nbsp;user = User.new<br> &nbsp; &nbsp;user.articles.should_receive(:find).with(&quot;1&quot;)
<br> &nbsp; &nbsp;user.find_article(&quot;1)<br> &nbsp;end<br>end<br><br>This has two benefits:<br>- each example is simpler and focuses on one object and how it behaves<br>- I can change the relationship between a User and it&#39;s Articles
<br>without changing the controller code.<br><br>FWIW,<br><font color="#888888">David<br></font><div><div></div><div class="Wj3C7c"><br><br>&gt;<br>&gt; linoj<br>&gt;<br>&gt;<br>&gt;<br>&gt;<br>&gt; On Dec 3, 2007, at 5:44 AM, Daniel N wrote:
<br>&gt; Assuming that there is a call like this in your controller<br>&gt;<br>&gt; @articles = current_user.articles<br>&gt;<br>&gt; One way to do this is to stub out the controller.current_user to return a<br>&gt; mock object of the current_user
<br>&gt;<br>&gt; Then put an expectation on the current user that it&#39;s articles method gets<br>&gt; called. (return a mocked collection of articles)<br>&gt;<br>&gt; Then check that @articles is set to the returned mocked collection of
<br>&gt; articles from current_user.articles<br>&gt;<br>&gt; &nbsp;phew...<br>&gt;<br>&gt; Ok So one way you might write this could be (This is untested...)<br>&gt;<br>&gt; it &quot;should scope the articles to the currrent_user&quot; do
<br>&gt;<br>&gt; &nbsp; user = mock_model(User)<br>&gt; &nbsp; articles = [mock_model(Article)]<br>&gt;<br>&gt; &nbsp; controller.stub!(:current_user).and_return(user)<br>&gt; &nbsp; user.should_receive (:articles).and_return(articles)<br>&gt;
<br>&gt; &nbsp; get :index<br>&gt;<br>&gt; &nbsp; assigns[:articles].should == articles<br>&gt;<br>&gt; end<br>&gt;<br>&gt;<br>&gt; Like I said though, that&#39;s not tested itself. &nbsp;If that&#39;s not exactly<br>&gt; right... it&#39;s along the right track of an option that can work.
<br>&gt;<br>&gt; HTH<br>&gt; Daniel<br>&gt;<br>&gt; On Dec 3, 2007 9:07 PM, Stefan Magnus Landrø &lt;<a href="mailto:stefan.landro@gmail.com">stefan.landro@gmail.com</a>&gt;<br>&gt; wrote:<br>&gt; &gt; Typically, I&#39;d write a method in your user model that returns the user&#39;s
<br>&gt; articles:<br>&gt; &gt;<br>&gt; &gt; class User do<br>&gt; &gt;<br>&gt; &gt; &nbsp; def find_articles_for_user<br>&gt; &gt; &nbsp; &nbsp;Article.find(:all, :conditions =&gt; [&#39;userid = ?&#39;, id)<br>&gt; &gt; &nbsp; end<br>&gt; &gt;
<br>&gt; &gt; end<br>&gt; &gt;<br>&gt; &gt; Then you&#39;d use a mock in your controller spec, and make sure you test that<br>&gt; your method is being called.<br>&gt; &gt;<br>&gt; &gt; On the other hand, the user model should be tested directly against the
<br>&gt; db.<br>&gt; &gt;<br>&gt; &gt; HTH,<br>&gt; &gt;<br>&gt; &gt; Stefan<br>&gt; &gt;<br>&gt; &gt;<br>&gt; &gt; 2007/12/3, Fischer, Daniel &lt;<a href="mailto:daniel@helpmebuyacar.org">daniel@helpmebuyacar.org</a>&gt;:
<br>&gt; &gt; &gt;<br>&gt; &gt; &gt;<br>&gt; &gt; &gt;<br>&gt; &gt; &gt; Let&#39;s say you&#39;re using the restful_authentication plugin.<br>&gt; &gt; &gt;<br>&gt; &gt; &gt;<br>&gt; &gt; &gt; You have a model called articles. On the index action of the
<br>&gt; articlescontroller you simply want to spec out that it&#39;ll scope the results<br>&gt; to the ownership of the current_user.<br>&gt; &gt; &gt;<br>&gt; &gt; &gt;<br>&gt; &gt; &gt; It should NOT include any articles other than the articles that user
<br>&gt; owns.<br>&gt; &gt; &gt;<br>&gt; &gt; &gt;<br>&gt; &gt; &gt; How would you properly spec this out?<br>&gt; &gt; &gt;<br>&gt; &gt; &gt;<br>&gt; &gt; &gt; Thanks for the help!<br>&gt; &gt; &gt; _______________________________________________
<br>&gt; &gt; &gt; rspec-users mailing list<br>&gt; &gt; &gt; <a href="mailto:rspec-users@rubyforge.org">rspec-users@rubyforge.org</a><br>&gt; &gt; &gt; <a href="http://rubyforge.org/mailman/listinfo/rspec-users" target="_blank">
http://rubyforge.org/mailman/listinfo/rspec-users</a><br>&gt; &gt; &gt;<br>&gt; &gt;<br>&gt; &gt;<br>&gt; &gt;<br>&gt; &gt; --<br>&gt; &gt; Bekk Open Source<br>&gt; &gt; <a href="http://boss.bekk.no" target="_blank">http://boss.bekk.no
</a><br>&gt; &gt; _______________________________________________<br>&gt; &gt; rspec-users mailing list<br>&gt; &gt; <a href="mailto:rspec-users@rubyforge.org">rspec-users@rubyforge.org</a><br>&gt; &gt; <a href="http://rubyforge.org/mailman/listinfo/rspec-users" target="_blank">
http://rubyforge.org/mailman/listinfo/rspec-users</a><br>&gt; &gt;<br>&gt;<br>&gt; _______________________________________________<br>&gt; rspec-users mailing list<br>&gt; <a href="mailto:rspec-users@rubyforge.org">rspec-users@rubyforge.org
</a><br>&gt; <a href="http://rubyforge.org/mailman/listinfo/rspec-users" target="_blank">http://rubyforge.org/mailman/listinfo/rspec-users</a><br>&gt;<br>&gt; _______________________________________________<br>&gt; rspec-users mailing list
<br>&gt; <a href="mailto:rspec-users@rubyforge.org">rspec-users@rubyforge.org</a><br>&gt; <a href="http://rubyforge.org/mailman/listinfo/rspec-users" target="_blank">http://rubyforge.org/mailman/listinfo/rspec-users</a><br>
&gt;<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></div></div></blockquote></div><br>