RSpec Post/Get and Sessions in a Story

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?

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[]?

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
, 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.

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
, 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.

On Fri, May 30, 2008 at 5:56 PM, Ben M.
[email protected] 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

Rick DeNatale

My blog on Ruby
http://talklikeaduck.denhaven2.com/