Want more features on Pastebin? Sign Up, it's FREE!
Guest

user_pages_spec.rb

By: a guest on Apr 23rd, 2012  |  syntax: Ruby  |  size: 3.31 KB  |  views: 41  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. require 'spec_helper'
  2.  
  3. describe "UserPages" do
  4.  
  5.   subject { page }
  6.  
  7.   describe "index" do
  8.     sign_in FactoryGirl.create(:user)
  9.     FactoryGirl.create(:user, :name => "Bob", :email => "bob@eg.com")
  10.     FactoryGirl.create(:user, :name=> "Ben", :email => "ben@eg.com")
  11.     visit users_path
  12.   end
  13.  
  14.   it { should have_selector('title', :text => 'All users') }
  15.  
  16.   it "should list each user" do
  17.     User.all.each do |user|
  18.       page.should have_selector('li', :text => user.name)
  19.     end
  20.   end
  21.  
  22.  
  23.   describe "signup page" do
  24.     before { visit signup_path }
  25.     it { should have_selector('h1',      :text => 'Sign up') }
  26.     it { should have_selector('title',   :text => full_title('Sign up')) }
  27.   end
  28.  
  29.   describe "profile page" do
  30.     let(:user) { FactoryGirl.create(:user) }
  31.     before { visit user_path(user) }
  32.  
  33.     it { should have_selector('h1',      :text => user.name )}
  34.     it { should have_selector('title',   :text => user.name )}
  35.   end
  36.  
  37.   describe "signup" do
  38.  
  39.     before { visit signup_path }
  40.  
  41.     let(:submit) { "Create my account" }
  42.  
  43.     describe "with invalid information" do
  44.       it "should not create a user" do
  45.         expect { click_button submit }.not_to change(User, :count)
  46.       end
  47.     end
  48.  
  49.     describe "with valid information" do
  50.       before do
  51.         fill_in "Name",         :with => "Example User"
  52.         fill_in "Email",        :with => "user@example.com"
  53.         fill_in "Password",     :with => "foobar"
  54.         fill_in "Confirmation", :with => "foobar"
  55.       end
  56.  
  57.       it "should create a user" do
  58.         expect { click_button submit }.to change(User, :count).by(1)
  59.       end
  60.  
  61.  
  62.       describe "after saving the user" do
  63.         before {click_button submit }
  64.  
  65.         let(:user) { User.find_by_email('user@example.com') }
  66.  
  67.         it { should have_selector('title', :text => user.name) }
  68.         it { should have_selector('div.alert.alert-success', :text => 'Welcome') }
  69.         it { should have_link('Sign out') }
  70.       end
  71.     end
  72.   end
  73.  
  74.   describe "edit" do
  75.     let(:user) { FactoryGirl.create(:user) }
  76.    
  77.     before do
  78.       sign_in user
  79.       visit edit_user_path(user)
  80.     end
  81.  
  82.     describe "page" do
  83.       it { should have_selector('h1',    :text => "Update your profile") }
  84.       it { should have_selector('title', :text => "Edit user") }
  85.       it { should have_link('change', :href => 'http://gravatar.com/emails') }
  86.     end
  87.  
  88.       describe "with invalid information" do
  89.         before { click_button "Save Changes" }
  90.  
  91.         it { should have_content('error') }
  92.       end
  93.  
  94.       describe "with valid information" do
  95.         let(:new_name)  { "New Name" }
  96.         let(:new_email) { "new@example.com" }
  97.         before do
  98.           fill_in "Name",             :with => new_name
  99.           fill_in "Email",            :with => new_email
  100.           fill_in "Password",         :with => user.password
  101.           fill_in "Confirmation", :with => user.password
  102.           click_button "Save Changes"
  103.         end
  104.  
  105.         it { should have_selector('title', :text => new_name) }
  106.         it { should have_selector('div.alert.alert-success') }
  107.         it { should have_link('Sign out', :href => signout_path) }
  108.         specify { user.reload.name.should  == new_name }
  109.         specify { user.reload.email.should == new_email }
  110.       end
  111.   end
  112. end
clone this paste RAW Paste Data