Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby: Error in minitest to destroy user

I am a newbie to ruby and am just creating my first test suite.

When writing a minitest to destroy a user I get the following error:

ERROR["test_should_destroy_when_logged_in_as_a_admin", UsersControllerTest]
test_should_destroy_when_logged_in_as_a_admin#UsersControllerTest ActionController::UrlGenerationError: 
No route matches {:action=>"/users/608331937", :controller=>"users"}

The test reads the following:

        def setup
        @user_destroy = users(:destroyme)
        @user_admin = users(:admin)
    end

    test "should destroy when logged in as a admin" do
        log_in_as(@user_admin)
        assert @user_admin.admin?, "not admin"
        assert_difference 'User.count', -1 do
            delete user_path(@user_destroy)
        end
    end

and fixture:

admin:
 name: Matthias Havenaar
 email: [email protected]
 password_digest: <%= User.digest('password') %>
 admin: true

destroyme:
 name: Destroy Me
 email: [email protected]
 password_digest: <%= User.digest('password') %>
 admin: true

It seems like something goes wrong with the user ID or user_path. Any idea what I am doing wrong here?

like image 741
Matthias Avatar asked Feb 16 '26 01:02

Matthias


1 Answers

Try this, I hope this will work.

Replace

delete user_path(@user_destroy)

With

delete :destroy, id: @user_destroy
like image 169
Amit Sharma Avatar answered Feb 18 '26 22:02

Amit Sharma



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!