[rspec-users] Need help mocking this out

Fischer, Daniel me at danielfischer.com
Tue Dec 4 01:40:55 EST 2007


Sorry for so many messages, I hope I don't get in trouble for this. Maybe
IRC would be better if there was a RSpec one.
Anyway, the previous problem was solved with the following
http://pastie.textmate.org/private/m6qqfd7tzeanw2yar8rua

The problem was caused by :

    @user = mock_model(User, :writings => [] )

I'm not sure if that is a bug or what, but that's what caused it.

It'd also say "no block given" if I put something there other than a "mock".

On Dec 3, 2007 10:33 PM, Fischer, Daniel <me at danielfischer.com> wrote:

> Alright, thanks, I'm getting more progress in this. As soon as I figure
> this out I won't have too many problems.. hopefully.
> Anyone know what "undefined method `call' for "1":String" means?
>
> I'm trying to do this:
>
> http://pastie.textmate.org/private/17jjjmbave0ph2mkcgp6w
>
>
> On Dec 3, 2007 9:33 PM, David Chelimsky <dchelimsky at gmail.com> wrote:
>
> > On Dec 3, 2007 10:26 PM, Fischer, Daniel <me at danielfischer.com> wrote:
> > > Hey cool, thanks for the help guys. One problem though, when I take
> > this
> > > approach I can't decouple the specs anymore. They all "User_xxx
> > receive
> > > unexpected message :articles". It seems silly to include all behaviors
> > in
> > > one spec, or put that expectation in each test. Is there a way around
> > this?
> >
> > Sure. Create a method somewhere that generates a baseline stub for you.
> >
> > module UserControllerSpecHelper
> >  def create_stub_user
> >    mock_model(User, :articles => [])
> >  end
> > end
> >
> > describe UserController, "..." do
> >  include UserControllerSpecHelper
> >  before(:each) do
> >    @user = create_stub_user
> >  end
> > end
> >
> > If there are other methods  you need to stub on all instances of user,
> > do it in the helper. Then you can use message expectations (mocks)
> > where you need them to describe specific behaviour.
> >
> > HTH,
> > David
> >
> > >
> > > Thanks for all the help,
> > > Daniel Fischer
> > > http://www.danielfischer.com
> > >
> > >
> > >
> > > On Dec 3, 2007 2:44 AM, Daniel N < has.sox at gmail.com> wrote:
> > > > Assuming that there is a call like this in your controller
> > > >
> > > >
> > > > @articles = current_user.articles
> > > >
> > > >
> > > > One way to do this is to stub out the controller.current_user to
> > return a
> > > mock object of the current_user
> > > >
> > > >
> > > > Then put an expectation on the current user that it's articles
> > method gets
> > > called. (return a mocked collection of articles)
> > > >
> > > >
> > > > Then check that @articles is set to the returned mocked collection
> > of
> > > articles from current_user.articles
> > > >
> > > >
> > > > phew...
> > > >
> > > >
> > > > Ok So one way you might write this could be (This is untested...)
> > > >
> > > >
> > > > it "should scope the articles to the currrent_user" do
> > > >
> > > >
> > > >   user = mock_model(User)
> > > >   articles = [mock_model(Article)]
> > > >
> > > >
> > > >   controller.stub!(:current_user).and_return(user)
> > > >
> > > >   user.should_receive (:articles).and_return(articles)
> > > >
> > > >   get :index
> > > >
> > > >   assigns[:articles].should == articles
> > > >
> > > >
> > > > end
> > > >
> > > >
> > > > 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.
> > > >
> > > >
> > > > HTH
> > > > Daniel
> > > >
> > > >
> > > >
> > > > On Dec 3, 2007 9:07 PM, Stefan Magnus Landrø <stefan.landro at gmail.com
> > >
> > > wrote:
> > > >
> > > >
> > > >
> > > >
> > > > > Typically, I'd write a method in your user model that returns the
> > user's
> > > articles:
> > > > >
> > > > > class User do
> > > > >
> > > > >   def find_articles_for_user
> > > > >    Article.find(:all, :conditions => ['userid = ?', id)
> > > > >   end
> > > > >
> > > > > end
> > > > >
> > > > > Then you'd use a mock in your controller spec, and make sure you
> > test
> > > that your method is being called.
> > > > >
> > > > > On the other hand, the user model should be tested directly
> > against the
> > > db.
> > > > >
> > > > > HTH,
> > > > >
> > > > > Stefan
> > > > >
> > > > >
> > > > > 2007/12/3, Fischer, Daniel < daniel at helpmebuyacar.org>:
> > > > > >
> > > > > >
> > > > > >
> > > > > > Let's say you're using the restful_authentication plugin.
> > > > > >
> > > > > >
> > > > > > 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.
> > > > > >
> > > > > >
> > > > > > It should NOT include any articles other than the articles that
> > user
> > > owns.
> > > > > >
> > > > > >
> > > > > > How would you properly spec this out?
> > > > > >
> > > > > >
> > > > > > Thanks for the help!
> > > > > > _______________________________________________
> > > > > > rspec-users mailing list
> > > > > > rspec-users at rubyforge.org
> > > > > > http://rubyforge.org/mailman/listinfo/rspec-users
> > > > > >
> > > > >
> > > > >
> > > > >
> > > > > --
> > > > > Bekk Open Source
> > > > > http://boss.bekk.no
> > > > > _______________________________________________
> > > > > rspec-users mailing list
> > > > > rspec-users at rubyforge.org
> > > > > http://rubyforge.org/mailman/listinfo/rspec-users
> > > > >
> > > >
> > > >
> > > > _______________________________________________
> > > > rspec-users mailing list
> > > > rspec-users at rubyforge.org
> > > > http://rubyforge.org/mailman/listinfo/rspec-users
> > > >
> > >
> > >
> > > _______________________________________________
> > > rspec-users mailing list
> > > rspec-users at rubyforge.org
> > > http://rubyforge.org/mailman/listinfo/rspec-users
> > >
> > _______________________________________________
> > rspec-users mailing list
> > rspec-users at rubyforge.org
> > http://rubyforge.org/mailman/listinfo/rspec-users
> >
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://rubyforge.org/pipermail/rspec-users/attachments/20071203/83b97e7b/attachment-0001.html 


More information about the rspec-users mailing list