Incorrect behavior on RSpec expects counter change with destruction

I want users to be able to delete only the tasks that they created (and not the tasks of others). I checked that my application does this through a "rails server". However, the tests come out weird.

Here are the tests in question:

require 'spec_helper'

describe "Authentication" do
  subject { page }

  describe "signin" do
    [...]

    describe "authorization" do
      [...]

      describe "correct user control" do
        let(:user) { FactoryGirl.create(:user) }
        let(:job) { FactoryGirl.create(:job, user: user) }
        let(:wrong_user) { FactoryGirl.create(:user, email: "wrong@example.com") }
        let(:wrong_job) { FactoryGirl.create(:job, user: wrong_user) }
        before { sign_in user }
        [...]
        describe "users can only delete their own jobs" do
          it "should not change job count" do
            expect do
              delete job_path(wrong_job)
            end.to_not change(Job, :count)
          end
        end
        describe "users can delete their own jobs" do
          it "should decrease job count" do
            expect do
              delete job_path(job)
            end.to change(Job, :count).by(-1)
          end
        end
      end
    end
  end
end

In addition, I get this strange conclusion:

Failures:

  1) Authentication signin authorization correct user control users can only delete their own jobs should not decrease job count
     Failure/Error: expect do
       count should not have changed, but did change from 0 to 1
     # ./spec/requests/authentication_pages_spec.rb:68:in `block (6 levels) in <top (required)>'

  2) Authentication signin authorization correct user control users can delete their own jobs should decrease job count
     Failure/Error: expect do
       count should have been changed by -1, but was changed by 1
     # ./spec/requests/authentication_pages_spec.rb:75:in `block (6 levels) in <top (required)>'

Why is the number of tasks increasing? Why is the test not working as intended?


Additional Information:

jobs_controller.rb

class JobsController < ApplicationController
  skip_before_action :require_signin, only: [:index, :show]
  skip_before_action :correct_user, only: [:index, :show, :new, :create]
  before_action :set_job, only: [:show, :edit, :update, :destroy]

  [...]

  def destroy
    @job.destroy
    respond_to do |format|
      format.html { redirect_to jobs_url }
      format.json { head :no_content }
    end
  end

  private
    def set_job
      @job = Job.find(params[:id])
    end

  def job_params
    params.require(:job).permit(:title, :org, :internship, :postdate, :filldate, :location, :link, :description)
  end
end

application_controller.rb

class ApplicationController < ActionController::Base
  protect_from_forgery with: :exception
  before_filter :require_signin
  before_filter :correct_user

  include SessionsHelper

  private
    def require_signin
      unless signed_in?
        store_location
        redirect_to signin_url, notice: "Please sign in."
      end
    end

    def correct_user
      @job = current_user.jobs.find_by(id: params[:id])
      redirect_to root_url if @job.nil?
    end
end

rake routes

Prefix Verb   URI Pattern              Controller#Action
       jobs GET    /jobs(.:format)          jobs#index
            POST   /jobs(.:format)          jobs#create
    new_job GET    /jobs/new(.:format)      jobs#new
   edit_job GET    /jobs/:id/edit(.:format) jobs#edit
        job GET    /jobs/:id(.:format)      jobs#show
            PATCH  /jobs/:id(.:format)      jobs#update
            PUT    /jobs/:id(.:format)      jobs#update
            DELETE /jobs/:id(.:format)      jobs#destroy
[...]
+3
source share
3 answers

There are two problems:

  • , let . , , . job expect, job . , let , let!.

  • get, put, patch delete , :

# jobs_controller_spec.rb
delete :destroy, id: job

Capybara :

# users_messing_around_with_jobs_spec.rb
click_button("Delete")

delete , : - jobs#index.

+3

! , "" . , , :

let!(:job) { FactoryGirl.create(:job, user: user) }        
let!(:wrong_job) { FactoryGirl.create(:job, user: wrong_user) }
+2

:

We did not find named routes for the action delete, therefore delete job_path(wrong_job), delete job_path(job)they will not delete the task as we expect. Instead, because of the two delete job_path(wrong_job)and delete job_path(job)performances lazy appreciated joband wrong_jobin each block it, so the job is always the object model is changed to 1 for each test.

There are two options to fix this:

  • Create named routes for the action delete.
  • Add an identifier to designate which task you want to remove, for example delete :destroy, id: job.id.

First message, let me know if it is incorrect.

0
source

All Articles