[rspec-users] Could anyone please help with rspec/nested resource behavior checking?

Fischer, Daniel daniel at danielfischer.com
Fri Jun 1 00:09:21 EDT 2007


Alright so I removed the 'nested' resources, but I still have another
problem. Most of my failures are gone, but I still have three left:


1)
Spec::Mocks::MockExpectationError in 'PostsController handling GET
/posts/1;edit should find the posts requested'
Post expected :find with (any args) once, but received it twice
./spec/controllers/posts_controller_spec.rb:217:

2)
Spec::Mocks::MockExpectationError in 'PostsController handling POST
/posts should create a new posts'
Mock 'Post_1027' received unexpected message :user_id= with (202018)
/Users/sparta/Projects/work/idastudios/podff_machine/config/../app/controllers/posts_controller.rb:36:in
`create'
/Users/sparta/Projects/work/idastudios/podff_machine/config/../vendor/plugins/haml/lib/sass/plugin.rb:116:in
`process_without_test'
./spec/controllers/posts_controller_spec.rb:250:in `do_post'
./spec/controllers/posts_controller_spec.rb:256:

3)
Spec::Mocks::MockExpectationError in 'PostsController handling POST
/posts should redirect to the new posts'
Mock 'Post_1028' received unexpected message :user_id= with (202018)
/Users/sparta/Projects/work/idastudios/podff_machine/config/../app/controllers/posts_controller.rb:36:in
`create'
/Users/sparta/Projects/work/idastudios/podff_machine/config/../vendor/plugins/haml/lib/sass/plugin.rb:116:in
`process_without_test'
./spec/controllers/posts_controller_spec.rb:250:in `do_post'
./spec/controllers/posts_controller_spec.rb:260:

Finished in 0.697209 seconds

38 examples, 3 failures

I don't understand the first one, but I do have an idea on the
second/third one. I am setting the @post.user_id to the current user
in the controller, I'm trying to mock that behavior but whatever I try
doesn't work. What would be the proper way? Right now I am doing this:


  it "should create a new posts" do
    Post.should_receive(:user_id).and_return(1)
    Post.should_receive(:new).with({'name' => 'Post'}).and_return(@post)
    do_post
  end

