Skip to content

Commit

Permalink
Test_model_user
Browse files Browse the repository at this point in the history
  • Loading branch information
samnangcattor committed Jul 10, 2015
1 parent faa4601 commit c8c6db8
Show file tree
Hide file tree
Showing 9 changed files with 258 additions and 22 deletions.
3 changes: 3 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ group :development, :test do
gem "byebug"
gem "web-console", "~> 2.0"
gem "rspec-rails", "~> 3.0"
gem "cucumber-rails", require: false
gem "capybara"
gem "database_cleaner"
end

group :production do
Expand Down
30 changes: 30 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,12 @@ GEM
byebug (5.0.0)
columnize (= 0.9.0)
cancancan (1.10.1)
capybara (2.4.4)
mime-types (>= 1.16)
nokogiri (>= 1.3.3)
rack (>= 1.0.0)
rack-test (>= 0.5.4)
xpath (~> 2.0)
carrierwave (0.10.0)
activemodel (>= 3.2.0)
activesupport (>= 3.2.0)
Expand All @@ -83,7 +89,20 @@ GEM
coffee-script-source (1.9.1.1)
columnize (0.9.0)
connection_pool (2.2.0)
cucumber (1.3.20)
builder (>= 2.1.2)
diff-lcs (>= 1.1.3)
gherkin (~> 2.12)
multi_json (>= 1.7.5, < 2.0)
multi_test (>= 0.1.2)
cucumber-rails (1.4.2)
capybara (>= 1.1.2, < 3)
cucumber (>= 1.3.8, < 2)
mime-types (>= 1.16, < 3)
nokogiri (~> 1.5)
rails (>= 3, < 5)
daemons (1.2.2)
database_cleaner (1.4.1)
debug_inspector (0.0.2)
deep_cloneable (2.1.1)
activerecord (>= 3.1.0, < 5.0.0)
Expand Down Expand Up @@ -213,6 +232,8 @@ GEM
fullcalendar-rails (2.3.1.0)
jquery-rails (>= 3.1.1, < 5.0.0)
momentjs-rails (~> 2.8, >= 2.8.4)
gherkin (2.12.2)
multi_json (~> 1.3)
globalid (0.3.5)
activesupport (>= 4.1.0)
haml (4.0.6)
Expand Down Expand Up @@ -259,6 +280,7 @@ GEM
momentjs-rails (2.10.3)
railties (>= 3.1)
multi_json (1.11.1)
multi_test (0.1.2)
mysql2 (0.3.18)
nested_form (0.3.2)
net-scp (1.2.1)
Expand Down Expand Up @@ -416,6 +438,8 @@ GEM
redis
redis-objects
thin
xpath (2.0.0)
nokogiri (~> 1.3)

PLATFORMS
ruby
Expand All @@ -424,8 +448,11 @@ DEPENDENCIES
bootstrap-sass (= 3.2.0.0)
byebug
cancancan (~> 1.10.1)
capybara
carrierwave
coffee-rails (~> 4.1.0)
cucumber-rails
database_cleaner
deep_cloneable (~> 2.1.1)
devise
factory_girl_rails
Expand Down Expand Up @@ -457,3 +484,6 @@ DEPENDENCIES
unicorn
web-console (~> 2.0)
websocket-rails

BUNDLED WITH
1.10.4
10 changes: 5 additions & 5 deletions app/models/user.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
class User < ActiveRecord::Base
include RailsAdminUser

before_create{self.role ||= Settings.user_roles[:normal]}

has_many :events, class_name: "Schedule", through: :schedule_users, foreign_key: :user_id
has_many :repeats, dependent: :destroy
has_many :schedule_users
Expand All @@ -9,11 +11,6 @@ class User < ActiveRecord::Base
validates :name, presence: true, length: {maximum: 100}
validate :avatar_size

scope :with_ids, ->ids{where id: ids}
scope :without_user, ->user_id{where.not id: user_id}

before_create{self.role ||= Settings.user_roles[:normal]}

devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable

Expand All @@ -23,6 +20,9 @@ class User < ActiveRecord::Base
define_method("#{user_role}?") {user_role == role}
end

scope :with_ids, ->ids{where id: ids}
scope :without_user, ->user_id{where.not id: user_id}

private
def avatar_size
if avatar.size > Settings.max_avatar_file_size.megabytes
Expand Down
29 changes: 14 additions & 15 deletions config/application.rb
Original file line number Diff line number Diff line change
@@ -1,40 +1,39 @@
require File.expand_path("../boot", __FILE__)

# Pick the frameworks you want:
require "active_model/railtie"
require "active_job/railtie"
require "active_record/railtie"
require "action_controller/railtie"
require "action_mailer/railtie"
require "action_view/railtie"
require "sprockets/railtie"
# require "rails/test_unit/railtie"

# Require the gems listed in Gemfile, including any gems
# you"ve limited to :test, :development, or :production.
Bundler.require(*Rails.groups)

module Crb
class Application < Rails::Application
# Settings in config/environments/* take precedence over those specified here.
# Application configuration should go into files in config/initializers
# -- all .rb files in that directory are automatically loaded.

config.middleware.delete Rack::Lock

# Set Time.zone default to the specified zone and make Active Record auto-convert to this zone.
# Run "rake -D time" for a list of tasks for finding time zone names. Default is UTC.

config.time_zone = "Hanoi"
config.active_record.default_timezone = :local
# config.active_record.time_zone_aware_attributes = false

# The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded.

config.i18n.load_path += Dir[Rails.root.join("config", "locales", "*.{rb,yml}").to_s]
config.i18n.default_locale = :en

# Do not swallow errors in after_commit/after_rollback callbacks.
config.active_record.raise_in_transactional_callbacks = true

# Include the authenticity token in remote forms.
config.action_view.embed_authenticity_token_in_remote_forms = true

config.generators do |g|
g.test_framework :rspec,
fixtures: true,
view_specs: true,
helper_specs: false,
routing_specs: false,
controller_specs: true,
request_specs: true
g.fixture_replacement :factory_girl, dir: "spec/factories"
end
end
end
Binary file added spec/factories/avatar.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 6 additions & 2 deletions spec/factories/users.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
FactoryGirl.define do
factory :user do
name {Faker::Company.name}
email {Faker::Internet.email}
sequence(:name) {|n| "example#{n}"}
sequence(:email) {|n| "example#{n}@gmail.com"}
password 123456
password_confirmation 123456
role Settings.user_roles.normal

factory :user_with_avatar do
avatar { File.new("#{Rails.root}/spec/factories/avatar.png") }
end
end
end
85 changes: 85 additions & 0 deletions spec/models/user_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
require "rails_helper"

describe User do
let(:user) {create :user}
subject {user}

describe "#user" do
context "has a valid user" do
it {is_expected.to be_an User}
end
end

describe "#name" do
describe "#validate presence" do
before do
subject.name = nil
subject.save
end
context "should invalid withouth name" do
it {expect(subject.errors[:name].size).to eq 1}
end
end

describe "#validate length" do
context "should invalid when maxinum length large than 100" do
before do
subject.name = "abcdefghij" * 100
subject.save
end
it {expect(subject.errors[:name].size).to eq 1}
end

context "should valid when maxinum length smaller than 100" do
before do
subject.name = "abcdefghij" * 5
subject.save
end
it {expect(subject.errors[:name].size).to eq 0}
end
end
end

describe "#avatar_size_method" do
context "#validate" do
it {allow(subject).to receive(:avatar_size)}
end
end

describe "#avatar" do
before {@another_user = create :user_with_avatar}
context "should validate upload avatar" do
it {expect(@another_user).to be_valid}
end
end

describe "#user_role" do
context "response to check user is normal" do
it {expect(subject.admin?).to eq false}
it {expect(subject.normal?).to eq true}
end

context "response to check user is admin" do
before do
subject.role = Settings.user_roles.admin
subject.save
end
it {expect(subject.admin?).to eq true}
it {expect(subject.normal?).to eq false}
end
end

describe ".with_ids" do
context "return the user if equal with id finding" do
before {@another_user = subject.id}
it {expect(User.with_ids(@another_user)).to include subject}
end
end

describe ".without_user" do
context "return the user if equl with id finding" do
before {@another_user = 12}
it {expect(User.without_user(@another_user)).to include subject}
end
end
end
23 changes: 23 additions & 0 deletions spec/rails_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
ENV["RAILS_ENV"] ||= "test"
require File.expand_path("../../config/environment", __FILE__)

abort("The Rails environment is running in production mode!") if Rails.env.production?

require "spec_helper"
require "rspec/rails"
require "devise"
require "factory_girl_rails"
require "sidekiq"
require "capybara/rspec"
require "rails_admin"

ActiveRecord::Migration.maintain_test_schema!

RSpec.configure do |config|
config.include Devise::TestHelpers, type: :controller
config.include Devise::TestHelpers, type: :view
config.include FactoryGirl::Syntax::Methods
config.fixture_path = "#{::Rails.root}/spec/fixtures"
config.use_transactional_fixtures = true
config.infer_spec_type_from_file_location!
end
92 changes: 92 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
# 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
end

# The settings below are suggested to provide a good initial experience
# with RSpec, but feel free to customize to your heart's content.
=begin
# These two settings work together to allow 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.
config.filter_run :focus
config.run_all_when_everything_filtered = true
# 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://myronmars.to/n/dev-blog/2012/06/rspecs-new-expectation-syntax
# - http://www.teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/
# - http://myronmars.to/n/dev-blog/2014/05/notable-changes-in-rspec-3#new__config_option_to_disable_rspeccore_monkey_patching
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
end

0 comments on commit c8c6db8

Please sign in to comment.