Search code examples
rubyminitestfixturesdestroy

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: my@mail.com
 password_digest: <%= User.digest('password') %>
 admin: true

destroyme:
 name: Destroy Me
 email: destroy@me.com
 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?


Solution

  • Try this, I hope this will work.

    Replace

    delete user_path(@user_destroy)
    

    With

    delete :destroy, id: @user_destroy