2017-08-01 33 views
0

在我的Ruby on Rails應用程序中,我有一個動作copy,它複製invoice並將其值顯示到new操作中的前端。如何用RSpec測試新動作的初始化值?

它的工作原理,但我很難發現它的測試:

describe 'GET #copy' do 

    before :each do 
    @invoice = FactoryGirl.create(:invoice, :date => Date.yesterday) 
    end 

    it "renders the :new template" do # this works 
    get :copy, :id => @invoice 
    expect(response).to render_template :new 
    end 

    it "sets the correct date" do 
    get :copy, :id => @invoice 
    expect(new_invoice.date).to eql(@invoice.date) # what I had in mind... 
    end 

end 

我只是想測試是否date被正確地複製和形式顯示。我需要先保存新發票才能測試嗎?

感謝您的任何幫助。

回答

1
class YourController < ApplicationController 
    def copy 
    @new_invoice 
    end 
end 

使用assigns測試實例變量控制器裏面,像expect(assigns(:new_invoice).date).to eql(@invoice.date) 對於最新的RSpec版本,需要使用assigns方法

+0

就是這樣,非常感謝安裝gem 'rails-controller-testing'! – Tintin81

相關問題