Question

Note: my app works just fine. I'm just unable to do the right rspec for it.

trash_controller.rb:

class TrashController < ApplicationController
    before_action :set_slide, only: [ :show, :destroy, :restore ]

    def set_slide
        @trashed_slide = Slide.only_deleted.find(params[:id])
    end

    def show
    end

trash_controller_spec.rb:

describe TrashController do

  let(:album) { create(:album) }
  let(:slide) { build(:slide) }

  describe "GET #show" do
    before do
      slide.save
      slide.destroy        
      get :show, id: slide.id
    end

    it { expect(assigns(:trashed_slide)).to match_array(Slide.only_deleted.to_a) } 
  end

error:

1) TrashController GET #show should contain exactly #<Slide id: 1, album_id: 1, description: "Brennon Prosacco", created_at: "2014-04-02 06:06:03", updated_at: "2014-04-02 06:06:03", photo_file_name: "sample_2.jpg", photo_content_type: "image/jpeg", photo_file_size: 204509, photo_updated_at: "2014-04-02 06:06:03", photo_fingerprint: "4dbd1870094527b8c4ddca6afd415eb9", deleted_at: "2014-04-02 06:06:03", photo_processing: false>
     Failure/Error: it { expect(assigns(:trashed_slide)).to match_array(Slide.only_deleted.to_a) }
       expected an array, actual collection was #<Slide id: 1, album_id: 1, description: "Brennon Prosacco", created_at: "2014-04-02 06:06:03", updated_at: "2014-04-02 06:06:03", photo_file_name: "sample_2.jpg", photo_content_type: "image/jpeg", photo_file_size: 204509, photo_updated_at: "2014-04-02 06:06:03", photo_fingerprint: "4dbd1870094527b8c4ddca6afd415eb9", deleted_at: "2014-04-02 06:06:03", photo_processing: false>
     # ./spec/controllers/trash_controller_spec.rb:25:in `block (3 levels) in <top (required)>'

I don't get what it's about as both lines looks the same. Any ideas ?

Was it helpful?

Solution

I would not expect Slide.only_deleted.find(params[:id]) to return an array. find returns just one slide. Therefore I would change the expectation to:

expect(assigns(:trashed_slide)).to eq(Slide.only_deleted.first)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top