[rspec-devel] controller.should_redirect_to in 0.7.0

David Chelimsky dchelimsky at gmail.com
Mon Nov 6 14:28:05 EST 2006


controller.should_redirect_to has to come before the action in
question. It's like a mock expectation. In fact, it IS a mock
expectation under the hood.

So you'll have to reorganize your spec to something like this:

context "A new user signing up" do
  fixtures :users
  controller_name :account
  integrate_views

  def do_post
    post :signup, :user => {:email => 'quire at example.com', :password => 'quire',
    :password_confirmation => 'quire' }
  end

  specify "should save a valid " do
    do_post
    assigns(:user).should_be_valid
    assigns(:user).should_not_be_new_record
  end

  specify "should redirect" do
    controller.should_redirect_to :controller => "/account", :action
=> "index"
    do_post
  end
end

I'll update the docs to make this more clear.

Cheers,
David

On 11/6/06, Brandon Keepers <bkeepers at gmail.com> wrote:
> I'm having an issue with controller.should_redirect_to in 0.7.0.
> Here is an example of my spec:
>
> context "A new user signing up" do
>    fixtures :users
>    controller_name :account
>    integrate_views
>    setup do
>      post :signup, :user => {:email => 'quire at example.com', :password
> => 'quire',
>        :password_confirmation => 'quire' }
>    end
>
>    specify "should save a valid " do
>      assigns(:user).should_be_valid
>      assigns(:user).should_not_be_new_record
>    end
>
>    specify "should redirect" do
>      controller.should_redirect_to :controller => "/account", :action
> => "index"
>    end
> end
>
> Which produces this failure:
>
> Spec::Mocks::MockExpectationError in 'A new user signing up should
> redirect'
> controller expected call to redirect_to {:controller=>"/
> account", :action=>"index"} but it was never received
>
> However, if I insert a breakpoint right before that expectation and
> inspect the response, I can see that the response is a redirect:
>
>  >> response.redirected_to
> => {:controller=>"/account", :action=>"index"}
>
> Any ideas?
>
> Thanks,
> Brandon
> _______________________________________________
> rspec-devel mailing list
> rspec-devel at rubyforge.org
> http://rubyforge.org/mailman/listinfo/rspec-devel
>


More information about the rspec-devel mailing list