Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to stub the Params values when i am testing a controller by Rspec

Can anyone please let me know if it is possible to stub params[] values in controller spec, so that the controller accepts the stubbed values as actual params[] value from view.

for example, my view has a statement as

<%= form_tag("/expense_details/add_expense", :method=>"post") do %>
<%= text_field_tag(:eamt, nil, placeholder: "Expense Amount") %>

my controller has a statement as

expense_type= params[ :eamt ]
if !expense_type.nil?
session[:emsg]="filled"
else
session[:emsg]="empty"
end

my controller spec is as follows

it "successful save" do
my spec has condition as
post 'add_expense' 
session[:emsg].should == "filled"
end

But my test is faling everytime with this below status

 Failure/Error: session[:emsg].should == "filled"
   expected: "filled"
        got: "empty" (using ==)

can anyone please let me know how can i achieve this? Thanks in advance

like image 276
Nilay Chakraborty Avatar asked Dec 07 '25 02:12

Nilay Chakraborty


1 Answers

In the RSpec controller spec, change the post line to:

post 'add_expense', eamt: 'stubbed_value'

The post method accepts a hash of options that will be considered parameters to the request. Docs from Rspec

like image 90
maxbeizer Avatar answered Dec 08 '25 14:12

maxbeizer



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!