【问题标题】:Rspec send_data test not passingRspec send_data 测试未通过
【发布时间】:2017-01-24 05:43:44
【问题描述】:

我似乎无法通过此测试,我不明白为什么。

controller_spec.rb:

require 'rails_helper'

RSpec.describe QuotationRequestsController, type: :controller do

  describe "GET download" do    
    it "streams the sample text as a text file" do
      #setup
      quotation_request = create(:quotation_request)
      file_options = {filename: "#{quotation_request.id}-#{quotation_request.client.name.parameterize}.txt", type: 'plain/text', disposition: 'attachment'}

      #exercise
      get :download, id: quotation_request

      #verification
      expect(@controller).to receive(:send_data).with(file_options) {@controller.render nothing: true}      
    end
  end
end

控制器:

def download
  @quotation_request = QuotationRequest.find(params[:id])
  send_data @quotation_request.sample_text, {
    filename: @quotation_request.sample_text_file, 
    type: "text/plain",
    disposition: "attachment"
  }
end

测试输出:

1) QuotationRequestsController GET download streams the sample text as a text file
  Failure/Error: expect(@controller).to receive(:send_data).with(file_options) {
    @controller.render nothing: true
  }       
  (# <QuotationRequestsController:0x007ff35f926058>).send_data({
    :filename=>"1-peter-johnson.txt",
    :type=>"plain/text",
    :disposition=>"attachment"
  })  
  expected: 1 time with arguments: ({
    :filename=>"1-peter-johnson.txt",
    :type=>"plain/text", :disposition=>"attachment"
  })
  received: 0 times
  # ./spec/controllers/quotation_requests_controller_spec.rb:380:in `block (3 levels) in <top (required)>'
  # -e:1:in `<main>'

【问题讨论】:

  • 我假设您使用的是FactoryGirl.create。有没有检查create(:quotation_request)是否成功创建记录?
  • 是的,我对此进行了测试。它创建了quotation_request。
  • 你使用 pry 还是 debugger 来调试测试用例?

标签: ruby-on-rails rspec


【解决方案1】:
  #exercise
  get :download, id: quotation_request

  #verification
  expect(@controller).to receive(:send_data).with(file_options) {@controller.render nothing: true}      

这是倒退。期望应该在方法调用之前。

【讨论】:

  • 我颠倒了上面的两行,我仍然得到同样的错误。有任何想法吗 ?我应该如何编写这个测试?
  • 这应该是解决方案,除非您的控制器操作未成功完成。
  • 控制器操作已成功完成,我可以在浏览器中对其进行测试。
【解决方案2】:

你应该传递 2 个参数 expect(@controller).to receive(:send_data).with(quotation_request.sample_text, file_options) {@controller.render nothing: true}

【讨论】:

    【解决方案3】:

    你写了以下内容:

    1. 获取文件
    2. 模拟文件

    但正确的情况是颠倒的:

    1. 模拟文件
    2. 获取文件

    尝试关注(使用before):

    require 'rails_helper'
    
    RSpec.describe QuotationRequestsController, type: :controller do
      describe "GET download" do   
        let(:quotation_request) { create(:quotation_request) }
        let(:file_options) { {filename: "#{quotation_request.id}-#{quotation_request.client.name.parameterize}.txt", type: 'plain/text', disposition: 'attachment'} }
    
        before do
          expect(@controller).to receive(:send_data)
             .with(file_options) { @controller.render nothing: true }
        end
    
        it "streams the sample text as a text file" do
          get :download, id: quotation_request
        end
      end
    end
    

    【讨论】:

      【解决方案4】:

      导轨 5:

      expect(@controller).to receive(:send_data).with(quotation_request.sample_text, file_options) {@controller.head :ok}

      参考:https://stackoverflow.com/a/43428992

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-01-03
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多