Skip to content

Instantly share code, notes, and snippets.

@raderj89
Last active August 29, 2015 14:05
Show Gist options
  • Save raderj89/244d29547a7d6540f0d4 to your computer and use it in GitHub Desktop.
Save raderj89/244d29547a7d6540f0d4 to your computer and use it in GitHub Desktop.
# spec/controllers/properties_controller_spec.rb
require 'spec_helper'
describe PropertiesController do
let(:manager_invitation) { create(:manager_invitation) }
describe 'GET #new' do
context 'with invalid invitation token' do
it "redirects to root path" do
get :new, invitation_token: 'asdfsae'
expect(response).to redirect_to(root_path)
end
it "flashes the correct notice" do
get :new, invitation_token: 'asdfsae'
expect(flash[:notice]).to match(/^You need to be invited to sign up./)
end
end
context 'with invitation token' do
it "renders the page" do
get :new, invitation_token: manager_invitation.token
expect(response).to render_template(:new)
end
end
end
describe 'POST #create' do
context 'with valid form information' do
it "creates the property" do
expect {
post :create, property: property_params_hash
}.to change(Property, :count).by(1)
end
it "creates the employee" do
expect {
post :create, property: property_params_hash
}.to change(Employee, :count).by(1)
end
it "creates the property_employee record" do
expect {
post :create, property: property_params_hash
}.to change(PropertyEmployee, :count).by(1)
end
it "creates the title record" do
expect {
post :create, property: property_params_hash
}.to change(Title, :count).by(1)
end
it "sets the employee ID in the session" do
post :create, property: property_params_hash
expect(session[:employee_id]).to eq Employee.last.id
end
it "responds with a success flash message" do
post :create, property: property_params_hash
expect(flash[:success]).to match(/^Your account was successfully created!/)
end
it "redirects to the employee invitations page" do
post :create, property: property_params_hash
expect(response).to redirect_to new_employee_invitation_path(Employee.last)
end
end
context 'with invalid form information' do
it "does not create a property record" do
expect {
post :create, property: invalid_property_params_hash
}.to_not change(Property, :count).by(1)
end
it "does not create an employee record" do
expect {
post :create, property: invalid_property_params_hash
}.to_not change(Employee, :count).by(1)
end
it "does not create a property employee record" do
expect {
post :create, property: invalid_property_params_hash
}.to_not change(PropertyEmployee, :count).by(1)
end
it "does not create a title record" do
expect {
post :create, property: invalid_property_params_hash
}.to_not change(Title, :count).by(1)
end
it "does not set the employee ID in the session" do
post :create, property: invalid_property_params_hash
expect(session[:employee_id]).to be_nil
end
it "responds with an error flash message" do
post :create, property: invalid_property_params_hash
expect(flash[:error]).to match(/^There was a problem creating your account./)
end
it "renders the new property page" do
post :create, property: invalid_property_params_hash
expect(response).to render_template(:new)
end
end
end
end
# spec/support/nested_params_helper.rb
module NestedParamsHelper
def property_params_hash
{ name: 'Trump Tower',
address: '165 Central Avenue',
city: 'New York',
state: 'NY',
zip: '11221',
property_employees_attributes:
{ "0"=>
{ title_attributes: { title: 'Super' },
employee_attributes:
{ invitation_token: manager_invitation.token,
is_admin: true,
first_name: 'Jared',
last_name: 'Rader',
nickname: 'Jay Rad',
email: 'raderj89+10@gmail.com',
password: 'password',
password_confirmation: 'password' }
}
}
}
end
def invalid_property_params_hash
{ name: '',
address: '',
city: '',
state: '',
zip: '',
property_employees_attributes:
{ "0"=>
{ title_attributes: { title: '' },
employee_attributes:
{ invitation_token: '',
is_admin: false,
first_name: '',
last_name: '',
nickname: '',
email: '',
password: '',
password_confirmation: '' }
}
}
}
end
end
RSpec.configure do |config|
config.include NestedParamsHelper, type: :controller
end
# spec/features/create_properties_spec.rb
require 'spec_helper'
feature 'creating property' do
let(:manager_invitation) { create(:manager_invitation) }
before do
visit new_property_path(manager_invitation.token)
end
scenario 'with valid information' do
# Property form
fill_in 'Name of Building', with: 'Trump Tower'
fill_in 'Address', with: '123 Rich Person Way'
fill_in 'City', with: 'New York'
find('#property_state').find(:xpath, 'option[2]').select_option
fill_in 'Zip', with: '11372'
attach_file 'Upload a photo of your building', "#{Rails.root}/spec/support/images/test.jpeg"
find(:xpath, "//input[@id='property_property_employees_attributes_0_title_attributes_title']").set "Super"
find(:xpath, "//input[@id='property_property_employees_attributes_0_employee_attributes_invitation_token']").set manager_invitation.token
# Property admin form
fill_in 'First name', with: 'Mike'
fill_in 'Last name', with: 'Super'
fill_in 'Nickname', with: 'SuperMike'
fill_in 'Email', with: manager_invitation.recipient_email
fill_in 'Password', with: 'password'
fill_in 'Password confirmation', with: 'password'
attach_file 'Upload a photo of yourself', "#{Rails.root}/spec/support/images/test.jpeg"
click_button 'Create Account'
expect(page).to have_content("Your account was successfully created!")
expect(current_path).to match(new_employee_invitation_path(Employee.last))
end
scenario 'with invalid credentials' do
click_button 'Create Account'
expect(page).to have_content("There was a problem creating your account.")
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment