From f58d9f0275d01c0282639d276a634bcc9b09e0f7 Mon Sep 17 00:00:00 2001 From: Vijay Raghavan Aravamudhan Date: Fri, 20 Oct 2017 22:02:21 +0530 Subject: [PATCH] Introduced rspec and wrote the first spec for this codebase. --- .gitignore | 4 +- .rspec | 6 + .ruby-gemset | 1 + .ruby-version | 1 + .simplecov | 11 + Gemfile | 18 +- Gemfile.lock | 57 ++++- README.md | 2 +- config/database.yml | 13 +- db/schema.rb | 396 +++++++++++++++---------------- spec/factories/roles.rb | 9 + spec/models/role_spec.rb | 7 + spec/rails_helper.rb | 64 +++++ spec/spec_helper.rb | 105 ++++++++ spec/support/database_cleaner.rb | 45 ++++ spec/support/factory_girl.rb | 12 + 16 files changed, 546 insertions(+), 205 deletions(-) create mode 100644 .rspec create mode 100644 .ruby-gemset create mode 100644 .ruby-version create mode 100644 .simplecov create mode 100644 spec/factories/roles.rb create mode 100644 spec/models/role_spec.rb create mode 100644 spec/rails_helper.rb create mode 100644 spec/spec_helper.rb create mode 100644 spec/support/database_cleaner.rb create mode 100644 spec/support/factory_girl.rb diff --git a/.gitignore b/.gitignore index b83ba07..4960865 100644 --- a/.gitignore +++ b/.gitignore @@ -22,4 +22,6 @@ /public/system .DS_Store -Passwords.txt \ No newline at end of file +Passwords.txt + +/coverage/ diff --git a/.rspec b/.rspec new file mode 100644 index 0000000..2d3d5d0 --- /dev/null +++ b/.rspec @@ -0,0 +1,6 @@ +--color +--order random +--profile +--backtrace +--format Fuubar +--require rails_helper diff --git a/.ruby-gemset b/.ruby-gemset new file mode 100644 index 0000000..a0e143a --- /dev/null +++ b/.ruby-gemset @@ -0,0 +1 @@ +Bee-Connect diff --git a/.ruby-version b/.ruby-version new file mode 100644 index 0000000..005119b --- /dev/null +++ b/.ruby-version @@ -0,0 +1 @@ +2.4.1 diff --git a/.simplecov b/.simplecov new file mode 100644 index 0000000..89aee9e --- /dev/null +++ b/.simplecov @@ -0,0 +1,11 @@ +SimpleCov.minimum_coverage 7.50 +# TODO: Uncomment once you write tests for each file +# SimpleCov.minimum_coverage_by_file 80 +SimpleCov.refuse_coverage_drop +SimpleCov.formatter = SimpleCov::Formatter::HTMLFormatter +SimpleCov.start :rails do + add_group 'Channels', 'app/channels' + add_group 'Mailers', 'app/mailers' + add_group 'Policies', 'app/policies' + add_filter 'vendor/' +end diff --git a/Gemfile b/Gemfile index e239f8a..340d438 100644 --- a/Gemfile +++ b/Gemfile @@ -5,6 +5,8 @@ git_source(:github) do |repo_name| "https://github.com/#{repo_name}.git" end +ruby '2.4.1' + gem 'rails', '~> 5.0.1' gem 'puma', '~> 3.0' gem 'sass-rails', '~> 5.0' @@ -21,7 +23,21 @@ group :development do gem 'meta_request' gem 'web-console' gem "awesome_print", require:"ap" +end + +group :development, :test do + gem 'rspec-rails', '~> 3.6' end + +group :test do + gem 'fuubar', '~> 2.2.0' + gem 'database_cleaner', '~> 1.6' + gem 'factory_girl_rails', '~> 4.6' + gem 'faker', '~> 1.7' + gem 'shoulda', '~> 3.5.0' + gem 'simplecov', '~> 0.14', require: false +end + group :production do gem 'mysql2', '~> 0.4.5' end @@ -37,4 +53,4 @@ gem 'kaminari' gem 'rails_admin' gem 'remotipart', '~> 1.2' -gem 'sidekiq' \ No newline at end of file +gem 'sidekiq' diff --git a/Gemfile.lock b/Gemfile.lock index 11e62e9..806f971 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -62,6 +62,7 @@ GEM coffee-script-source (1.12.2) concurrent-ruby (1.0.4) connection_pool (2.2.1) + database_cleaner (1.6.1) debug_inspector (0.0.2) devise (4.2.0) bcrypt (~> 3.0) @@ -69,10 +70,22 @@ GEM railties (>= 4.1.0, < 5.1) responders warden (~> 1.2.3) + diff-lcs (1.3) + docile (1.1.5) erubis (2.7.0) execjs (2.7.0) + factory_girl (4.8.1) + activesupport (>= 3.0.0) + factory_girl_rails (4.8.0) + factory_girl (~> 4.8.0) + railties (>= 3.0.0) + faker (1.8.4) + i18n (~> 0.5) font-awesome-rails (4.7.0.1) railties (>= 3.2, < 5.1) + fuubar (2.2.0) + rspec-core (~> 3.0) + ruby-progressbar (~> 1.4) globalid (0.3.7) activesupport (>= 4.1.0) haml (4.0.7) @@ -87,6 +100,7 @@ GEM thor (>= 0.14, < 2.0) jquery-ui-rails (5.0.5) railties (>= 3.2.16) + json (2.1.0) kaminari (0.17.0) actionpack (>= 3.0.0) activesupport (>= 3.0.0) @@ -121,7 +135,7 @@ GEM cocaine (~> 0.5.5) mime-types mimemagic (~> 0.3.0) - puma (3.6.2) + puma (3.10.0) rack (2.0.1) rack-contrib (1.2.0) rack (>= 0.9.1) @@ -173,6 +187,24 @@ GEM remotipart (1.3.1) responders (2.3.0) railties (>= 4.2.0, < 5.1) + rspec-core (3.7.0) + rspec-support (~> 3.7.0) + rspec-expectations (3.7.0) + diff-lcs (>= 1.2.0, < 2.0) + rspec-support (~> 3.7.0) + rspec-mocks (3.7.0) + diff-lcs (>= 1.2.0, < 2.0) + rspec-support (~> 3.7.0) + rspec-rails (3.7.1) + actionpack (>= 3.0) + activesupport (>= 3.0) + railties (>= 3.0) + rspec-core (~> 3.7.0) + rspec-expectations (~> 3.7.0) + rspec-mocks (~> 3.7.0) + rspec-support (~> 3.7.0) + rspec-support (3.7.0) + ruby-progressbar (1.9.0) sass (3.4.23) sass-rails (5.0.6) railties (>= 4.0.0, < 6) @@ -180,6 +212,12 @@ GEM sprockets (>= 2.8, < 4.0) sprockets-rails (>= 2.0, < 4.0) tilt (>= 1.1, < 3) + shoulda (3.5.0) + shoulda-context (~> 1.0, >= 1.0.1) + shoulda-matchers (>= 1.4.1, < 3.0) + shoulda-context (1.2.2) + shoulda-matchers (2.8.0) + activesupport (>= 3.0.0) sidekiq (4.2.9) concurrent-ruby (~> 1.0) connection_pool (~> 2.2, >= 2.2.0) @@ -188,6 +226,11 @@ GEM simple_form (3.4.0) actionpack (> 4, < 5.1) activemodel (> 4, < 5.1) + simplecov (0.15.1) + docile (~> 1.1.0) + json (>= 1.8, < 3) + simplecov-html (~> 0.10.0) + simplecov-html (0.10.2) sprockets (3.7.1) concurrent-ruby (~> 1.0) rack (> 1, < 3) @@ -226,7 +269,11 @@ DEPENDENCIES awesome_print bootstrap-sass coffee-rails (~> 4.2) + database_cleaner (~> 1.6) devise + factory_girl_rails (~> 4.6) + faker (~> 1.7) + fuubar (~> 2.2.0) haml jbuilder (~> 2.5) jquery-rails @@ -239,13 +286,19 @@ DEPENDENCIES rails_admin redis (~> 3.0) remotipart (~> 1.2) + rspec-rails (~> 3.6) sass-rails (~> 5.0) + shoulda (~> 3.5.0) sidekiq simple_form + simplecov (~> 0.14) sqlite3 tzinfo-data uglifier (>= 1.3.0) web-console +RUBY VERSION + ruby 2.4.1p111 + BUNDLED WITH - 1.13.6 + 1.15.4 diff --git a/README.md b/README.md index 5c71a3e..5b82311 100644 --- a/README.md +++ b/README.md @@ -44,7 +44,7 @@ An advance rails user interaction system. Think like its is mini facebook with l ## Installation -### Devlopment +### Development For development we use sqlite. Feel free to use any database like mysql or sqlite or pgsql. * For windows user you can install imagemagic and add to path and also remember to install redis server on windows. For ubuntu you can follow the guide. diff --git a/config/database.yml b/config/database.yml index dd7f739..9bbda25 100644 --- a/config/database.yml +++ b/config/database.yml @@ -1,9 +1,18 @@ -development: +default: &default adapter: sqlite3 + # For details on connection pooling, see rails configuration guide + # http://guides.rubyonrails.org/configuring.html#database-pooling pool: 5 timeout: 5000 + +development: + <<: *default database: db/development.sqlite3 +test: + <<: *default + database: db/test.sqlite3 + production: adapter: mysql2 encoding: utf8 @@ -11,4 +20,4 @@ production: database: chatounce_production pool: 5 username: laxman - password: chatouncem* \ No newline at end of file + password: chatouncem* diff --git a/db/schema.rb b/db/schema.rb index d9a2e8f..a0fbf79 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -1,198 +1,198 @@ -# This file is auto-generated from the current state of the database. Instead -# of editing this file, please use the migrations feature of Active Record to -# incrementally modify your database, and then regenerate this schema definition. -# -# Note that this schema.rb definition is the authoritative source for your -# database schema. If you need to create the application database on another -# system, you should be using db:schema:load, not running all the migrations -# from scratch. The latter is a flawed and unsustainable approach (the more migrations -# you'll amass, the slower it'll run and the greater likelihood for issues). -# -# It's strongly recommended that you check this file into your version control system. - -ActiveRecord::Schema.define(version: 20170116043801) do - - create_table "chat_rooms", force: :cascade do |t| - t.string "title" - t.integer "user_id" - t.datetime "created_at", null: false - t.datetime "updated_at", null: false - t.index ["user_id"], name: "index_chat_rooms_on_user_id" - end - - create_table "comments", force: :cascade do |t| - t.integer "user_id" - t.integer "post_id" - t.text "content" - t.datetime "created_at", null: false - t.datetime "updated_at", null: false - t.index ["post_id"], name: "index_comments_on_post_id" - t.index ["user_id"], name: "index_comments_on_user_id" - end - - create_table "conversations", force: :cascade do |t| - t.integer "author_id" - t.integer "receiver_id" - t.datetime "created_at", null: false - t.datetime "updated_at", null: false - t.index ["author_id", "receiver_id"], name: "index_conversations_on_author_id_and_receiver_id", unique: true - t.index ["author_id"], name: "index_conversations_on_author_id" - t.index ["receiver_id"], name: "index_conversations_on_receiver_id" - end - - create_table "follows", force: :cascade do |t| - t.integer "following_id", null: false - t.integer "follower_id", null: false - t.datetime "created_at", null: false - t.datetime "updated_at", null: false - t.index ["follower_id"], name: "index_follows_on_follower_id" - t.index ["following_id", "follower_id"], name: "index_follows_on_following_id_and_follower_id", unique: true - t.index ["following_id"], name: "index_follows_on_following_id" - end - - create_table "messages", force: :cascade do |t| - t.text "body" - t.integer "user_id" - t.integer "chat_room_id" - t.datetime "created_at", null: false - t.datetime "updated_at", null: false - t.index ["chat_room_id"], name: "index_messages_on_chat_room_id" - t.index ["user_id"], name: "index_messages_on_user_id" - end - - create_table "notifications", force: :cascade do |t| - t.integer "recipient_id" - t.integer "actor_id" - t.datetime "read_at" - t.string "action" - t.integer "notifiable_id" - t.string "notifiable_type" - t.datetime "created_at", null: false - t.datetime "updated_at", null: false - end - - create_table "personal_messages", force: :cascade do |t| - t.text "body" - t.integer "conversation_id" - t.integer "user_id" - t.datetime "created_at", null: false - t.datetime "updated_at", null: false - t.string "attachment_file_name" - t.string "attachment_content_type" - t.integer "attachment_file_size" - t.datetime "attachment_updated_at" - t.index ["conversation_id"], name: "index_personal_messages_on_conversation_id" - t.index ["user_id"], name: "index_personal_messages_on_user_id" - end - - create_table "posts", force: :cascade do |t| - t.text "content" - t.datetime "created_at", null: false - t.datetime "updated_at", null: false - t.string "attachment_file_name" - t.string "attachment_content_type" - t.integer "attachment_file_size" - t.datetime "attachment_updated_at" - t.integer "user_id" - t.string "postable_type" - t.integer "postable_id" - t.index ["postable_id", "postable_type"], name: "index_posts_on_postable_id_and_postable_type" - t.index ["user_id"], name: "index_posts_on_user_id" - end - - create_table "roles", force: :cascade do |t| - t.string "name" - t.datetime "created_at", null: false - t.datetime "updated_at", null: false - end - - create_table "groups", force: :cascade do |t| - t.string "name" - t.text "description" - t.datetime "created_at", null: false - t.datetime "updated_at", null: false - t.string "thumbnail_image_file_name" - t.string "thumbnail_image_content_type" - t.integer "thumbnail_image_file_size" - t.datetime "thumbnail_image_updated_at" - t.string "timeline_image_file_name" - t.string "timeline_image_content_type" - t.integer "timeline_image_file_size" - t.datetime "timeline_image_updated_at" - end - - create_table "group_memberships", force: :cascade do |t| - t.integer "user_id" - t.integer "group_id" - t.integer "role_id" - t.datetime "created_at", null: false - t.datetime "updated_at", null: false - t.index ["role_id"], name: "index_group_memberships_on_role_id" - t.index ["group_id"], name: "index_group_memberships_on_group_id" - t.index ["user_id", "group_id"], name: "index_group_memberships_on_user_id_and_group_id", unique: true - t.index ["user_id"], name: "index_group_memberships_on_user_id" - end - - create_table "group_messages", force: :cascade do |t| - t.text "body" - t.integer "user_id" - t.integer "group_id" - t.datetime "created_at", null: false - t.datetime "updated_at", null: false - t.index ["group_id"], name: "index_group_messages_on_group_id" - t.index ["user_id"], name: "index_group_messages_on_user_id" - end - - create_table "users", force: :cascade do |t| - t.string "email", default: "", null: false - t.string "encrypted_password", default: "", null: false - t.string "reset_password_token" - t.datetime "reset_password_sent_at" - t.datetime "remember_created_at" - t.integer "sign_in_count", default: 0, null: false - t.datetime "current_sign_in_at" - t.datetime "last_sign_in_at" - t.string "current_sign_in_ip" - t.string "last_sign_in_ip" - t.string "confirmation_token" - t.datetime "confirmed_at" - t.datetime "confirmation_sent_at" - t.string "unconfirmed_email" - t.integer "failed_attempts", default: 0, null: false - t.string "unlock_token" - t.datetime "locked_at" - t.datetime "created_at", null: false - t.datetime "updated_at", null: false - t.string "first_name" - t.string "last_name" - t.string "user_name" - t.string "avatar_file_name" - t.string "avatar_content_type" - t.integer "avatar_file_size" - t.datetime "avatar_updated_at" - t.text "bio" - t.boolean "is_female", default: false - t.integer "role_id" - t.index ["confirmation_token"], name: "index_users_on_confirmation_token", unique: true - t.index ["email"], name: "index_users_on_email", unique: true - t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true - t.index ["role_id"], name: "index_users_on_role_id" - t.index ["unlock_token"], name: "index_users_on_unlock_token", unique: true - t.index ["user_name"], name: "index_users_on_user_name", unique: true - end - - create_table "votes", force: :cascade do |t| - t.string "votable_type" - t.integer "votable_id" - t.string "voter_type" - t.integer "voter_id" - t.boolean "vote_flag" - t.string "vote_scope" - t.integer "vote_weight" - t.datetime "created_at" - t.datetime "updated_at" - t.index ["votable_id", "votable_type", "vote_scope"], name: "index_votes_on_votable_id_and_votable_type_and_vote_scope" - t.index ["voter_id", "voter_type", "vote_scope"], name: "index_votes_on_voter_id_and_voter_type_and_vote_scope" - end - -end +# This file is auto-generated from the current state of the database. Instead +# of editing this file, please use the migrations feature of Active Record to +# incrementally modify your database, and then regenerate this schema definition. +# +# Note that this schema.rb definition is the authoritative source for your +# database schema. If you need to create the application database on another +# system, you should be using db:schema:load, not running all the migrations +# from scratch. The latter is a flawed and unsustainable approach (the more migrations +# you'll amass, the slower it'll run and the greater likelihood for issues). +# +# It's strongly recommended that you check this file into your version control system. + +ActiveRecord::Schema.define(version: 20170116043801) do + + create_table "chat_rooms", force: :cascade do |t| + t.string "title" + t.integer "user_id" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + t.index ["user_id"], name: "index_chat_rooms_on_user_id" + end + + create_table "comments", force: :cascade do |t| + t.integer "user_id" + t.integer "post_id" + t.text "content" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + t.index ["post_id"], name: "index_comments_on_post_id" + t.index ["user_id"], name: "index_comments_on_user_id" + end + + create_table "conversations", force: :cascade do |t| + t.integer "author_id" + t.integer "receiver_id" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + t.index ["author_id", "receiver_id"], name: "index_conversations_on_author_id_and_receiver_id", unique: true + t.index ["author_id"], name: "index_conversations_on_author_id" + t.index ["receiver_id"], name: "index_conversations_on_receiver_id" + end + + create_table "follows", force: :cascade do |t| + t.integer "following_id", null: false + t.integer "follower_id", null: false + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + t.index ["follower_id"], name: "index_follows_on_follower_id" + t.index ["following_id", "follower_id"], name: "index_follows_on_following_id_and_follower_id", unique: true + t.index ["following_id"], name: "index_follows_on_following_id" + end + + create_table "group_memberships", force: :cascade do |t| + t.integer "user_id" + t.integer "group_id" + t.integer "role_id" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + t.index ["group_id"], name: "index_group_memberships_on_group_id" + t.index ["role_id"], name: "index_group_memberships_on_role_id" + t.index ["user_id", "group_id"], name: "index_group_memberships_on_user_id_and_group_id", unique: true + t.index ["user_id"], name: "index_group_memberships_on_user_id" + end + + create_table "group_messages", force: :cascade do |t| + t.text "body" + t.integer "user_id" + t.integer "group_id" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + t.index ["group_id"], name: "index_group_messages_on_group_id" + t.index ["user_id"], name: "index_group_messages_on_user_id" + end + + create_table "groups", force: :cascade do |t| + t.string "name" + t.text "description" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + t.string "thumbnail_image_file_name" + t.string "thumbnail_image_content_type" + t.integer "thumbnail_image_file_size" + t.datetime "thumbnail_image_updated_at" + t.string "timeline_image_file_name" + t.string "timeline_image_content_type" + t.integer "timeline_image_file_size" + t.datetime "timeline_image_updated_at" + end + + create_table "messages", force: :cascade do |t| + t.text "body" + t.integer "user_id" + t.integer "chat_room_id" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + t.index ["chat_room_id"], name: "index_messages_on_chat_room_id" + t.index ["user_id"], name: "index_messages_on_user_id" + end + + create_table "notifications", force: :cascade do |t| + t.integer "recipient_id" + t.integer "actor_id" + t.datetime "read_at" + t.string "action" + t.integer "notifiable_id" + t.string "notifiable_type" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + end + + create_table "personal_messages", force: :cascade do |t| + t.text "body" + t.integer "conversation_id" + t.integer "user_id" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + t.string "attachment_file_name" + t.string "attachment_content_type" + t.integer "attachment_file_size" + t.datetime "attachment_updated_at" + t.index ["conversation_id"], name: "index_personal_messages_on_conversation_id" + t.index ["user_id"], name: "index_personal_messages_on_user_id" + end + + create_table "posts", force: :cascade do |t| + t.text "content" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + t.string "attachment_file_name" + t.string "attachment_content_type" + t.integer "attachment_file_size" + t.datetime "attachment_updated_at" + t.integer "user_id" + t.string "postable_type" + t.integer "postable_id" + t.index ["postable_id", "postable_type"], name: "index_posts_on_postable_id_and_postable_type" + t.index ["user_id"], name: "index_posts_on_user_id" + end + + create_table "roles", force: :cascade do |t| + t.string "name" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + end + + create_table "users", force: :cascade do |t| + t.string "email", default: "", null: false + t.string "encrypted_password", default: "", null: false + t.string "reset_password_token" + t.datetime "reset_password_sent_at" + t.datetime "remember_created_at" + t.integer "sign_in_count", default: 0, null: false + t.datetime "current_sign_in_at" + t.datetime "last_sign_in_at" + t.string "current_sign_in_ip" + t.string "last_sign_in_ip" + t.string "confirmation_token" + t.datetime "confirmed_at" + t.datetime "confirmation_sent_at" + t.string "unconfirmed_email" + t.integer "failed_attempts", default: 0, null: false + t.string "unlock_token" + t.datetime "locked_at" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + t.string "first_name" + t.string "last_name" + t.string "user_name" + t.string "avatar_file_name" + t.string "avatar_content_type" + t.integer "avatar_file_size" + t.datetime "avatar_updated_at" + t.text "bio" + t.boolean "is_female", default: false + t.integer "role_id" + t.index ["confirmation_token"], name: "index_users_on_confirmation_token", unique: true + t.index ["email"], name: "index_users_on_email", unique: true + t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true + t.index ["role_id"], name: "index_users_on_role_id" + t.index ["unlock_token"], name: "index_users_on_unlock_token", unique: true + t.index ["user_name"], name: "index_users_on_user_name", unique: true + end + + create_table "votes", force: :cascade do |t| + t.string "votable_type" + t.integer "votable_id" + t.string "voter_type" + t.integer "voter_id" + t.boolean "vote_flag" + t.string "vote_scope" + t.integer "vote_weight" + t.datetime "created_at" + t.datetime "updated_at" + t.index ["votable_id", "votable_type", "vote_scope"], name: "index_votes_on_votable_id_and_votable_type_and_vote_scope" + t.index ["voter_id", "voter_type", "vote_scope"], name: "index_votes_on_voter_id_and_voter_type_and_vote_scope" + end + +end diff --git a/spec/factories/roles.rb b/spec/factories/roles.rb new file mode 100644 index 0000000..5fb44e8 --- /dev/null +++ b/spec/factories/roles.rb @@ -0,0 +1,9 @@ +# frozen_string_literal: true + +FactoryGirl.define do + factory :role do + sequence :name do |n| + "#{Faker::Company.profession} #{n}" + end + end +end diff --git a/spec/models/role_spec.rb b/spec/models/role_spec.rb new file mode 100644 index 0000000..0ad2d5b --- /dev/null +++ b/spec/models/role_spec.rb @@ -0,0 +1,7 @@ +# frozen_string_literal: true + +RSpec.describe Role, type: :model do + describe 'relationships' do + it { is_expected.to have_many(:users) } + end +end diff --git a/spec/rails_helper.rb b/spec/rails_helper.rb new file mode 100644 index 0000000..a580ffb --- /dev/null +++ b/spec/rails_helper.rb @@ -0,0 +1,64 @@ +# frozen_string_literal: true + +# This file is copied to spec/ when you run 'rails generate rspec:install' +ENV['RAILS_ENV'] ||= 'test' +require 'simplecov' +require 'spec_helper' +require 'faker' +require File.expand_path('../../config/environment', __FILE__) +# Prevent database truncation if the environment is production +abort('The Rails environment is running in production mode!') if Rails.env.production? +require 'rspec/rails' +I18n.reload! +Faker::Config.locale = I18n.locale + +# Add additional requires below this line. Rails is not loaded until this point! + +# Requires supporting ruby files with custom matchers and macros, etc, in +# spec/support/ and its subdirectories. Files matching `spec/**/*_spec.rb` are +# run as spec files by default. This means that files in spec/support that end +# in _spec.rb will both be required and run as specs, causing the specs to be +# run twice. It is recommended that you do not name files matching this glob to +# end with _spec.rb. You can configure this pattern with the --pattern +# option on the command line or in ~/.rspec, .rspec or `.rspec-local`. +# +# The following line is provided for convenience purposes. It has the downside +# of increasing the boot-up time by auto-requiring all files in the support +# directory. Alternatively, in the individual `*_spec.rb` files, manually +# require only the support files necessary. +# +Dir[Rails.root.join('spec/support/**/*.rb')].each { |f| require f } + +# Checks for pending migration and applies them before tests are run. +# If you are not using ActiveRecord, you can remove this line. +ActiveRecord::Migration.maintain_test_schema! + +RSpec.configure do |config| + # Remove this line if you're not using ActiveRecord or ActiveRecord fixtures + config.fixture_path = Rails.root.join('spec', 'fixtures') + + # If you're not using ActiveRecord, or you'd prefer not to run each of your + # examples within a transaction, remove the following line or assign false + # instead of true. + config.use_transactional_fixtures = false + + # RSpec Rails can automatically mix in different behaviours to your tests + # based on their file location, for example enabling you to call `get` and + # `post` in specs under `spec/controllers`. + # + # You can disable this behaviour by removing the line below, and instead + # explicitly tag your specs with their type, e.g.: + # + # RSpec.describe UsersController, type: :controller do + # # ... + # end + # + # The different available types are documented in the features, such as in + # https://relishapp.com/rspec/rspec-rails/docs + config.infer_spec_type_from_file_location! + + # Filter lines from Rails gems in backtraces. + config.filter_rails_from_backtrace! + # arbitrary gems may also be filtered via: + # config.filter_gems_from_backtrace("gem name") +end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb new file mode 100644 index 0000000..bd47dfe --- /dev/null +++ b/spec/spec_helper.rb @@ -0,0 +1,105 @@ +# frozen_string_literal: true + +# This file was generated by the `rails generate rspec:install` command. Conventionally, all +# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`. +# The generated `.rspec` file contains `--require spec_helper` which will cause +# this file to always be loaded, without a need to explicitly require it in any +# files. +# +# Given that it is always loaded, you are encouraged to keep this file as +# light-weight as possible. Requiring heavyweight dependencies from this file +# will add to the boot time of your test suite on EVERY test run, even for an +# individual file that may not need all of that loaded. Instead, consider making +# a separate helper file that requires the additional dependencies and performs +# the additional setup, and require it from the spec files that actually need +# it. +# +# The `.rspec` file also contains a few flags that are not defaults but that +# users commonly want. +# +# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration +RSpec.configure do |config| + # rspec-expectations config goes here. You can use an alternate + # assertion/expectation library such as wrong or the stdlib/minitest + # assertions if you prefer. + config.expect_with :rspec do |expectations| + # This option will default to `true` in RSpec 4. It makes the `description` + # and `failure_message` of custom matchers include text for helper methods + # defined using `chain`, e.g.: + # be_bigger_than(2).and_smaller_than(4).description + # # => "be bigger than 2 and smaller than 4" + # ...rather than: + # # => "be bigger than 2" + expectations.include_chain_clauses_in_custom_matcher_descriptions = true + end + + # rspec-mocks config goes here. You can use an alternate test double + # library (such as bogus or mocha) by changing the `mock_with` option here. + config.mock_with :rspec do |mocks| + # Prevents you from mocking or stubbing a method that does not exist on + # a real object. This is generally recommended, and will default to + # `true` in RSpec 4. + mocks.verify_partial_doubles = true + + # This option should be set when all dependencies are being loaded + # before a spec run, as is the case in a typical spec helper. It will + # cause any verifying double instantiation for a class that does not + # exist to raise, protecting against incorrectly spelt names. + mocks.verify_doubled_constant_names = true + end + + # This option will default to `:apply_to_host_groups` in RSpec 4 (and will + # have no way to turn it off -- the option exists only for backwards + # compatibility in RSpec 3). It causes shared context metadata to be + # inherited by the metadata hash of host groups and examples, rather than + # triggering implicit auto-inclusion in groups with matching metadata. + config.shared_context_metadata_behavior = :apply_to_host_groups + + # The settings below are suggested to provide a good initial experience + # with RSpec, but feel free to customize to your heart's content. + # # This allows you to limit a spec run to individual examples or groups + # # you care about by tagging them with `:focus` metadata. When nothing + # # is tagged with `:focus`, all examples get run. RSpec also provides + # # aliases for `it`, `describe`, and `context` that include `:focus` + # # metadata: `fit`, `fdescribe` and `fcontext`, respectively. + # config.filter_run_when_matching :focus + # + # # Allows RSpec to persist some state between runs in order to support + # # the `--only-failures` and `--next-failure` CLI options. We recommend + # # you configure your source control system to ignore this file. + # config.example_status_persistence_file_path = "spec/examples.txt" + # + # # Limits the available syntax to the non-monkey patched syntax that is + # # recommended. For more details, see: + # # - http://rspec.info/blog/2012/06/rspecs-new-expectation-syntax/ + # # - http://www.teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/ + # # - http://rspec.info/blog/2014/05/notable-changes-in-rspec-3/#zero-monkey-patching-mode + # config.disable_monkey_patching! + # + # # Many RSpec users commonly either run the entire suite or an individual + # # file, and it's useful to allow more verbose output when running an + # # individual spec file. + # if config.files_to_run.one? + # # Use the documentation formatter for detailed output, + # # unless a formatter has already been configured + # # (e.g. via a command-line flag). + # config.default_formatter = 'doc' + # end + # + # # Print the 10 slowest examples and example groups at the + # # end of the spec run, to help surface which specs are running + # # particularly slow. + # config.profile_examples = 10 + # + # # Run specs in random order to surface order dependencies. If you find an + # # order dependency and want to debug it, you can fix the order by providing + # # the seed, which is printed after each run. + # # --seed 1234 + # config.order = :random + # + # # Seed global randomization in this process using the `--seed` CLI option. + # # Setting this allows you to use `--seed` to deterministically reproduce + # # test failures related to randomization by passing the same `--seed` value + # # as the one that triggered the failure. + # Kernel.srand config.seed +end diff --git a/spec/support/database_cleaner.rb b/spec/support/database_cleaner.rb new file mode 100644 index 0000000..5c26fc5 --- /dev/null +++ b/spec/support/database_cleaner.rb @@ -0,0 +1,45 @@ +# frozen_string_literal: true + +RSpec.configure do |config| + config.use_transactional_fixtures = false + + config.before(:suite) do + if config.use_transactional_fixtures? + raise(<<-MSG) + Delete line `config.use_transactional_fixtures = true` from rails_helper.rb + (or set it to false) to prevent uncommitted transactions being used in + JavaScript-dependent specs. + + During testing, the app-under-test that the browser driver connects to + uses a different database connection to the database connection used by + the spec. The app's database connection would not be able to access + uncommitted transaction data setup over the spec's database connection. + MSG + end + DatabaseCleaner.clean_with(:transaction) + end + + # config.before(:each, js: true) do + # DatabaseCleaner.strategy = :truncation + # end + + config.before(:each) do + DatabaseCleaner.strategy = :transaction + end + + # config.before(:each, type: :feature) do + # DatabaseCleaner.strategy = :transaction unless Capybara.current_driver == :rack_test + # end + + config.before(:each, :transaction_mode) do + DatabaseCleaner.strategy = :transaction + end + + config.before(:each) do + DatabaseCleaner.start + end + + config.append_after(:each) do + DatabaseCleaner.clean + end +end diff --git a/spec/support/factory_girl.rb b/spec/support/factory_girl.rb new file mode 100644 index 0000000..904c850 --- /dev/null +++ b/spec/support/factory_girl.rb @@ -0,0 +1,12 @@ +# frozen_string_literal: true + +RSpec.configure do |config| + config.before(:suite) do + begin + DatabaseCleaner.start + FactoryGirl.lint unless config.files_to_run.one? + ensure + DatabaseCleaner.clean + end + end +end