2016-09-16 19 views
7

Tôi dường như không thể vượt qua bài kiểm tra này và tôi không hiểu tại sao.Kiểm tra send_data Rspec không vượt qua

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 

điều khiển:

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 

Sản lượng kiểm tra:

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>' 
+0

tôi giả sử bạn đang sử dụng 'FactoryGirl.create'. Bạn đã kiểm tra xem liệu 'create (: quotation_request)' có tạo thành công bản ghi không? – fylooi

+0

Có tôi thử nghiệm cho điều đó. Nó tạo ra các quote_request. – chell

+0

Bạn có sử dụng pry hoặc debugger để gỡ lỗi các trường hợp thử nghiệm không? –

Trả lời

3
#exercise 
    get :download, id: quotation_request 

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

Đây là ngược. Kỳ vọng sẽ đến trước khi gọi phương thức.

+0

Tôi đảo ngược hai dòng trên và tôi vẫn gặp lỗi tương tự. Bất kỳ ý tưởng? Tôi nên viết bài kiểm tra này như thế nào? – chell

+0

Đây phải là giải pháp, trừ khi hành động điều khiển của bạn không hoàn thành thành công. – fylooi

+0

Thao tác bộ điều khiển hoàn tất thành công vì tôi có thể kiểm tra nó trong trình duyệt. – chell

4

Bạn phải vượt qua 2 đối số expect(@controller).to receive(:send_data).with(quotation_request.sample_text, file_options) {@controller.render nothing: true}

1

bạn viết như sau:

  1. tập tin Nhận
  2. tập tin Mock

Nhưng ngay trường hợp bị đảo ngược:

  1. tập tin Mock
  2. tập tin Nhận

thử sau (sử dụng 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 
Các vấn đề liên quan