Skip to content

Instantly share code, notes, and snippets.

@Will-Sommers
Created March 5, 2012 21:01
Show Gist options
  • Save Will-Sommers/1981068 to your computer and use it in GitHub Desktop.
Save Will-Sommers/1981068 to your computer and use it in GitHub Desktop.
Rails Tutorial
require 'spec_helper'
describe "Authentication" do
subject { page }
describe "signin" do
before { visit signin_path }
describe "with invalid information" do
before { click_button "Sign in" }
it { should have_selector('h1', text: 'Sign in') }
it { should have_selector('div.flash.error', text: 'Invalid') }
describe "after visiting another page" do
before { click_link "Home" }
it { should_not have_selector('div.flash.error') }
end
end
end
describe "with valid information" do
let(:user) { FactoryGirl.create(:user) }
before { sign_in(user) }
it { should have_selector('title', text: user.name) }
it { should have_link('Profile', href: user_path(user)) }
it { should have_link('Settings', href: edit_user_path(user)) }
it { should have_link('Sign out', href: signout_path) }
it { should_not have_link('Sign in', href: signin_path) }
describe "followed by signout" do
before { click_link "Sign out" }
it { should have_link('Sign in') }
end
end
end
<% provide :title, "Edit user" %>
<h1>Edit user</h1>
<%= form_for(@user) do |f| %>
<%= render 'shared/error_messages' %>
<div class="field">
<%= f.label :name %><br />
<%= f.text_field :name %>
</div>
<div class="field">
<%= f.label :email %><br />
<%= f.text_field :email %>
</div>
<div class="field">
<%= f.label :password %><br />
<%= f.password_field :password %>
</div>
<div class="field">
<%= f.label :password_confirmation, "Confirmation" %><br />
<%= f.password_field :password_confirmation %>
</div>
<div class="actions">
<%= f.submit "Update" %>
</div>
<% end %>
<div>
<%= gravatar_for(@user) %>
<a href="http://gravatar.com/emails">change</a>
</div>
Failures:
1) UsersController GET 'name:string' returns http success
Failure/Error: get 'name:string'
AbstractController::ActionNotFound:
The action 'name:string' could not be found for UsersController
# ./spec/controllers/users_controller_spec.rb:6:in `block (3 levels) in <top (required)>'
2) UsersController GET 'email:string' returns http success
Failure/Error: get 'email:string'
AbstractController::ActionNotFound:
The action 'email:string' could not be found for UsersController
# ./spec/controllers/users_controller_spec.rb:13:in `block (3 levels) in <top (required)>'
3) Authentication signin with valid information
Failure/Error: before { sign_in(user) }
NoMethodError:
undefined method `sign_in' for #<RSpec::Core::ExampleGroup::Nested_4::Nested_1::Nested_2:0x007f9a442f2d40>
# ./spec/requests/authentication_pages_spec.rb:23:in `block (4 levels) in <top (required)>'
4) Authentication signin with valid information
Failure/Error: before { sign_in(user) }
NoMethodError:
undefined method `sign_in' for #<RSpec::Core::ExampleGroup::Nested_4::Nested_1::Nested_2:0x007f9a431f5948>
# ./spec/requests/authentication_pages_spec.rb:23:in `block (4 levels) in <top (required)>'
5) Authentication signin with valid information
Failure/Error: before { sign_in(user) }
NoMethodError:
undefined method `sign_in' for #<RSpec::Core::ExampleGroup::Nested_4::Nested_1::Nested_2:0x007f9a41d1b518>
# ./spec/requests/authentication_pages_spec.rb:23:in `block (4 levels) in <top (required)>'
6) Authentication signin with valid information
Failure/Error: before { sign_in(user) }
NoMethodError:
undefined method `sign_in' for #<RSpec::Core::ExampleGroup::Nested_4::Nested_1::Nested_2:0x007f9a431ee0d0>
# ./spec/requests/authentication_pages_spec.rb:23:in `block (4 levels) in <top (required)>'
7) Authentication signin with valid information
Failure/Error: before { sign_in(user) }
NoMethodError:
undefined method `sign_in' for #<RSpec::Core::ExampleGroup::Nested_4::Nested_1::Nested_2:0x007f9a432e5dd0>
# ./spec/requests/authentication_pages_spec.rb:23:in `block (4 levels) in <top (required)>'
8) Authentication signin with valid information followed by signout
Failure/Error: before { sign_in(user) }
NoMethodError:
undefined method `sign_in' for #<RSpec::Core::ExampleGroup::Nested_4::Nested_1::Nested_2::Nested_1:0x007f9a43634860>
# ./spec/requests/authenticatio
require 'spec_helper'
describe "UserPages" do
subject{ page }
describe "profile page" do
let(:user) { FactoryGirl.create(:user) }
before { visit user_path(user) }
it { should have_selector('h1', text: user.name) }
it { should have_selector('title', test: user.name) }
end
describe "signup page" do
before { visit signup_path }
it { should have_selector('h1', text: 'Sign up') }
it { should have_selector('title', text: full_title('Sign up')) }
end
describe "signup" do
before {visit signup_path }
describe "error messages" do
before { click_button "Sign up" }
let(:error) { 'errors prohibited this user from being saved' }
it { should have_selector('title', text: 'Sign up') }
it { should have_content(error) }
end
describe "with invalid information" do
it "should not create a user" do
expect { click_button "Sign up" }.not_to change(User, :count)
end
end
describe "with valid information" do
before do
fill_in "Name", with: "Example User"
fill_in "Email", with: "[email protected]"
fill_in "Password", with: "foobar"
fill_in "Confirmation", with: "foobar"
end
it "should create a user" do
expect { click_button "Sign up" }.to change(User, :count).by(1)
end
describe "after saving the user" do
before { click_button "Sign up" }
let(:user) { User.find_by_email('[email protected]') }
it {should have_selector('title', text: user.name) }
it {should have_selector('div.flash.success', text: 'Welcome') }
it { should have_link('Sign out') }
end
end
describe "edit" do
let(:user) { FactoryGirl.create(:user) }
before { visit edit_user_path(user) }
describe "page" do
it { should have_selector('h1', text: "Edit user") }
it { should have_selector('title', text: "Edit user") }
it { should have_link('change', href: 'http://gravatar.com/emails') }
end
describe "with valid information" do
let(:user) { FactoryGirl.create(:user) }
let(:new_name) { "New Name" }
let(:new_email) { "[email protected]" }
before do
fill_in "Name", with: new_name
fill_in "Email", with: new_email
fill_in "Password", with: user.password
fill_in "Confirmation", with: user.password
click_button "Update"
end
it { should have_selector('title', text:new_name) }
it { should have_selector('div.flash.success') }
it { should have_link('Sign out', :href=> signout_path) }
specify { user.reload.name.should == new_name }
specify { user.reload.email.should == new_email }
end
end
end
end
class UsersController < ApplicationController
def show
@user = User.find(params[:id])
end
def new
@user = User.new
end
def create
@user = User.new(params[:user])
if @user.save
sign_in @user
flash[:success] = "Welcome to the Sample App!"
redirect_to @user
else
render 'new'
end
end
def edit
@user = User.find(params[:id] )
end
def update
@user = User.find(params[:id])
if @user.update_attributes(params[:user])
flash[:success] = "Profile updated"
sign_in @user
redirect_to @user
else
render 'edit'
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment