Skip to content

Instantly share code, notes, and snippets.

@Frank004
Created February 26, 2016 03:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Frank004/8aa4acf20f60a3666a42 to your computer and use it in GitHub Desktop.
Save Frank004/8aa4acf20f60a3666a42 to your computer and use it in GitHub Desktop.
require 'ffaker'
FactoryGirl.define do
factory :company do
name "Company Demo"
address {FFaker::Address.street_address}
city "Ponce"
state "PR"
country "PR"
phone {FFaker::PhoneNumberCU.e164_home_work_phone_number}
subdomain "CompanyDemo"
end
end
class Company < ActiveRecord::Base
#---------------------Association----------------------------------
has_many :service_users, dependent: :destroy
has_many :client_users, dependent: :destroy
has_many :projects, dependent: :destroy
has_many :employees, dependent: :destroy
has_many :sections
has_many :trainings, dependent: :destroy
has_many :incidents, dependent: :destroy
has_many :accidents, dependent: :destroy
#-------------Call backs-----------------------------------------
before_save :to_lower
after_create :create_tenant #----- create Subdomain
after_destroy :destroy_tenant #----- destroy Subdomain
private
#------------------Create or Delete Tenant-----------------------------------
def create_tenant
Apartment::Tenant.create(subdomain)
end
def destroy_tenant
Apartment::Tenant.drop(subdomain)
end
end
require 'rails_helper'
RSpec.describe Company, type: :model do
before do
@company = build(:company)
end
#------------------validation fails--------------------------
it 'is invalid without a name' do
expect(build(:company, name: nil)).to_not be_valid
end
it 'is invalid without a address' do
expect(build(:company, address: nil)).to_not be_valid
end
it 'is invalid without a city' do
expect(build(:company, city: nil)).to_not be_valid
end
it 'is invalid without a country' do
expect(build(:company, country: nil)).to_not be_valid
end
it 'is invalid without a phone' do
expect(build(:company, phone: nil )).to_not be_valid
end
#------------------validation pass--------------------------
it 'has a valid factory' do
expect(build(:company)).to be_valid
end
it 'should be a valid name' do
expect(build(:company, name: @company.name)).to be_valid
end
it 'should be a valid address' do
expect(build(:company, address: @company.address)).to be_valid
end
it 'should be a valid city' do
expect(build(:company, city: @company.city)).to be_valid
end
it 'should be a valid country' do
expect(build(:company, country: @company.country)).to be_valid
end
it 'should be a valid phone' do
expect(build(:company, phone: @company.phone)).to be_valid
end
#------------------Methods--------------------------
it 'full_company is the Name and the City' do
expect(@company.full_company).to eql("Company Demo - Ponce")
end
it 'the subdomain to be downcase and parameterize' do
expect(@company.to_lower).to eql("companydemo")
end
it 'Change the Country PR to Puerto Rico' do
expect(@company.country_name).to eql("Puerto Rico")
end
#------------------Association--------------------------
describe do
it { should have_many(:service_users) }
end
describe do
it { should have_many(:client_users) }
end
describe do
it { should have_many(:projects) }
end
describe do
it { should have_many(:sections) }
end
describe do
it { should have_many(:trainings) }
end
describe do
it { should have_many(:incidents) }
end
describe do
it { should have_many(:accidents) }
end
end
$ bundle exec rspec spec/models/project_spec.rb
F
Failures:
1) Project has a valid factory
Failure/Error: Apartment::Tenant.create(subdomain)
Apartment::TenantExists:
The schema companydemo already exists.
# ./app/models/company.rb:67:in `create_tenant'
# ./spec/models/project_spec.rb:5:in `block (2 levels) in <top (required)>'
# ------------------
# --- Caused by: ---
# PG::DuplicateSchema:
# ERROR: schema "companydemo" already exists
# ./app/models/company.rb:67:in `create_tenant'
Finished in 1.22 seconds (files took 4.13 seconds to load)
1 example, 1 failure
Failed examples:
rspec ./spec/models/project_spec.rb:12 # Project has a valid factory
require 'rails_helper'
RSpec.describe Project, type: :model do
before do
@project = build(:project)
end
#------------------validation pass--------------------------
it 'has a valid factory' do
expect(build(:project)).to be_valid
end
end
require 'ffaker'
FactoryGirl.define do
factory :project do
name "Project"
budget_hours 1000
association :company, factory: :company
end
end
RSpec.configure do |config|
Shoulda::Matchers.configure do |config|
config.integrate do |with|
with.test_framework :rspec
with.library :rails
end
end
config.before(:suite) do
# Clean all tables to start
DatabaseCleaner.clean_with :truncation
# Use transactions for tests
DatabaseCleaner.strategy = :transaction
# Truncating doesn't drop schemas, ensure we're clean here, app *may not* exist
Apartment::Tenant.drop('app') rescue nil
# Create the default tenant for our tests
Company.create!(
name: 'Influitive Corp.',
subdomain: 'app',
address: '311 12th Street East Elkridge, MD 21075',
city: 'Buffalo',
state: 'NY',
country: 'USA',
phone: '7876774434'
)
end
config.before(:each) do
# Start transaction for this test
DatabaseCleaner.start
# Switch into the default tenant
Apartment::Tenant.switch! 'app'
end
config.after(:each) do
# Reset tentant back to `public`
Apartment::Tenant.reset
# Rollback transaction
DatabaseCleaner.clean
end
config.expect_with :rspec do |expectations|
expectations.include_chain_clauses_in_custom_matcher_descriptions = true
end
config.mock_with :rspec do |mocks|
mocks.verify_partial_doubles = true
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment