1

I am new to RSpec testing and currently try to add tests to existing controller for a Rails 4 app. Here is the Github app link, in case you need more details: https://github.com/iacobson/Zero2Dev

resources_controller.rb

    class ResourcesController < ApplicationController
        before_action :authenticate_user!, only:[:new, :create, :destroy]

          def destroy
            @resource = current_user.resources.find(params[:id])
            @resource.destroy
            redirect_to resources_path
          end

        private

        def resource_params
          params.require(:resource).permit(:content, :user_id) 
        end
    end

resources_controller_spec.rb

require 'rails_helper'

RSpec.describe ResourcesController, type: :controller do


  describe "DELETE #destroy" do
    let(:user1) {User.create!(name:"John", email:"[email protected]", password:"password")}
    let(:user2) {User.create!(name:"Mary", email:"[email protected]", password:"password")}
    let(:resource){user1.resources.create!(content: "Neque porro quisquam est qui dolorem ipsum")}


    it "deletes resource when user 1 (that created the resource) is logged-in" do
      sign_in user1
      delete :destroy, id: resource.id
      puts resource.content
      expect(resource.content).to be_nil
    end    
  end        
end

but looks like "resource" never get deleted:

 Failure/Error: expect(resource.content).to be_nil
       expected: nil
            got: "Neque porro quisquam est qui dolorem ipsum"

I tried a lot of other options from Devise tutorial or from other tutorials or answers I found on internet, but all ended in error. I even tried to eliminate the current_user validation from controller, but no chance.

What would be the correct way to test the Destroy action in a controller, using Rails4, Devise and Rspec

Thank you !

1 Answer 1

1

The resource you have in your spec is already loaded and doesn't change when the row is deleted from the DB. You can do a couple things:

Test that the resource is gone from the DB

expect(Resource.find_by(id: resource.id)).to be_nil

Test that the DB count changes

expect { delete :destroy, id: resource.id }.to change(Resource, :count).by(-1)
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you! That makes so much sense, and my question so silly... I was looking in the completely wrong place for the error.
I've definitely done this to myself plenty in the past :-D It's always the simple things that trip you up the longest.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.