[rspec-users] Restful Authentication and Cucumber

David Chelimsky dchelimsky at gmail.com
Thu Nov 27 06:30:47 EST 2008

On Wed, Nov 26, 2008 at 11:58 PM, Andrei Erdoss <erdoss at gmail.com> wrote:
> Hello,
> I am using Restful Authentication and I would like to login in Cucumber. I
> am having trouble keeping the user logged in. I tried finding a solution for
> this everywhere. Only source is this article:
> http://afreshcup.com/2008/10/09/authentication-in-cucumber-tests/
> This is how my feature looks like:
> Scenario: Register new place
>     Given I am logged in as a user
>     And I am on the new place page with parent place "Georgia"
>     When I fill in "Name" with "Atlanta"
>     And I press "Create"
>     And I should see "Atlanta"
> This is the step:
> Given /I am logged in as a user/ do
>    @current_user = Factory.define :user do |u|
>      u.name 'the user'
>      u.email 'erdoss at gmail.com'
>      u.login  'the_login'
>      u.password 'password'
>      u.password_confirmation 'password'
>    end
>    visits "/login"
>    fills_in("login", :with => "the_login")
>    fills_in("password", :with => "password")
>    clicks_button("Log in")
> end
> # places controller
> before_filter :login_required, :only => [:create]
>   # GET /places/new
>   def new
>     @place = @parent_place.children.create
>     respond_to do |format|
>       format.html # new.html.erb
>     end
>   end
>   # POST /places
>   def create
>     @place = @parent_place.children.create(params[:place])
>     respond_to do |format|
>       if @place.save
>         flash[:notice] = 'Place was successfully created.'
>         format.html { redirect_to(@place) }
>       else
>         format.html { render :action => "new" }
>       end
>     end
>   end
> The "Given I am logged in as a user" step passes, along with all the steps,
> except the last one (And I should see "Atlanta"). The problem is that the
> login page is shown and not the show page with the new place created.
> What do I have to set in Cucumber so that it acts as if the user is logged
> in? In know that in Rspec :login_required should return true. What's the
> equivalent of that in Cucumber?

I'm not sure what you mean by "in RSpec :login_required should return
true," but that might be a subject for a different thread.

When using Cucumber with Rails you're dealing with a wrapped rails
integration test. Because integration tests (in rails) are designed to
support multiple user sessions within a single test (scenario in the
case of Cucumber), you don't have any direct access to the internals
of any controllers.

With that, the problem you are having seems to be that the login step
is not actually working. Just because the step turns green doesn't
mean the User is actually logged in. There are no expectations being
set that would fail, and it's possible that the Factory.define method
doesn't raise errors if it doesn't actually create the User (I'm
guessing you're using Factory Girl, but I have no experience with it
myself yet).

To debug this, I'd add an expectation to the Given step about
something on the page. When someone logs in, does the page say
something like "Welcome, the user"? If so, you can expect that text to
be there.

Also, check to make sure that the "the user" User is actually in the
database. Something like User.find_by_name("the user").should_not


More information about the rspec-users mailing list