Topic: RSpec and nested routes behavior

Having tons of problems trying to figure out a posts_controller behavior nested behind users:


http://pastie.caboo.se/66440

For my code.


Any help would be greatly appreciated - thanks smile

Last edited by DFischer (2007-05-31 05:57:37)

http://danielfischer.com - Personal Web-Technology-Blog, Los Angeles.

Re: RSpec and nested routes behavior

The rendering of "diagnostics.rhtml" means there was an error raised in the controller and it's trying to display the error page. You should override this so it outputs the stack trace to rspec so you can see what's going on. See this post (and the rest of the thread) for details.

This should tell you more about what part of the code is causing problems.

Railscasts - Free Ruby on Rails Screencasts

Re: RSpec and nested routes behavior

Hey so if you read my previous thing about nested routes - I'm
just not going to do with them anymore.

Anyway, so my controller has a before_filter on the edit action; which
checks to see if the current post's user, is the same as the currently
logged in user, if true then they can edit, otherwise not.

def check_user
   if current_user.login != @post.user.login
     redirect_to post_url(@post)
   end
end

Now I can easily test this functionality by seeing if I get redirected
if I hit that page, but what if I actually want to be logged in, so I
can test to see if the edit action is working?

Any input on this?

Thanks.

Last edited by DFischer (2007-05-31 22:12:02)

http://danielfischer.com - Personal Web-Technology-Blog, Los Angeles.

Re: RSpec and nested routes behavior

You can set a session variable on @request before calling the action.

@request.session[:user_id] = 1

I'm a little fuzzy on the syntax, but I think that's correct.

Alternatively you can stub out the current_user method on @controller. I guess this approach is better because it avoids a hit to the database.

Railscasts - Free Ruby on Rails Screencasts

Re: RSpec and nested routes behavior

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 sad

Last edited by DFischer (2007-06-01 00:29:48)

http://danielfischer.com - Personal Web-Technology-Blog, Los Angeles.