1

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 ?

6
  • What's gonna happen if you remove to_a call? Commented Apr 4, 2014 at 11:41
  • Then nothing is shown after 1) TrashController GET #show should contain exactly in its line. Rest are the same. Commented Apr 4, 2014 at 11:59
  • I assume that only_deleted is a scope, right? Could you show how is it defined and how trashed_slide is being assigned? Commented Apr 4, 2014 at 12:04
  • probably yes, as I was trying to read the code of paranoid2 gem. In my controler there's no more than empty def show. All the rest is set by the gem itself and paranoid in the model. I was wondering if it could be a problem for model rspec that module Paranoid2 extend ActiveSupport::Concern. Commented Apr 4, 2014 at 12:09
  • Could you also show your show action? Commented Apr 4, 2014 at 12:24

1 Answer 1

1
+100

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)
Sign up to request clarification or add additional context in comments.

2 Comments

worked like a charm ! Just one more question if I may. I think it should be somehow visible on the error message but even now I can't see anything what would suggest where's the problem. Am I missing something important there ?
The error descriptions says: expected an array, actual collection was #<Slide id: 1... If there was an array returned there would have been a [ in that message in front of the #<Slide. Hard to see that there is one character missing.

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.