There is a session variable used in controller for create. How to set the session variable for rspec test?
Here is the method create in controller w/ session[eng_dh] :
def create
if session[:eng_dh]
@category = Category.new(params[:category], :as => :roles_new)
if @category.save
redirect_to categories_path, :notice => 'Category was successfully created.'
else
render :action => "new"
end
else
redirect_to categories_path, :notice =>"NO access to create category!"
end
end
Here is the failing rspec code:
require 'spec_helper'
describe CategoriesController do
before(:each) do
#the following recognizes that there is a before filter without execution of it.
controller.should_receive(:require_signin)
controller.should_receive(:require_employee)
end
render_views
describe "Post 'create'" do
describe "success" do
before(:each) do
@category = Factory(:category)
session[:eng_dh] = true #THIS LINE CAUSED ERROR ( undefined method `stringify_keys' for "1":String) IN RSPEC TEST!!!
end
it "should create a category" do
lambda do
post :create, :category => @category
end.should change(Category, :count).by(1)
end
it "should redirect to the category index page" do
post :create, :category => @category
response.should redirect_to(categories_path)
end
end
end
end
Any solutions? Thanks.
You should be able to set a session variable using that syntax.
The
undefined method 'stringify_keys'is not due to your setting of the session variable, rather it’s in the construction of the POST parameters from your@categoryobject. The@categoryobject has already been saved;postexpects a hash.Properly sending the post params will fix it:
Your test began to fail when you added the session line because your controller will only try to save when that session var is in place. Without the session var,
createdoes nothing, but yourFactoryline has already saved the row. That’s a false “pass”.