On Sun, May 19, 2013 at 2:32 PM, netzfisch <[email protected]> wrote:

> I want to *test destroying a user* through the controller, therefore I do
> in user users_controller_spec.rb:
>
>     it "destroys the requested user" do
>       user = User.create! valid_attributes
>       expect {
>         delete :destroy, {:id => user.to_param}, valid_session
>       }.to change(User, :count).by(-1)
>     end
>
>
> The *valid_session* is generated that way
>
>   def valid_session
>     controller.stub(current_user: mock_model(User))
>   end
>
> This ^^ returns an RSpec::Mocks::MethodDouble object, which is probably
not what you want.


> In *other models *this* test setup works* fine.
>

My guess is that is accidental - that the result of valid_session does not
get used.


> But in the user context, I get the following *error:*
>
>   1) UsersController DELETE destroy destroys the requested user
>      Failure/Error: expect {
>        count *should* have been *changed by -1*, but was *changed by 0*
>
> I assume, this is behaviour is related to *valid_session*, which
> interacts with the test, because of creating a second user?
>

The second user is generated with mock_model, which does not save anything
to the database, so I doubt that's the underlying issue.


> So there is one additional user added and one destroyed, which equals to
> zero!
>
> But I don't see a way to avoid this!
>
> The destroy implementation is straight foward:
>
>   def destroy
>     @user = User.find(params[:id])
>
>     if @user.destroy
>       session[:user_id] = nil
>       redirect_to root_url, notice: 'User was successfully deleted and signed 
> out!'
>   end
>
>
> Any ideas,
>

If the idea here is that the session user is removing him/herself from the
system, then the example should set up _that_ user in the session e.g.

  it "destroys the requested user" do
    user = User.create! valid_attributes
      expect {
        delete :destroy, {:id => user.to_param}, {:user_id => user.id}
      }.to change(User, :count).by(-1)
    end
  end

Since :user_id is the only key used in destroy, I _think_ this should work.

HTH,
David

-- 
You received this message because you are subscribed to the Google Groups 
"rspec" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to