still fails with that message though :(
On 5/31/07, Fischer, Daniel <daniel at danielfischer.com> wrote:
> David,
>
> Awesome - well at least it's telling me something isn't working "that well"
> anymore.
>
> Instead of the previous "expected success? to return true, got false"
>
> It's now "Couldn't find User with ID=1"
>
> But I swore that same code worked before, it looks proper in my pastie
> doesn't it?
>
> *scratches head* - it certainly works when it's "live".
>
> On 5/31/07, David Chelimsky <dchelimsky at gmail.com> wrote:
> > On 5/31/07, Fischer, Daniel <daniel at danielfischer.com> wrote:
> > > 1)
> > > 'PostsController handling GET /users/1/posts should be successful'
> FAILED
> > > expected success? to return true, got false
> > >
> /usr/local/lib/ruby/gems/1.8/gems/rspec-1.0.2/lib/spec/expectations.rb:52:in
> > > `fail_with'
> > >
> /usr/local/lib/ruby/gems/1.8/gems/rspec-1.0.2/lib/spec/expectations/handler.rb:16:in
> > > `handle_matcher'
> > >
> /usr/local/lib/ruby/gems/1.8/gems/rspec-1.0.2/lib/spec/expectations/extensions/object.rb:32:in
> > > `should'
> > >  ./spec/controllers/posts_controller_spec.rb:46:
> > > /usr/local/lib/ruby/gems/1.8/gems/rspec-
> 1.0.2/lib/spec/dsl/example.rb:73:in
> > > `instance_eval'
> > >
> /usr/local/lib/ruby/gems/1.8/gems/rspec-1.0.2/lib/spec/dsl/example.rb:73:in
> > > `run_example'
> > > /usr/local/lib/ruby/gems/1.8/gems/rspec-
> 1.0.2/lib/spec/dsl/example.rb:23:in
> > > `run'
> > > /usr/local/lib/ruby/1.8/timeout.rb:48:in `timeout'
> > >
> /usr/local/lib/ruby/gems/1.8/gems/rspec-1.0.2/lib/spec/dsl/example.rb:21:in
> > > `run'
> > >
> /usr/local/lib/ruby/gems/1.8/gems/rspec-1.0.2/lib/spec/dsl/behaviour.rb:72:in
> > > `run'
> > >
> /usr/local/lib/ruby/gems/1.8/gems/rspec-1.0.2/lib/spec/dsl/behaviour.rb:69:in
> > > `each'
> > > /usr/local/lib/ruby/gems/1.8/gems/rspec-
> > > 1.0.2/lib/spec/dsl/behaviour.rb:69:in `run'
> > >
> /usr/local/lib/ruby/gems/1.8/gems/rspec-1.0.2/lib/spec/runner/behaviour_runner.rb:45:in
> > > `run_behaviours'
> > > /usr/local/lib/ruby/gems/1.8/gems/rspec-
> 1.0.2/lib/spec/runner/behaviour_runner.rb:44:in
> > > `each'
> > >
> /usr/local/lib/ruby/gems/1.8/gems/rspec-1.0.2/lib/spec/runner/behaviour_runner.rb:44:in
> > > `run_behaviours'
> > > /usr/local/lib/ruby/gems/1.8/gems/rspec-
> 1.0.2/lib/spec/runner/behaviour_runner.rb:27:in
> > > `run'
> > > /usr/local/lib/ruby/gems/1.8/gems/rspec-
> > > 1.0.2/lib/spec/runner/command_line.rb:17:in `run'
> > > /usr/local/lib/ruby/gems/1.8/gems/rspec-
> 1.0.2/bin/spec:3:
> > > /usr/local/bin/spec:18:in `load'
> > > /usr/local/bin/spec:18:
> > >
> > > Doesn't really tell me anything, how about you?
> >
> > Nope. Thanks for playing ;)
> >
> > One thing - controller examples used to implicitly re-raise controller
> > errors. We removed this in response to a bug report, which had the
> > insidious side-effect of hiding errors without you knowing. If you
> > upgrade to 1.0.4, you'll see a call in spec_helper.rb to
> > raise_controller_errors. This is a new method that lets you do so
> > explicitly, and is included in spec_helper.rb by default.
> >
> > I'm thinking this might help expose the problem.
> >
> > >
> > >
> > > On 5/31/07, David Chelimsky <dchelimsky at gmail.com > wrote:
> > > > On 5/31/07, Fischer, Daniel <daniel at danielfischer.com> wrote:
> > > > > Oh my...
> > > > >
> > > > > I don't know what happened... but my entire posts_controller_spec
> > > > > completely exploded and i'm having 31 failures, before I was having
> > > > > like 8. I'm not sure what is going on, I could REALLY use the help.
> > > > >
> > > > > http://pastie.caboo.se/66440 for a list of everything that is going
> > > > > on, and the code.
> > > >
> > > > Try running the one file with the -b switch to get a full backtrace.
> > > >
> > > > script/spec spec/controllers/posts_controller_spec.rb
> -b
> > > >
> > > > >
> > > > > I really appreciate the help guys, once I get over the mountain I'm
> > > > > sure I'll understand it :)
> > > > >
> > > > > On 5/30/07, Fischer, Daniel < daniel at danielfischer.com> wrote:
> > > > > > Okay that failed.
> > > > > >
> > > > > > I could really use a full blown example with this, just mocking
> isn't
> > > going
> > > > > > to "give me insight" anymore I don't think. If anyone can post a
> basic
> > > crud
> > > > > > behavior check, w/ nested routes - I'd really appreciate it.
> > > > > >
> > > > > > Thanks,
> > > > > > Daniel
> > > > > >
> > > > > > On 5/30/07, Chris Anderson <jchris at mfdz.com> wrote:
> > > > > > > I usually do something like this in my before(:each) section:
> > > > > > >
> > > > > > > User.stub!(:find).and_return(@u = mock_model(User, :posts => @pc
> =
> > > > > > > mock('posts collection')))
> > > > > > > @pc.stub(:find).and_return([@p = mock_model(Post)])
> > > > > > >
> > > > > > > with the corresponding should_receives in my various examples...
> the
> > > > > > > trick is to mock the user's posts collection as its own object.
> > > > > > >
> > > > > > > On 5/30/07, Fischer, Daniel < daniel at danielfischer.com> wrote:
> > > > > > > > Jonathan,
> > > > > > > >
> > > > > > > > Yeah something like that. I have no idea how to put it
> together
> > > > > > > > properly. I'm beyond confused at this point, that's why I
> could
> > > really
> > > > > > > > use some help.
> > > > > > > >
> > > > > > > > I figured out my "response" error, but now Im trying to "find
> all
> > > posts"
> > > > > > > >
> > > > > > > > Right now I got something like:
> > > > > > > >
> > > > > > > >
> > > > > >
> > >
> User.should_receive(:posts).with(:all).and_return([@posts])
> > > > > > > >
> > > > > > > > But I know it should be User.posts.find(:all) - i'm not sure
> how
> > > to
> > > > > > > > make this a proper behavior check.
> > > > > > > >
> > > > > > > > How do I setup my mocks to correspond with this?
> > > > > > > >
> > > > > > > > Thanks.
> > > > > > > >
> > > > > > > > On 5/30/07, Jonathan Linowes < jonathan at parkerhill.com> wrote:
> > > > > > > > > Don't you need to stub the User model :find too? (needed in
> your
> > > > > > > > > private get_user call)
> > > > > > > > >
> > > > > > > > >     private
> > > > > > > > >     def get_user
> > > > > > > > >       @user = User.find(params[:user_id])
> > > > > > > > >       @post = @user.posts.find(params[:id]) if params[:id]
> > > > > > > > >     end
> > > > > > > > >
> > > > > > > > >
> > > > > > > > >
> > > > > > > > > On May 30, 2007, at 8:03 PM, Fischer, Daniel wrote:
> > > > > > > > >
> > > > > > > > > > My problem has been listed here:
> > > > > > > > > >
> > > > > >
> http://railsforum.com/viewtopic.php?pid=25439#p25439
> > > > > > > > > >
> > > > > > > > > > Don't think it would be required to completely re-type it
> here
> > > :)
> > > > > > > > > >
> > > > > > > > > > Thanks!
> > > > > > > > > >
> > > > > > > > > > --
> > > > > > > > > > -Daniel Fischer
> > > > > > > > > >
> > > > > > > > > > http://danielfischer.com - Geek Blog
> > > > > > > > > > http://abigfisch.com - Portfolio
> > > > > > > > > > http://writersbeat.com - Writing Community
> > > > > > > > > >
> > > _______________________________________________
> > > > > > > > > > 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
> > > > > > > > >
> > > > > > > >
> > > > > > > >
> > > > > > > > --
> > > > > > > > -Daniel Fischer
> > > > > > > >
> > > > > > > > http://danielfischer.com - Geek Blog
> > > > > > > > http://abigfisch.com - Portfolio
> > > > > > > > http://writersbeat.com - Writing Community
> > > > > > > >
> _______________________________________________
> > > > > > > > rspec-users mailing list
> > > > > > > > rspec-users at rubyforge.org
> > > > > > > >
> http://rubyforge.org/mailman/listinfo/rspec-users
> > > > > > > >
> > > > > > >
> > > > > > >
> > > > > > > --
> > > > > > > Chris Anderson
> > > > > > > http://jchris.mfdz.com
> > > > > > >
> > > > > >
> > > > > >
> > > > > >
> > > > > > --
> > > > > >
> > > > > > -Daniel Fischer
> > > > > >
> > > > > > http://danielfischer.com - Geek Blog
> > > > > >  http://abigfisch.com - Portfolio
> > > > > > http://writersbeat.com - Writing Community
> > > > >
> > > > >
> > > > > --
> > > > > -Daniel Fischer
> > > > >
> > > > > http://danielfischer.com - Geek Blog
> > > > > http://abigfisch.com - Portfolio
> > > > > http://writersbeat.com - Writing Community
> > > > > _______________________________________________
> > > > > rspec-users mailing list
> > > > > rspec-users at rubyforge.org
> > > > > http://rubyforge.org/mailman/listinfo/rspec-users
> > > > >
> > > >
> > >
> > >
> > >
> > > --
> > >
> > > -Daniel Fischer
> > >
> > > http://danielfischer.com - Geek Blog
> > >   http://abigfisch.com - Portfolio
> > > http://writersbeat.com - Writing Community
> >
>
>
>
> --
>
> -Daniel Fischer
>
>  http://danielfischer.com - Geek Blog
> http://abigfisch.com - Portfolio
> http://writersbeat.com - Writing Community


-- 
-Daniel Fischer

http://danielfischer.com - Geek Blog
http://abigfisch.com - Portfolio
http://writersbeat.com - Writing Community


More information about the rspec-users mailing list