2016-09-16 20 views
7

Nie mogę przetestować tego testu i nie rozumiem dlaczego.Rspec test send_data nie przechodzi

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 

kontroler:

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 

Wyjście z testu:

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

Zakładam, że używasz 'FactoryGirl.create'. Czy sprawdziłeś, czy 'create (: quotation_request)' pomyślnie tworzy rekord? – fylooi

+0

Tak, testuję to. Tworzy zapytanie o wycinkę. – chell

+0

Czy korzystasz z podpowiedzi lub debuggera do debugowania przypadków testowych? –

Odpowiedz

3
#exercise 
    get :download, id: quotation_request 

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

Jest tyłu. Oczekiwanie powinno nastąpić przed wywołaniem metody.

+0

Odwróciłem powyższe dwie linie i nadal otrzymuję ten sam błąd. Jakieś pomysły ? Jak mam napisać ten test? – chell

+0

To powinno być rozwiązanie, chyba że działanie kontrolera nie zakończyło się pomyślnie. – fylooi

+0

Czynność kontrolera kończy się pomyślnie, ponieważ mogę ją przetestować w przeglądarce. – chell

4

Należy zdać 2 argumenty expect(@controller).to receive(:send_data).with(quotation_request.sample_text, file_options) {@controller.render nothing: true}

1

piszesz następujące:

  1. plik Get
  2. Mock plik

Ale prawo sprawa jest odwrócony:

  1. Mock plik
  2. plik Get

try następujące (przy użyciu 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 
Powiązane problemy