Karan
Karan

Reputation: 15104

Ruby on Rails: Mock session variable in helper class

I am trying to mock the session hash in a controller's helper file:

Helper_Spec:

session.stub!(:[]).with("fb_token")
  RotaHelper.getListOfFriends.should == expected_friends

Helper:

FbGraph::User.me(session["fb_token"]).friends

Error:

NameError:
   undefined local variable or method `session' for RotaHelper:Module

Am I stubbing it incorrectly? Or helpers do not have acces to the session hash?

Upvotes: 1

Views: 1614

Answers (1)

e3matheus
e3matheus

Reputation: 2132

That's strange. In my helper tests, a session hash is created automatically. Maybe you didn't require spec_helper?

By the way, if the session wasn't created automatically by spec helper, you could also initialize it by saying that is a method of the helper. Like the following code:

session_hash = {}
helper.stub!(:session) { session_hash }

Upvotes: 2

Related Questions