即使数组相同,RSpec match_array也会失败

时间:2014-04-02 07:08:56

标签: ruby-on-rails ruby rspec rspec-rails

注意:我的应用工作正常。我只是无法为它做正确的rspec。

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

错误:

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)>'

由于两条线条看起来相同,我不知道它的含义。有什么想法吗?

1 个答案:

答案 0 :(得分:1)

我不希望Slide.only_deleted.find(params[:id])返回一个数组。 find只返回一张幻灯片。因此,我会将期望改为:

expect(assigns(:trashed_slide)).to eq(Slide.only_deleted.first)