title: why is this failing 'undefined method `destroy' for nil:NilClass' rspec rails
body: <p>The idea of this test is to handle the event a record is not found. There for a destroy action should not take place and yet it is. I've chopped and changed it around, nut essentially the test reads right, so there must be a problem in my code.</p>
  
  <pre><code>RSpec.describe Admin::EntriesController, :type =&gt; :controller do
  
    setup_factories
  
    let(:model){ Photo }
    let(:instance){photo}
    let(:no_instances){no_photos}
    let(:some_instances){some_photos}
    let(:params_full_instance){params_full_photo}
    let(:params_new_instance){params_new_photo}
  
  describe "delete destroy" do
        context "where the record is found" do
          before do
            allow(model).to receive(:where).and_return(some_instances)
            allow(some_instances).to receive(:first).and_return(instance)
          end
          context "where the record is destroyed" do
            before do
              allow(instance).to receive(:destroy).and_return(true)
              delete :destroy, params_id
            end
            sets_flash(:notice)
            redirects_to('/admin/entries/rejected')
          end
          context "where the record is not destroyed" do
            before do
              allow(instance).to receive(:destroy).and_return(false)
              delete :destroy, params_id
            end
            sets_flash(:error)
            redirects_to('/admin/entries/rejected')
          end
        end
        context "where the record is not found" do
          before do
            allow(model).to receive(:where).and_return(no_instances)
            delete :destroy, params_id
  
          end
          sets_flash(:error)
          redirects_to('/admin/entries/rejected')
        end
  </code></pre>
  
  <p>CONTROLLER</p>
  
  <pre><code>  before_action :get_entry_id, only: [:destroy, :toggle_approve_field, :toggle_reject_field]
      def destroy
         if @entry.nil?
            flash[:error] = "object is not not found"
          end
          if @entry.destroy
            flash[:notice] = 'blah'
            respond_to do |format|
              format.html { redirect_to rejected_admin_entries_path }
              format.json { head :no_content }
              format.js
            end
          else
            flash[:error] = 'There was a problem fetching the record'
            redirect_to rejected_admin_entries_path
          end
        end
  
      def get_entry_id
          if @entry.nil?
            flash[:error] = "object is not not found"
          end
          @entry = Photo.where(id: params[:id]).first
        end
  </code></pre>
  
  <p>I probably need a fresh pair of eyes on it. And a nice explanation as to what I am getting wrong ;)</p>
  
  <p>UPDATE: expected behaviour, is, when a destroy action is made, and the record is not found, i expect to see a flash error and to be redirected.</p>
  
  <p>UPDATE 2: Implemented guys code below, still receiving error </p>
  
  <pre><code> 1) Admin::EntriesController for authenticated users delete destroy where the record is not found should set flash error
       Failure/Error: expect(flash[method]).to_not be_nil
         expected: not nil
              got: nil
       # -e:1:in `&lt;main&gt;'
  
    2) Admin::EntriesController for authenticated users delete destroy where the record is not found should redirect to /admin/entries/rejected
       Failure/Error: expect(response).to redirect_to(path)
         Expected response to be a &lt;redirect&gt;, but was &lt;200&gt;
  </code></pre>
  
  <p>Thanks
  Rico</p>