it "destroys the requested user" douser = User.create! valid_attributesexpect {delete :destroy, {:id => user.to_param}, valid_session}.to change(User, :count).by(-1)end
def valid_sessioncontroller.stub(current_user: mock_model(User))end
def destroy@user = User.find(params[:id])if @user.destroysession[:user_id] = nilredirect_to root_url, notice: 'User was successfully deleted and signed out!'end
I want to test destroying a user through the controller, therefore I do in user users_controller_spec.rb:it "destroys the requested user" douser = User.create! valid_attributesexpect {delete :destroy, {:id => user.to_param}, valid_session}.to change(User, :count).by(-1)endThe valid_session is generated that waydef valid_sessioncontroller.stub(current_user: mock_model(User))end
In other models this test setup works fine.
But in the user context, I get the following error:1) UsersController DELETE destroy destroys the requested userFailure/Error: expect {count should have been changed by -1, but was changed by 0I assume, this is behaviour is related to valid_session, which interacts with the test, because of creating a second user?
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.destroysession[:user_id] = nilredirect_to root_url, notice: 'User was successfully deleted and signed out!'endAny ideas,
On Sun, May 19, 2013 at 2:32 PM, netzfisch <tj2...@gmail.com> 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" douser = User.create! valid_attributesexpect {delete :destroy, {:id => user.to_param}, valid_session}.to change(User, :count).by(-1)endThe valid_session is generated that waydef valid_sessioncontroller.stub(current_user: mock_model(User))endThis ^^ returns an RSpec::Mocks::MethodDouble object, which is probably not what you want.
let(:user) { User.create! valid_attributes }def valid_session{ :user_id => user.id }endor not to interfere the test - with the usage of a seperate user:let(:user) { User.create! valid_attributes }def valid_session{ :user_id => FactoryGirl.create(:user) }end
In other models this test setup works fine.
My guess is that is accidental - that the result of valid_session does not get used.
def valid_sessioncontroller.stub(current_user: FactoryGirl.create(:user))end