<br><br><div class="gmail_quote">On Dec 17, 2007 8:43 AM, David Chelimsky <<a href="mailto:dchelimsky@gmail.com">dchelimsky@gmail.com</a>> 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 <<a href="mailto:jonathan@parkerhill.com">jonathan@parkerhill.com</a>> wrote:<br>><br>> btw, to answer the same question for member actions (such as show), you can
<br>> stub or assert the association proxy class<br>><br>> @article = current_user.articles.find(params[:id])<br>><br>> then,<br>><br>> it "should find article scoped by current_user" do<br>
> article = mock_model(Article)<br>> current_user.should_receive(:articles).and_return(Article)<br>> Article.should_receive(:find).and_return(article)<br>> get :show, :id => <a href="http://article.id" target="_blank">
article.id</a><br>> end<br>><br>> this will ensure that your controller is not calling Article.find unscoped<br><br></div>I tend to take this one more step. I'm a bit of a demeter zealot, and<br>I'm also usually writing the examples from the code first. So I'd do
<br>something like:<br><br>it "should find the current users articles" do<br> article = mock_model(Article)<br> current_user.should_receive(:find_article).with("1").and_return(Article)<br> get :show, :id => "1"
<br>end</blockquote><div><br>Where is current_user defined here? I've always stubbed current_user on the controller object. How are you doing this one?<br><br> </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> it "should find it's own articles" do<br> user = User.new<br> user.articles.should_receive(:find).with("1")
<br> user.find_article("1)<br> 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'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>><br>> linoj<br>><br>><br>><br>><br>> On Dec 3, 2007, at 5:44 AM, Daniel N wrote:
<br>> Assuming that there is a call like this in your controller<br>><br>> @articles = current_user.articles<br>><br>> One way to do this is to stub out the controller.current_user to return a<br>> mock object of the current_user
<br>><br>> Then put an expectation on the current user that it's articles method gets<br>> called. (return a mocked collection of articles)<br>><br>> Then check that @articles is set to the returned mocked collection of
<br>> articles from current_user.articles<br>><br>> phew...<br>><br>> Ok So one way you might write this could be (This is untested...)<br>><br>> it "should scope the articles to the currrent_user" do
<br>><br>> user = mock_model(User)<br>> articles = [mock_model(Article)]<br>><br>> controller.stub!(:current_user).and_return(user)<br>> user.should_receive (:articles).and_return(articles)<br>>
<br>> get :index<br>><br>> assigns[:articles].should == articles<br>><br>> end<br>><br>><br>> Like I said though, that's not tested itself. If that's not exactly<br>> right... it's along the right track of an option that can work.
<br>><br>> HTH<br>> Daniel<br>><br>> On Dec 3, 2007 9:07 PM, Stefan Magnus Landrø <<a href="mailto:stefan.landro@gmail.com">stefan.landro@gmail.com</a>><br>> wrote:<br>> > Typically, I'd write a method in your user model that returns the user's
<br>> 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<br>> your method is being called.<br>> ><br>> > On the other hand, the user model should be tested directly against the
<br>> db.<br>> ><br>> > HTH,<br>> ><br>> > Stefan<br>> ><br>> ><br>> > 2007/12/3, Fischer, Daniel <<a href="mailto:daniel@helpmebuyacar.org">daniel@helpmebuyacar.org</a>>:
<br>> > ><br>> > ><br>> > ><br>> > > Let's say you're using the restful_authentication plugin.<br>> > ><br>> > ><br>> > > You have a model called articles. On the index action of the
<br>> articlescontroller you simply want to spec out that it'll scope the results<br>> to the ownership of the current_user.<br>> > ><br>> > ><br>> > > It should NOT include any articles other than the articles that user
<br>> owns.<br>> > ><br>> > ><br>> > > How would you properly spec this out?<br>> > ><br>> > ><br>> > > Thanks for the help!<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>> > ><br>> ><br>> ><br>> ><br>> > --<br>> > Bekk Open Source<br>> > <a href="http://boss.bekk.no" target="_blank">http://boss.bekk.no
</a><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>> ><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" target="_blank">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" target="_blank">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" target="_blank">
http://rubyforge.org/mailman/listinfo/rspec-users</a><br></div></div></blockquote></div><br>