I started working on BDD through cucumber. (using Rails-3, gem 'cucumber-rails')
I want to redirect to user profile page (/users/id) when successful login.
I defined in controller as (redirect_to user_path(@user)) and the same thing I defined in cucumber as (page.current_path.should == user_path(@user))
In my step_definition
Given /^a user visits the signin page$/ do
visit signin_path
end
When /^he log in as "(.*)\/(.*)"$/ do |email, password|
@email = email
fill_in "email", with: email
fill_in "password", with: password
click_button "Login"
end
Then /^he should see a signin link$/ do
page.should have_link('Sign in', href: signin_path)
end
Then /^he should see his profile page$/ do
@user = User.where("email = ?", @email)
page.current_path.should == user_path(@user)
end
In my controller
class SessionsController < ApplicationController
def create
@user = User.find_by_email(params[:session][:email].downcase)
if @user && @user.authenticate(params[:session][:password])
sign_in @user
redirect_to user_path(@user)
else
flash.now[:error] = 'Invalid email/password combination'
render 'new'
end
end
end
Now when running cucumber, I am getting this error:
expected: "/users/%23%3CActiveRecord::Relation:0x000000062fbec0%3E"
got: "/users/4" (using ==) (RSpec::Expectations::ExpectationNotMetError)
In my features/support/env.rb:
require File.expand_path(File.dirname(__FILE__) + '/../../config/environment')
require 'cucumber/rails'
require 'rspec/expectations'
please answer where I am doing wrong.
This code in your step definition is the problem:
Then /^he should see his profile page$/ do
@user = User.where("email = ?", @email)
page.current_path.should == user_path(@user)
end
User.where
will always return a relation, even if the result is only one User record, hence the ActiveRecord::Relation
part of your error message.
If you can be sure there will only be one User returned, you can swap it out for something like User.find_by_email(@email)
or User.where(:email => @email).first
, to get a single User instance.