Commit 9cadb602 by vulehuan

rspec for product model

parent b7c7f19b
......@@ -49,7 +49,12 @@ group :development, :test do
gem 'faker', '1.1.2'
end
group :development do
gem 'sunspot_solr', "~> 2.1.0"
gem 'sunspot_solr', "~> 2.1.0"
gem "pry"
gem "thin"
gem "better_errors"
gem 'metric_fu'
gem 'rack-mini-profiler'
end
group :test do
gem 'capybara', "~> 2.1.0"
......
......@@ -26,16 +26,36 @@ GEM
thread_safe (~> 0.1)
tzinfo (~> 0.3.37)
arel (4.0.1)
arrayfields (4.9.0)
atomic (1.1.14)
awesome_print (1.2.0)
bcrypt-ruby (3.0.1)
better_errors (1.0.1)
coderay (>= 1.0.0)
erubis (>= 2.6.6)
bluff (0.1.0)
breadcrumbs_on_rails (2.3.0)
builder (3.1.4)
cane (2.6.0)
parallel
capybara (2.1.0)
mime-types (>= 1.16)
nokogiri (>= 1.3.3)
rack (>= 1.0.0)
rack-test (>= 0.5.4)
xpath (~> 2.0)
chronic (0.10.2)
churn (0.0.34)
chronic (>= 0.2.3)
hirb
json_pure
main
ruby_parser (~> 3.0)
sexp_processor (~> 4.1)
code_analyzer (0.4.3)
sexp_processor
code_metrics (0.1.1)
coderay (1.1.0)
coffee-rails (4.0.1)
coffee-script (>= 2.2.0)
railties (>= 4.0.0, < 5.0)
......@@ -43,8 +63,11 @@ GEM
coffee-script-source
execjs
coffee-script-source (1.6.3)
colored (1.2)
daemons (1.1.9)
diff-lcs (1.2.4)
erubis (2.7.0)
eventmachine (1.0.3)
execjs (2.0.2)
factory_girl (4.2.0)
activesupport (>= 3.0.0)
......@@ -53,7 +76,15 @@ GEM
railties (>= 3.0.0)
faker (1.1.2)
i18n (~> 0.5)
fattr (2.2.1)
flay (2.4.0)
ruby_parser (~> 3.0)
sexp_processor (~> 4.0)
flog (4.2.0)
ruby_parser (~> 3.1, > 3.1.0)
sexp_processor (~> 4.4)
hike (1.2.3)
hirb (0.7.1)
i18n (0.6.5)
jbuilder (1.5.2)
activesupport (>= 3.0.0)
......@@ -62,9 +93,32 @@ GEM
railties (>= 3.0, < 5.0)
thor (>= 0.14, < 2.0)
json (1.8.1)
json_pure (1.8.1)
mail (2.5.4)
mime-types (~> 1.16)
treetop (~> 1.4.8)
main (5.2.0)
arrayfields (>= 4.7.4)
chronic (>= 0.6.2)
fattr (>= 2.2.0)
map (>= 5.1.0)
map (6.5.1)
method_source (0.8.2)
metric_fu (4.4.4)
bluff
cane (~> 2.5, >= 2.5.2)
churn (~> 0.0.28)
code_metrics (~> 0.1)
coderay
flay (~> 2.1, >= 2.0.1)
flog (~> 4.1, >= 4.1.1)
metric_fu-Saikuro (>= 1.1.1.0)
multi_json
rails_best_practices (~> 1.14, >= 1.14.3)
redcard
reek (~> 1.3, >= 1.3.3)
roodi (~> 3.1)
metric_fu-Saikuro (1.1.1.0)
mime-types (1.25)
mini_portile (0.5.2)
minitest (4.7.5)
......@@ -72,9 +126,17 @@ GEM
mysql2 (0.3.13)
nokogiri (1.6.0)
mini_portile (~> 0.5.0)
parallel (0.9.0)
polyglot (0.3.3)
pr_geohash (1.0.0)
pry (0.9.6)
coderay (>= 0.9.8)
method_source (>= 0.6.5)
ruby_parser (>= 2.0.5)
slop (~> 2.1.0)
rack (1.5.2)
rack-mini-profiler (0.1.31)
rack (>= 1.1.3)
rack-test (0.6.2)
rack (>= 1.0)
rails (4.0.0)
......@@ -85,6 +147,15 @@ GEM
bundler (>= 1.3.0, < 2.0)
railties (= 4.0.0)
sprockets-rails (~> 2.0.0)
rails_best_practices (1.14.4)
activesupport
awesome_print
code_analyzer (>= 0.4.3)
colored
erubis
i18n
require_all
ruby-progressbar
railties (4.0.0)
actionpack (= 4.0.0)
activesupport (= 4.0.0)
......@@ -94,6 +165,14 @@ GEM
rdoc (3.12.2)
json (~> 1.4)
recaptcha-mailhide (0.1.1)
redcard (1.1.0)
reek (1.3.4)
ruby2ruby (~> 2.0.2)
ruby_parser (~> 3.2)
sexp_processor
require_all (1.3.1)
roodi (3.1.1)
ruby_parser (~> 3.2, >= 3.2.2)
rsolr (1.0.9)
builder (>= 2.1.2)
rspec-core (2.14.6)
......@@ -107,6 +186,12 @@ GEM
rspec-core (~> 2.14.0)
rspec-expectations (~> 2.14.0)
rspec-mocks (~> 2.14.0)
ruby-progressbar (1.2.0)
ruby2ruby (2.0.6)
ruby_parser (~> 3.1)
sexp_processor (~> 4.0)
ruby_parser (3.2.2)
sexp_processor (~> 4.1)
sass (3.2.12)
sass-rails (4.0.1)
railties (>= 4.0.0, < 5.0)
......@@ -115,6 +200,8 @@ GEM
sdoc (0.3.20)
json (>= 1.1.3)
rdoc (~> 3.10)
sexp_processor (4.4.0)
slop (2.1.0)
sprockets (2.10.0)
hike (~> 1.2)
multi_json (~> 1.0)
......@@ -132,6 +219,10 @@ GEM
rails (>= 3)
sunspot (= 2.1.0)
sunspot_solr (2.1.0)
thin (1.6.0)
daemons (>= 1.0.9)
eventmachine (>= 1.0.0)
rack (>= 1.5.0)
thor (0.18.1)
thread_safe (0.1.3)
atomic
......@@ -145,6 +236,7 @@ GEM
uglifier (2.2.1)
execjs (>= 0.3.0)
multi_json (~> 1.0, >= 1.0.2)
validates_existence (0.8.0)
will_paginate (3.0.4)
will_paginate-bootstrap (1.0.0)
will_paginate (>= 3.0.3)
......@@ -156,6 +248,7 @@ PLATFORMS
DEPENDENCIES
bcrypt-ruby (~> 3.0.0)
better_errors
breadcrumbs_on_rails (~> 2.3.0)
capybara (~> 2.1.0)
coffee-rails (~> 4.0.0)
......@@ -163,7 +256,10 @@ DEPENDENCIES
faker (= 1.1.2)
jbuilder (~> 1.2)
jquery-rails
metric_fu
mysql2
pry
rack-mini-profiler
rails (= 4.0.0)
recaptcha-mailhide (~> 0.1.1)
rspec-rails (~> 2.14.0)
......@@ -171,7 +267,9 @@ DEPENDENCIES
sdoc
sunspot_rails (~> 2.1.0)
sunspot_solr (~> 2.1.0)
thin
turbolinks
uglifier (>= 1.3.0)
validates_existence (>= 0.4)
will_paginate (= 3.0.4)
will_paginate-bootstrap (~> 1.0.0)
class Product < ActiveRecord::Base
validates :name, :code, :condition, :image_medium, :price, :price_currency, presence: true
belongs_to :product_category
belongs_to :product_category
belongs_to :user
validates :product_category, presence: true
validates :user, presence: true
validates :name, :code, :condition, :image_medium, :price, presence: true
validates :name, length: { maximum: 255 }
validates :price, :product_category_id, :user_id, numericality: true
validates :price_currency, presence: true, if: "price > 0"
searchable do
text :name, :description
string :headline, :code, :condition, :price_currency
......
class ProductCategory < ActiveRecord::Base
has_many :product
end
\ No newline at end of file
end
class User < ActiveRecord::Base
has_many :product
Please register or sign in to reply
before_save { self.email = email.downcase }
before_create :create_remember_token
validates :name, presence: true, length: { maximum: 50 }
......@@ -19,4 +20,4 @@ class User < ActiveRecord::Base
def create_remember_token
self.remember_token = User.encrypt(User.new_remember_token)
end
end
\ No newline at end of file
end
......@@ -2,21 +2,27 @@ require 'spec_helper'
describe Product do
before do
@product = Product.new(
@product_category = ProductCategory.new(
name: "Product category name"
)
@product_category.save
@user = User.new(name: "Example User", email: "user@example.com", password: "foobar", password_confirmation: "foobar")
@user.save
@product = Product.new(
name: "Example User",
description: "user@example.com",
headline: "foobar",
availability: "foobar",
code: '',
condition: '',
code: 'Code',
condition: 'Condition',
image_small: '',
image_medium: '',
image_medium: 'Image Path',
review_rate: 0,
review_count: 0,
price_currency: '',
price_currency: 'USD',
price: 0,
product_category_id: 0,
user_id: 0,
product_category_id: @product_category.id,
user_id: @user.id,
status: 1
)
end
......@@ -42,26 +48,60 @@ describe Product do
it { should be_valid }
describe "when name is not present" do
before { @product.name = " " }
it { should_not be_valid }
end
describe "when name is too long" do
before { @product.name = "a" * 256 }
it { should_not be_valid }
end
describe "when code is not present" do
before { @product.code = " " }
it { should_not be_valid }
end
describe "when image medium is not present" do
describe "when condition is not present" do
before { @product.condition = " " }
it { should_not be_valid }
end
describe "when image medium is not present" do
before { @product.image_medium = " " }
it { should_not be_valid }
end
describe "when price is not a number" do
before { @product.price = "string" }
it { should_not be_valid }
end
describe "when input price" do
before { @product.price = 99.99 }
describe "and currency is not present" do
before { @product.price_currency = " " }
it { should_not be_valid }
end
end
describe "when product category id is not a number" do
before { @product.product_category_id = "string" }
it { should_not be_valid }
end
describe "when product category is not exist" do
before { @product.product_category_id = 0 }
it { should_not be_valid }
end
describe "when product user id is not a number" do
before { @product.user_id = "string" }
it { should_not be_valid }
end
describe "when user is not exist" do
before { @product.user_id = 0 }
it { should_not be_valid }
end
end
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment