-
-
Save jwo/64151078628663aa7577 to your computer and use it in GitHub Desktop.
stack overflow answer for 13093403
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Users::RegistrationsController < Devise::RegistrationsController | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment