secret
Created

stack overflow answer for 13093403

  • Download Gist
registrations_controller_spec.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
require File.dirname(__FILE__) + '/../spec_helper'
 
describe Users::RegistrationsController do
include Devise::TestHelpers
fixtures :all
render_views
 
before(:each) do
@request.env["devise.mapping"] = Devise.mappings[:user]
end
 
describe "POST 'create'" do
describe "failure" do
before(:each) do
@attr = { :email => "", :password => "",
:password_confirmation => "", :display_name => "" }
end
 
it "should not create a user" do
lambda do
post :create, :user_registration => @attr
end.should_not change(User, :count)
end
 
it "should render the 'new' page" do
post :create, :user_registration => @attr
response.should render_template('new')
end
end
 
describe "success" do
before(:each) do
@attr = { :email => "user@example.com",
:password => "foobar01", :password_confirmation => "foobar01", :display_name => "New User" }
end
 
it "should create a user" do
lambda do
post :create, :user => @attr
response.should redirect_to(root_path)
#response.body.should have_selector('h1', :text => "Sample App")
#response.should have_css('h1', :text => "Sample App")
#flash[:success].should == "A message with a confirmation link has been sent to your email address. Please open the link to activate your account."
#response.should have_content "A message with a confirmation link has been sent to your email address. Please open the link to activate your account."
end.should change(User, :count).by(1)
end
 
end
 
end
 
describe "PUT 'update'" do
before(:each) do
@user = FactoryGirl.create(:user)
@user.confirm! # or set a confirmed_at inside the factory. Only necessary if you are using the confirmable module
sign_in @user
end
 
describe "Failure" do
 
before(:each) do
# The following information is valid except for display_name which is too long (max 20 characters)
@attr = { :email => @user.email, :display_name => ("t" * 35), :current_password => @user.password }
end
 
it "should render the 'edit' page" do
put :update, :id => subject.current_user, :user => @attr
 
# HAVE PUT THE DEBUGS THAT I'D LIKE TO GET WORKING FIRST
# Would like to be able to debug and check I'm getting the error(s) I'm expecting
#puts subject.current_user.errors.messages # doesn't show me the errors
# Would like to be able to debug what html is being returned:
#puts page.html # only return the first line of html
 
# Would like to be able to determine that this test is failing for the right reasons
response.body.should include "Display name is too long (maximum is 30 characters)"
assigns[:user].errors[:display_name].should include "is too long (maximum is 30 characters)"
 
 
response.should render_template('edit')
end
end
 
describe "Success" do
 
it "should change the user's display name" do
@attr = { :email => @user.email, :display_name => "Test", :current_password => @user.password }
put :update, :id => subject.current_user, :user => @attr
subject.current_user.reload
response.should redirect_to(root_path)
subject.current_user.display_name == @attr[:display_name]
end
 
end
end
 
describe "authentication of edit/update pages" do
 
describe "for non-signed-in users" do
 
before(:each) do
@user = FactoryGirl.create(:user)
end
 
describe "for non-signed-in users" do
 
it "should deny access to 'edit'" do
get :edit, :id => @user
response.should redirect_to(new_user_session_path)
end
 
it "should deny access to 'update'" do
put :update, :id => @user, :user => {}
response.should redirect_to(new_user_session_path)
end
 
end
 
end
end
 
end
schema.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
# encoding: UTF-8
# This file is auto-generated from the current state of the database. Instead
# of editing this file, please use the migrations feature of Active Record to
# incrementally modify your database, and then regenerate this schema definition.
#
# Note that this schema.rb definition is the authoritative source for your
# database schema. If you need to create the application database on another
# system, you should be using db:schema:load, not running all the migrations
# from scratch. The latter is a flawed and unsustainable approach (the more migrations
# you'll amass, the slower it'll run and the greater likelihood for issues).
#
# It's strongly recommended to check this file into your version control system.
 
ActiveRecord::Schema.define(:version => 20121027180617) do
 
create_table "users", :force => true do |t|
t.string "email", :default => "", :null => false
t.string "encrypted_password", :default => "", :null => false
t.string "display_name"
t.string "reset_password_token"
t.datetime "reset_password_sent_at"
t.datetime "remember_created_at"
t.integer "sign_in_count", :default => 0
t.datetime "current_sign_in_at"
t.datetime "last_sign_in_at"
t.string "current_sign_in_ip"
t.string "last_sign_in_ip"
t.string "confirmation_token"
t.datetime "confirmed_at"
t.datetime "confirmation_sent_at"
t.string "unconfirmed_email"
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
end
 
add_index "users", ["email"], :name => "index_users_on_email", :unique => true
add_index "users", ["reset_password_token"], :name => "index_users_on_reset_password_token", :unique => true
 
end
user.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11
class User < ActiveRecord::Base
# Include default devise modules. Others available are:
# :token_authenticatable, :confirmable,
# :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable, :confirmable,
:recoverable, :rememberable, :trackable, :validatable
 
# Setup accessible (or protected) attributes for your model
attr_accessible :email, :password, :password_confirmation, :remember_me, :display_name
validates_length_of :display_name, maximum: 30
end
users:regsitrations_controller.rb
Ruby
1 2
class Users::RegistrationsController < Devise::RegistrationsController
end

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.