# RSpec Post/Get and Sessions in a Story

**URL:** <https://www.ruby-forum.com/t/rspec-post-get-and-sessions-in-a-story/139095>\
**Category:** Rails\
**Created:** [May 30, 2008, 6:48pm UTC](https://www.ruby-forum.com/t/rspec-post-get-and-sessions-in-a-story/139095 "2008-05-30T18:48:03Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![glenatron](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/94ad74/32.png) [@glenatron](https://www.ruby-forum.com/u/glenatron)\
**Post date:** [May 30, 2008, 6:48pm UTC](https://www.ruby-forum.com/t/rspec-post-get-and-sessions-in-a-story/139095/1 "2008-05-30T18:48:03Z")

</div>

I’ve got an RSpec story that reads like:

* * *

Given that a post exists  
And I am logged in  
And I have edit permissions  
When I visit the post details page  
Then there should be a link to add a new comment

* * *

The issue I’m having is that I can’t seem to set my login state within  
the Given clause. Ideally, I’d like to just assume that the user is  
logged in by setting a session value (session[‘username’] = ‘whatever’),  
but I don’t believe I can access the session variables from an RSpec  
story - correct me if I’m wrong.

Since I can’t just set a session value, I’m trying to implement the “I  
am logged in” bit as follows:

* * *

Given(“I am logged in”) do  
post :login, {:name =\> “domain\name”, :password =\> “password”}  
end

* * *

But I get “NoMethodError: undefined method `post’ for  
#Object:0x3fafcf0”

So, in order to get around this, I’ve tried:

* * *

Given(“I am logged in”) do  
@login = LoginController.new  
@login.authenticate(‘domain’, ‘username’,‘password’)  
end

* * *

But, since the LoginController can’t access the session variable, I get  
a “nil object when you didn’t expect it” error when it tries to set the  
session upon authenticating.

So, two questions:

1. Can I do get/post within a story, and if so, how? I’ve seen examples  
of get/posts in stories, but everything I’ve tried comes back with “get”  
or "post as an undefined method.
2. Is there any way to get to the session directly within an RSpec  
story?

---

<div class="post-metadata">

**Author:** ![glenatron](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/94ad74/32.png) [@glenatron](https://www.ruby-forum.com/u/glenatron)\
**Post date:** [May 30, 2008, 9:24pm UTC](https://www.ruby-forum.com/t/rspec-post-get-and-sessions-in-a-story/139095/2 "2008-05-30T21:24:16Z")

</div>

Ben M. wrote:

> 1. Can I do get/post within a story, and if so, how? I’ve seen examples  
> of get/posts in stories, but everything I’ve tried comes back with “get”  
> or "post as an undefined method.

Are get/post methods defined inside your controller or are you thinking  
of the httpd verbs here? These are two very different things. Your  
default controller actions are:

def index  
def show  
def new  
def edit  
def create  
def update  
def destroy

> 1. Is there any way to get to the session directly within an RSpec  
> story?

Your session is accessible via the params hash. The question is: what  
form of login authentication are you using and how is it represented in  
parans[]?

---

<div class="post-metadata">

**Author:** ![glenatron](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/94ad74/32.png) [@glenatron](https://www.ruby-forum.com/u/glenatron)\
**Post date:** [May 30, 2008, 10:14pm UTC](https://www.ruby-forum.com/t/rspec-post-get-and-sessions-in-a-story/139095/3 "2008-05-30T22:14:36Z")

</div>

The params thing should do it for the session variable. All that’s  
happening to check whether a user is logged in or not is to see if the  
session[‘username’] has a value. Currently it’s doing authentication  
via our Windows domain.

> Are get/post methods defined inside your controller or are you thinking  
> of the httpd verbs here? These are two very different things. Your  
> default controller actions are:

I’m not testing the controller directly in this situation - I’ve done  
that within an RSpec spec. What I’m attempting to do is request a page  
from our RSpec story. According to  
[http://tomtenthij.co.uk/2008/1/25/rspec-plain-text-story-runner-on-a-fresh-rails-app](http://tomtenthij.co.uk/2008/1/25/rspec-plain-text-story-runner-on-a-fresh-rails-app)  
, I should be able to call “post” within our story without specifying  
the method in a controller context. That being said, I have absolutely  
no idea what object post or get is being called on within the example  
I’ve linked to.

Are you aware of another way to request a page? For example, my first  
instinct is to call “get ‘/posts/1/’” when considering the story line  
“When I visit the post details page”, and this does not work.

---

<div class="post-metadata">

**Author:** ![glenatron](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/94ad74/32.png) [@glenatron](https://www.ruby-forum.com/u/glenatron)\
**Post date:** [May 30, 2008, 11:56pm UTC](https://www.ruby-forum.com/t/rspec-post-get-and-sessions-in-a-story/139095/4 "2008-05-30T23:56:27Z")

</div>

Ben M. wrote:

> The params thing should do it for the session variable. All that’s  
> happening to check whether a user is logged in or not is to see if the  
> session[‘username’] has a value. Currently it’s doing authentication  
> via our Windows domain.
> 
> > Are get/post methods defined inside your controller or are you thinking  
> > of the httpd verbs here? These are two very different things. Your  
> > default controller actions are:
> 
> I’m not testing the controller directly in this situation - I’ve done  
> that within an RSpec spec. What I’m attempting to do is request a page  
> from our RSpec story. According to  
> [http://tomtenthij.co.uk/2008/1/25/rspec-plain-text-story-runner-on-a-fresh-rails-app](http://tomtenthij.co.uk/2008/1/25/rspec-plain-text-story-runner-on-a-fresh-rails-app)  
> , I should be able to call “post” within our story without specifying  
> the method in a controller context. That being said, I have absolutely  
> no idea what object post or get is being called on within the example  
> I’ve linked to.
> 
> Are you aware of another way to request a page? For example, my first  
> instinct is to call “get ‘/posts/1/’” when considering the story line  
> “When I visit the post details page”, and this does not work.

I’ve solved this by doing a complete overview of the differences between  
the example code in that article and my code. It seems I missed  
defining the type of story to run. In the story itself, it needs to  
have:

with\_steps\_for(:login) do  
run\_local\_story “login\_story”, :type =\> RailsStory  
end

This RailsStory thing isn’t in much of the sample RSpec code I’ve run  
across, but by defining the type, you gain access to some Rails commands  
like get and post.

---

<div class="post-metadata">

**Author:** ![glenatron](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/94ad74/32.png) [@glenatron](https://www.ruby-forum.com/u/glenatron)\
**Post date:** [June 1, 2008, 6:23pm UTC](https://www.ruby-forum.com/t/rspec-post-get-and-sessions-in-a-story/139095/5 "2008-06-01T18:23:53Z")

</div>

On Fri, May 30, 2008 at 5:56 PM, Ben M.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > > default controller actions are:
> 
> run\_local\_story “login\_story”, :type =\> RailsStory  
> end
> 
> This RailsStory thing isn’t in much of the sample RSpec code I’ve run  
> across, but by defining the type, you gain access to some Rails commands  
> like get and post.

Yes, and keep in mind that RailsStory stories use the integration  
testing support from Rails, which is slightly different from the  
functional testing support used in controller specs, so you want to  
post/get… a path rather than an action.

## You might also want to consider the rspec specific mailing list [http://rubyforge.org/mailman/listinfo/rspec-users](http://rubyforge.org/mailman/listinfo/rspec-users)

Rick DeNatale

My blog on Ruby  
[http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)
