Skip to content

Instantly share code, notes, and snippets.

@drager
Last active August 29, 2015 14:00
Show Gist options
  • Save drager/11404913 to your computer and use it in GitHub Desktop.
Save drager/11404913 to your computer and use it in GitHub Desktop.
require 'spec_helper'
require 'faker'
feature 'Create user' do
scenario 'creates a user successfully with a valid username, email and password' do
register_with Faker::Internet.user_name, 'test123', 'test123', Faker::Internet.email
user_sees_success_message "Registered successfully!"
end
scenario 'notifies the user if his username is invalid' do
register_with nil, 'test123', 'test123', Faker::Internet.email
user_sees_error_message "Username can't be blank"
end
scenario 'notifies the user if his username is to short' do
register_with 'el', 'test123', 'test123', Faker::Internet.email
user_sees_error_message "Username is too short (minimum is 3 characters)"
end
scenario 'notifies the user if his email is invalid' do
register_with Faker::Internet.user_name, 'test123', 'test123', nil
user_sees_error_message "Email can't be blank"
end
scenario 'notifies the user if his password is to short' do
register_with Faker::Internet.user_name, 'test', 'test', Faker::Internet.email
user_sees_error_message "Password is too short (minimum is 6 characters)"
end
scenario 'notifies the user if his password doesnt match' do
register_with Faker::Internet.user_name, 'test123', 'test321', Faker::Internet.email
user_sees_error_message "Password confirmation doesn't match Password"
end
def user_sees_error_message(message)
expect(page).to have_css '.error_messages', message
end
def user_sees_success_message(message)
expect(page).to have_css '.notice', message
end
def register_with(username, password, password_confirm, email)
visit new_user_path
fill_in 'Username', with: username
fill_in 'Password', with: password
fill_in 'Password confirmation', with: password_confirm
fill_in 'Email', with: email
click_button 'Register'
end
end
require 'spec_helper'
require 'faker'
feature 'Signing in' do
background do
@email = '[email protected]'
@password = 'test123'
@user = create(:user, username: 'ellen.u', email: @email, password: @password, password_confirmation: @password)
end
scenario 'signs the user in successfully with a valid email and password' do
sign_in_with(@email, @password)
user_sees_success_message 'Logged in!'
end
scenario 'notifies the user if his email or password is invalid' do
sign_in_with(Faker::Internet.email, Faker::Internet.password)
user_sees_error_message 'Invalid email or password'
end
def user_sees_error_message(message)
expect(page).to have_css '.alert', message
end
def user_sees_success_message(message)
expect(page).to have_css '.notice', message
end
def sign_in_with(email, password)
visit session_path
fill_in 'Email', with: email
fill_in 'Password', with: password
click_button 'Login'
end
end
require 'spec_helper'
require 'faker'
feature 'Signing out' do
background do
@user = create(:user, username: 'ellen.u', email: '[email protected]', password: 'test123', password_confirmation: 'test123')
end
scenario 'signs the user out successfully' do
login_user(@user)
logout_user
user_sees_success_message 'Logged out!'
end
def user_sees_error_message(message)
expect(page).to have_css '.alert', message
end
def user_sees_success_message(message)
expect(page).to have_css '.notice', message
end
def login_user(user)
visit session_path
fill_in 'Email', with: user.email
fill_in 'Password', with: user.password
click_button 'Login'
end
def logout_user
visit root_path
click_link 'Logout'
end
end
class User < ActiveRecord::Base
has_many :posts
has_many :topics
has_secure_password
validates :password, length: { minimum: 6 }
validates :username, presence: true, uniqueness: true, length: { minimum: 3 }
validates :email, presence: true, uniqueness: true
end
require 'spec_helper'
describe User do
it "is valid if all parameters match" do
build(:user).should be_valid
end
it { should have_secure_password }
it { should validate_presence_of(:email) }
it { should ensure_length_of(:username).is_at_least(3) }
it { should ensure_length_of(:password).is_at_least(6) }
it do
should validate_confirmation_of(:password)
end
it { should validate_uniqueness_of(:username) }
it { should validate_uniqueness_of(:email) }
end
require 'faker'
FactoryGirl.define do
factory :user do
email {Faker::Internet.email}
username {Faker::Internet.user_name}
first_name {Faker::Name.first_name}
last_name {Faker::Name.last_name}
password 'test123'
password_confirmation 'test123'
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment