哪里放嘲笑

时间:2014-06-30 19:20:21

标签: ruby-on-rails rspec

我试图指明这个动作。

def get
    @asset = current_user.assets.find(params[:id])

    send_file @asset.uploaded_file.path, type: @asset.uploaded_file_content_type

    rescue ActionController::MissingFile
        redirect_to assets_url, error: 'missing file'
end

为了测试发送文件方法,我们将其模拟出来。

controller.should_receive(:send_file)

但是,我不知道在哪里放这个模拟器:

以下是我的规范:

subject { response }

let!(:user) { FactoryGirl.create(:user) }  
let!(:user_2) { FactoryGirl.create(:user) }  
let!(:asset) { FactoryGirl.create(:asset, user_id: user.id) }
let!(:file) { fixture_file_upload('files/eve.jpg', 'image/jpeg') }
let!(:folder) { FactoryGirl.create(:folder, user_id: user.id, parent_id: nil) }

before do
  sign_in user
end

describe '#get' do
    context 'when exists' do
      before do
        get :get, id: asset.id
      end
      # controller.should_receive(:send_file).with(*args) <-- I need to test that
      it { should have_http_status 302 }
    end

    context 'when doesn\'t exist' do
      before do
        get :get, id: 765      
      end

      it { should redirect_to_location '/assets'}
      it { should set_flash_type_to :error }
      it { should set_flash_message_to 'missing file' }
    end
end

如何测试第6行。我希望尽可能保留一行语法。

1 个答案:

答案 0 :(得分:0)

将其放入before

before do
  controller.should_receive(:send_file)
  get :get, id: asset.id
end