Last active
August 29, 2015 14:21
-
-
Save bernstein7/cbc2b2f28a02feb81cf1 to your computer and use it in GitHub Desktop.
RSpec basics
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# This file is copied to spec/ when you run 'rails generate rspec:install' | |
ENV["RAILS_ENV"] = 'test' | |
require 'simplecov' | |
SimpleCov.start 'rails' | |
require File.expand_path("../../config/environment", __FILE__) | |
require 'rspec/rails' | |
require 'fakeredis/rspec' | |
require 'sidekiq/testing' | |
# Requires supporting ruby files with custom matchers and macros, etc, | |
# in spec/support/ and its subdirectories. | |
Dir[Rails.root.join("spec/support/**/*.rb")].each { |f| require f } | |
DatabaseCleaner.strategy = :truncation | |
DatabaseCleaner.orm = "mongoid" | |
Capybara.default_driver = :selenium | |
Capybara.default_wait_time = 30 | |
# Note: Do not disable_net_connect! because thirdparty request | |
WebMock.allow_net_connect! | |
Sidekiq::Testing.fake! | |
# Capybara.register_driver :chrome do |app| | |
# Capybara::Selenium::Driver.new(app, :browser => :chrome) | |
# end | |
# Capybara.javascript_driver = :chrome | |
RSpec::Sidekiq.configure do |config| | |
# Clears all job queues before each example | |
config.clear_all_enqueued_jobs = true # default => true | |
# Whether to use terminal colours when outputting messages | |
config.enable_terminal_colours = true # default => true | |
# Warn when jobs are not enqueued to Redis but to a job array | |
config.warn_when_jobs_not_processed_by_sidekiq = true # default => true | |
end | |
RSpec.configure do |config| | |
config.order = "random" | |
config.raise_errors_for_deprecations! | |
config.before :each do | |
DatabaseCleaner.start | |
end | |
config.after(:each) do | |
Redis.new.flushall | |
DatabaseCleaner.clean | |
Sidekiq::Worker.clear_all | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'spec_helper' | |
describe Manage::CompaniesController, type: :controller do | |
let(:super_admin) { user_admin } | |
before :each do | |
# populate_countries_cities | |
allow_any_instance_of(ActionController::TestRequest) | |
.to receive(:session).and_return(token: super_admin.sso_token) | |
end | |
describe 'GET #index' do | |
let!(:first) { FactoryGirl.create :taxipixi } | |
let!(:second) { FactoryGirl.create :ace } | |
it 'populate an array of all companies @companies' do | |
get :index | |
expect(assigns(:companies)).to match_array([first, second]) | |
end | |
it 'have a positive responce status 200' do | |
get :index | |
expect(response.status).to eq(200) | |
end | |
it 'render template index' do | |
get :index | |
expect(response).to render_template :index | |
end | |
end | |
describe 'GET #new' do | |
it 'assigns a new Company to @company' do | |
get :new, format: :js | |
expect(assigns(:company)).to be_a_new(Company) | |
end | |
it 'have a positive responce status 200' do | |
get :new, format: :js | |
expect(response.status).to eq(200) | |
end | |
it 'render template new' do | |
get :new, format: :js | |
expect(response).to render_template :new | |
end | |
end | |
describe 'GET #edit' do | |
let(:company) { FactoryGirl.create :taxipixi } | |
it 'assigns a Company to @company' do | |
get :edit, id: company, format: :js | |
expect(assigns(:company)).to eq company | |
end | |
it 'have a positive responce status 200' do | |
get :edit, id: company, format: :js | |
expect(response.status).to eq(200) | |
end | |
it 'render template edit' do | |
get :edit, id: company, format: :js | |
expect(response).to render_template :edit | |
end | |
end | |
describe 'POST #create' do | |
it 'create a new Company in the database' do | |
expect { post :create, company: attributes_for(:taxipixi) } | |
.to change(Company, :count).by(1) | |
end | |
it 'redirects to companies#index' do | |
post :create, company: attributes_for(:taxipixi) | |
expect(response).to redirect_to manage_companies_path | |
end | |
end | |
describe 'PUT #update' do | |
let!(:company) { FactoryGirl.create :taxipixi } | |
it 'update a new Company in the database' do | |
put :update, | |
id: company, | |
company: attributes_for(:taxipixi, company_name: 'New name') | |
company.reload | |
expect(company.company_name).to eq('New name') | |
end | |
it 'redirects to companies#index' do | |
put :update, | |
id: company, | |
company: attributes_for(:taxipixi, company_name: 'New name') | |
expect(response).to redirect_to manage_companies_path | |
end | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
FactoryGirl.define do | |
factory :order do | |
requested_pickup_time { Time.now.utc + 5.minutes } | |
requested_pickup_time_offset -330 | |
notified_cab_at { Time.now.utc } | |
notified_cab_about { Faker::Lorem.paragraphs(1) } | |
pickup_time { Time.now.utc + 5.minutes} | |
arrival_time { Time.now.utc + 10*60 } | |
elapse_time { Time.now.utc } | |
distance { rand(10) } | |
estimated_distance { "%04d" % rand(10000) } | |
estimated_duration { "%03d" % rand(1000) } | |
estimated_pickup_time { Time.now.utc } | |
estimated_arrival_time { Time.now.utc + 10*60 } | |
accepted_time { Time.now.utc + 10*60 } | |
requested_cab_types { ["economy"] } | |
fake_job "Driver" | |
sub_status "Active" | |
app_version { rand(1..20) } | |
taxipixi_message { Faker::Lorem.paragraphs(1) } | |
customer_comments { Faker::Lorem.paragraphs(1) } | |
selected_cab_type "economy" #{ ["regular", "premium", "economy"].sample.to_s } | |
city 'Delhi/NCR' | |
country 'India' | |
api_version { rand(10) } | |
country_phone_code '+911' | |
os { Enum::Order::OS[:options].first } | |
name { Faker::Name.name } | |
promotion_code { SecureRandom.hex 5 } | |
days { rand(1..30) } | |
ip_address '127.298.60' | |
sequence(:phone) { |n| "00#{n}" } | |
sequence(:email) { |n| "email#{n}@example.com" } | |
association :customer, :factory => :customer | |
association :company, :factory => :taxipixi | |
association :promotion, :factory => :promotion | |
start_location { build(:start_location_india) } | |
end_location { build(:end_location_india) } | |
after(:create) do |order, evaluator| | |
order.cab = FactoryGirl.create("cab_#{order.country}_#{order.city.gsub(/\W/,'_')}".downcase.to_sym) | |
order.tradearea = order.cab.tradearea | |
order.save | |
end | |
trait :end_location_malaysia do | |
end_location { build(:end_location_malaysia) } | |
end | |
trait :with_temporary_cab_id do | |
temporary_cab_id { rand(1000) } | |
end | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'spec_helper' | |
describe Cab do | |
describe 'validations' do | |
it { is_expected.to validate_presence_of(:name) } | |
it { is_expected.to_not allow_value("", nil).for(:name) } | |
it { is_expected.to validate_presence_of(:phone) } | |
it { is_expected.to_not allow_value("", nil).for(:phone) } | |
it { is_expected.to validate_presence_of(:car) } | |
it { is_expected.to_not allow_value("", nil).for(:car) } | |
it { is_expected.to validate_presence_of(:registration_number) } | |
it { is_expected.to_not allow_value("", nil).for(:registration_number) } | |
end | |
describe 'acts_as_assigned' do | |
it 'has assign job 0 by default' do | |
expect(subject.assign_job).to be == 0 | |
end | |
it 'should have acts_as_assigned' do | |
expect(Cab).to respond_to(:acts_as_assigned) | |
end | |
end | |
describe '#select_suited_orders' do | |
let(:order) { FactoryGirl.build(:order, tradearea: cab.tradearea) } | |
let(:incorrect_order) { FactoryGirl.build(:order, tradearea: cab.tradearea) } | |
let(:p2p_t_r_order) { FactoryGirl.build(:order, tradearea: cab.tradearea, | |
requested_pickup_time: 7.hours.from_now) } | |
let(:full_day_t_r_order) { FactoryGirl.build(:order, tradearea: cab.tradearea, | |
requested_pickup_time: 30.hours.from_now, | |
order_type: 'hourly', | |
package_type: 'FULL DAY') } | |
let(:non_full_day_t_r_order) { FactoryGirl.build(:order, tradearea: cab.tradearea, | |
requested_pickup_time: 7.hours.from_now, | |
order_type: 'hourly', | |
package_type: '8 HOURS') } | |
let(:cab) { FactoryGirl.build(:cab_india_delhi_ncr) } | |
subject { JSON.parse(cab.select_suited_orders(@time_range).to_json).count } | |
context "when have one order" do | |
it "expect order count to change by 1" do | |
expect {order.save}.to change(Order, :count).by(1) | |
end | |
end | |
context "when incorrect car category" do | |
before do | |
order.requested_cab_types = (["regular", "premium", "economy"] - [order.requested_cab_types]).first | |
order.save | |
end | |
it "zero correct results" do | |
is_expected.to eq(0) | |
end | |
end | |
context "when incorrect tradearea" do | |
before do | |
order.tradearea = FactoryGirl.build :tradearea_malaysia_kuala_lumpur | |
order.save | |
end | |
it "zero correct results" do | |
is_expected.to eq(0) | |
end | |
end | |
context "with default time range" do | |
before do | |
order.save # suits current time | |
p2p_t_r_order.save # suits future time for p2p (> current time, < p2p max time) | |
full_day_t_r_order.save # suits future time for FULL_DAY (> current time, < full day max time), but should not be present | |
end | |
it "3 correct results among record" do | |
pending 'need to debug' | |
is_expected.to eq(3) | |
end | |
end | |
context "with <untill_midnight> time rage" do | |
before do | |
@time_range = 'untill_midnight' | |
order.save # suits current time | |
full_day_t_r_order.save # suits future time for FULL_DAY (> current time, < full day max time), but should not be present | |
end | |
it "1 correct results among record" do | |
pending 'need to debug' | |
is_expected.to eq(1) | |
end | |
end | |
context "with <for_tomorrow> time rage" do | |
before do | |
@time_range = 'for_tomorrow' | |
order.requested_pickup_time = Time.now.utc.end_of_day - 5.minutes | |
order.cab_id = cab.id | |
order.save # suits current time, should not be present | |
full_day_t_r_order.requested_pickup_time = Time.now.utc.tomorrow.beginning_of_day + 6.hours | |
full_day_t_r_order.save # suits future time for FULL_DAY (> current time, < full day max time) | |
end | |
it "1 correct results among record" do | |
pending 'need to debug' | |
is_expected.to eq(1) | |
end | |
end | |
end | |
describe '#set_location' do | |
# TODO: write spec for existed code | |
let(:cab) { create(:driver, | |
:with_cab_online_times, | |
updated_location_at: Time.now.utc) } | |
let(:mongoid_point) { build(:mongo_point) } | |
let(:latitude) { mongoid_point.latitude * 1e6 } | |
let(:longitude) { mongoid_point.longitude * 1e6 } | |
let(:cab_tracking_day) { cab.cab_tracking_days.last } | |
subject { cab.set_location latitude, longitude } | |
after do | |
Timecop.return | |
end | |
context 'on first login' do | |
let(:cab) { create(:driver, updated_location_at: nil) } | |
before do | |
Timecop.freeze(Time.utc(2015, 12, 15, 10)) # 15 Dec 2015 10:00 | |
end | |
it 'create CabOnlineTime instance' do | |
expect{subject}.to change{ CabOnlineTime.count }.by(1) | |
end | |
end | |
context 'when cab already logged in today' do | |
before do | |
Timecop.freeze(Time.utc(2015, 12, 15, 10)) # 15 Dec 2015 10:00 | |
cab.reload | |
Timecop.freeze(Time.utc(2015, 12, 15, 10, 5)) # 15 Dec 2015 10:05 | |
end | |
it 'not create CabTrackingDay instance' do | |
expect{subject}.to_not change{ CabOnlineTime.count } | |
end | |
end | |
context 'when cab updated location on time' do | |
before do | |
Timecop.freeze(Time.utc(2015, 12, 15, 10)) # 15 Dec 2015 10:00 | |
cab.reload | |
Timecop.freeze(Time.utc(2015, 12, 15, 10, 2)) # 15 Dec 2015 10:02 | |
cab.set_location latitude, longitude | |
cab.reload | |
end | |
it 'increase online time with expire time' do | |
expect(cab.cab_online_times.today_time).to eq(420) # 120 + 300 | |
end | |
end | |
context 'when cab was away more than expire time' do | |
before do | |
Timecop.freeze(Time.utc(2015, 12, 15, 10)) # 15 Dec 2015 10:00 | |
cab.reload | |
Timecop.freeze(Time.utc(2015, 12, 15, 10, 6)) # 15 Dec 2015 10:06 | |
end | |
it 'create CabOnlineTime instance' do | |
expect{subject}.to change{ CabOnlineTime.count }.by(1) | |
end | |
it 'increase only last online time' do | |
cab.set_location latitude, longitude | |
expect(cab.cab_online_times.today_time).to eq(600) # 300 + 300 | |
end | |
end | |
context 'when cab worked some, then was away, than have worked again' do | |
before do | |
Timecop.freeze(Time.utc(2015, 12, 15, 10)) # 15 Dec 2015 10:00 | |
cab.reload | |
2.step(10, 2) do |i| | |
Timecop.freeze(Time.utc(2015, 12, 15, 10, i)) # 10:02 - 10:10 | |
cab.set_location latitude, longitude | |
end # online_time 900 | |
0.step(10, 5) do |i| | |
Timecop.freeze(Time.utc(2015, 12, 15, 11, i)) # 11:00 - 11:10 | |
cab.set_location latitude, longitude | |
end # online_time +900 | |
cab.reload | |
end | |
it 'create CabOnlineTime instance' do | |
expect(CabOnlineTime.count).to eq(2) | |
end | |
it 'have correct online time' do | |
expect(cab.cab_online_times.today_time).to eq(1800) | |
end | |
end | |
end | |
describe '#set_metered_order' do | |
let(:cab) { FactoryGirl.build(:cab_india_delhi_ncr) } | |
let(:order) { FactoryGirl.build(:order, tradearea: cab.tradearea) } | |
it 'should correctly assign order id' do | |
cab.set_metered_order(order) | |
expect(cab.metered_order_id).to_not be_nil | |
end | |
it 'should correctly build association' do | |
cab.set_metered_order(order) | |
expect(cab.metered_order).to be_kind_of(Order) | |
end | |
end | |
describe '#reset_metered_order' do | |
let(:cab) { FactoryGirl.build(:cab_india_delhi_ncr) } | |
it 'should assign order id to nil' do | |
cab.reset_metered_order | |
expect(cab.metered_order_id).to be_nil | |
end | |
it 'should correctly build association' do | |
cab.reset_metered_order | |
expect(cab.metered_order).to be_nil | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment