Skip to content

Commit

Permalink
Feature/request specs (#121)
Browse files Browse the repository at this point in the history
* renamed factories

* added request spec for company#new

* added rubocop-rspec

* removed features

* added create spec
  • Loading branch information
keshavbiswa authored Feb 8, 2022
1 parent 0546c7a commit 3eb0a07
Show file tree
Hide file tree
Showing 10 changed files with 149 additions and 2 deletions.
33 changes: 33 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ require:
- rubocop-packaging
- rubocop-performance
- rubocop-rails
- rubocop-rspec

AllCops:
TargetRubyVersion: 2.7
Expand Down Expand Up @@ -302,3 +303,35 @@ Performance/DeletePrefix:
Performance/DeleteSuffix:
Enabled: true

# RSpec cops from rubocop-rspec
# https://github.com/rubocop-hq/rubocop-rspec/blob/master/config/default.yml

RSpec/ExampleLength:
Description: Checks for long examples.
Enabled: true
Max: 15
StyleGuide: https://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/ExampleLength

RSpec/MultipleExpectations:
Max: 2
Exclude:
- "spec/system/**/*"
- "spec/requests/**/*"
StyleGuide: https://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/MultipleExpectations

RSpec/DescribeClass:
Description: Check that the first argument to the top level describe is a constant.
StyleGuide: https://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/DescribeClass
Exclude:
- spec/requests/*

RSpec/EmptyExampleGroup:
Description: 'Checks if an example group does not include any tests.'
Enabled: false

RSpec/NestedGroups:
Max: 6

RSpec/MessageChain:
Description: Avoid stubbing using `receive_message_chain`.
StyleGuide: http://www.rubydoc.info/gems/rubocop-rspec/RuboCop/Cop/RSpec/MessageChain
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ group :development, :test do
gem "rubocop-packaging", require: false
gem "rubocop-performance", require: false
gem "rubocop-rails", require: false
gem "rubocop-rspec", "~> 2.8", require: false

# Use RSpec as the testing framework
gem "rspec-rails", "~> 5.0", ">= 5.0.2"
Expand Down
3 changes: 3 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,8 @@ GEM
activesupport (>= 4.2.0)
rack (>= 1.1)
rubocop (>= 1.7.0, < 2.0)
rubocop-rspec (2.8.0)
rubocop (~> 1.19)
ruby-progressbar (1.11.0)
ruby-vips (2.1.4)
ffi (~> 1.12)
Expand Down Expand Up @@ -469,6 +471,7 @@ DEPENDENCIES
rubocop-packaging
rubocop-performance
rubocop-rails
rubocop-rspec (~> 2.8)
sass-rails
selenium-webdriver (>= 4.0.0)
shoulda-matchers (~> 5.1)
Expand Down
1 change: 1 addition & 0 deletions app/controllers/company_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ def create
redirect_to root_path
else
flash[:error] = "Company creation failed"
render :new, status: :unprocessable_entity
Rails.logger.error "DEBUG::COMPANY_CONTROLLER::CREATE"
end
end
Expand Down
File renamed without changes.
1 change: 1 addition & 0 deletions spec/factories/user.rb → spec/factories/users.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@
last_name { Faker::Name.last_name.gsub(/\W/, "") }
email { Faker::Internet.safe_email }
password { Faker::Internet.password }
confirmed_at { Date.today }
end
end
7 changes: 5 additions & 2 deletions spec/rails_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
# Prevent database truncation if the environment is production
abort("The Rails environment is running in production mode!") if Rails.env.production?
require "rspec/rails"
require "support/factory_bot"
require "devise"
# Add additional requires below this line. Rails is not loaded until this point!

# Requires supporting ruby files with custom matchers and macros, etc, in
Expand All @@ -23,7 +23,7 @@
# directory. Alternatively, in the individual `*_spec.rb` files, manually
# require only the support files necessary.
#
# Dir[Rails.root.join('spec', 'support', '**', '*.rb')].sort.each { |f| require f }
Dir[Rails.root.join("spec", "support", "**", "*.rb")].sort.each { |f| require f }

# Checks for pending migrations and applies them before tests are run.
# If you are not using ActiveRecord, you can remove these lines.
Expand Down Expand Up @@ -75,4 +75,7 @@
config.filter_rails_from_backtrace!
# arbitrary gems may also be filtered via:
# config.filter_gems_from_backtrace("gem name")

config.include Devise::Test::IntegrationHelpers, type: :request
config.include Warden::Test::Helpers
end
69 changes: 69 additions & 0 deletions spec/requests/companies/create_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
# frozen_string_literal: true

require "rails_helper"

RSpec.describe "Companies#create", type: :request do
let (:user) { create(:user) }

context "when authenticated" do
before do
sign_in user
end

context "when company is valid" do
before do
send_request(:post, company_path, params: {
company: {
name: "Test Company",
address: "test address",
business_phone: "Test phone",
country: "India",
timezone: "IN",
base_currency: "Rs",
standard_price: "1000",
fiscal_year_end: "April",
date_format: "DD/MM/YYYY"
}
})
end

it "creates a new company" do
expect(Company.count).to eq(1)
end

it "sets the company_id to current_user" do
expect(user.company_id).to eq(Company.first.id)
end

it "redirects to root_path " do
expect(response).to have_http_status(:redirect)
end
end
context "when company is invalid" do
before do
send_request(:post, company_path, params: {
company: {
business_phone: "12345677",
timezone: "",
base_currency: "",
standard_price: "",
fiscal_year_end: "",
date_format: ""
}
})
end

it "creates a new company" do
expect(response.body).to include("Company creation failed")
end

it "sets the company_id to current_user" do
expect(Company.count).to eq(0)
end

it "redirects to root_path " do
expect(response).to have_http_status(:unprocessable_entity)
end
end
end
end
22 changes: 22 additions & 0 deletions spec/requests/companies/new_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# frozen_string_literal: true

require "rails_helper"

RSpec.describe "Companies#new", type: :request do
let (:user) { create(:user) }

context "When authenticated" do
before do
sign_in user
get new_company_path
end

it "is successful " do
expect(response).to be_successful
end

it "renders Company#new page" do
expect(response.body).to include("Setup Org")
end
end
end
14 changes: 14 additions & 0 deletions spec/support/helpers/request_parser.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# frozen_string_literal: true

def send_request(request_type, url, headers: {}, params: {})
case request_type
when :get
get url, headers: headers, params: params
when :post
post url, headers: headers, params: params
when :put
put url, headers: headers, params: params
when :delete
delete url, headers: headers, params: params
end
end

0 comments on commit 3eb0a07

Please sign in to comment.