如何使用 RSpec 测试新动作的初始化值?
How to test initialization values of new action with RSpec?
在我的 Ruby on Rails 应用程序中,我有一个操作 copy
复制 invoice
并将其值呈现到 new
中的前端行动。
它有效,但我发现它很难测试:
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
是否被正确复制并显示在表格中。我是否必须先保存新发票才能进行测试?
感谢您的帮助。
class YourController < ApplicationController
def copy
@new_invoice
end
end
使用 assigns
测试控制器内的实例变量,就像那样 expect(assigns(:new_invoice).date).to eql(@invoice.date)
对于最新的 RSpec 版本,需要安装 gem 'rails-controller-testing'
才能使用 assigns
方法
在我的 Ruby on Rails 应用程序中,我有一个操作 copy
复制 invoice
并将其值呈现到 new
中的前端行动。
它有效,但我发现它很难测试:
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
是否被正确复制并显示在表格中。我是否必须先保存新发票才能进行测试?
感谢您的帮助。
class YourController < ApplicationController
def copy
@new_invoice
end
end
使用 assigns
测试控制器内的实例变量,就像那样 expect(assigns(:new_invoice).date).to eql(@invoice.date)
对于最新的 RSpec 版本,需要安装 gem 'rails-controller-testing'
才能使用 assigns
方法