谁能告诉我是否可以在 Controller 规范中 stub params[] 值,以便 Controller 接受 stub 值作为 View 中的实际 params[] 值。
例如, 我的观点有一个陈述
<%= form_tag("/expense_details/add_expense", :method=>"post") do %>
<%= text_field_tag(:eamt, nil, placeholder: "Expense Amount") %>
我的 Controller 有一个声明
expense_type= params[ :eamt ]
if !expense_type.nil?
session[:emsg]="filled"
else
session[:emsg]="empty"
end
我的 Controller 规范如下
it "successful save" do
my spec has condition as
post 'add_expense'
session[:emsg].should == "filled"
end
但是我的测试每次都失败,状态如下
Failure/Error: session[:emsg].should == "filled"
expected: "filled"
got: "empty" (using ==)
任何人都可以让我知道我怎样才能做到这一点? 提前致谢
最佳答案
在 RSpec Controller 规范中,将 post 行更改为:
post 'add_expense', eamt: 'stubbed_value'
post
方法接受将被视为请求参数的选项散列。 Docs from Rspec
关于ruby-on-rails - 当我通过 Rspec 测试 Controller 时如何 stub 参数值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30512198/