2017-08-01 4 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最新のRSpecのバージョンについてはそのexpect(assigns(:new_invoice).date).to eql(@invoice.date) のように、あなたのコントローラ内のインスタンス変数をテストするための、それだassigns方法

+0

を使用するためのgem 'rails-controller-testing'をインストールする必要があります、どうもありがとう! – Tintin81

関連する問題