Commit a1896d33 by Truong Ba Dieu

tmp

parent c0dab2d3
--color
#--warnings
--require spec_helper
--format d
-r turnip/rspec
\ No newline at end of file
...@@ -57,13 +57,24 @@ gem "breadcrumbs_on_rails" ...@@ -57,13 +57,24 @@ gem "breadcrumbs_on_rails"
gem 'momentjs-rails', '>= 2.8.1' gem 'momentjs-rails', '>= 2.8.1'
gem 'bootstrap3-datetimepicker-rails', '~> 4.7.14' gem 'bootstrap3-datetimepicker-rails', '~> 4.7.14'
gem 'draper', '~> 1.3' gem 'draper', '~> 1.3'
group :test do gem 'sunspot_rails'
gem 'sunspot_solr'
group :development, :test do
# gem 'capybara' # Integration test tool to simulate a user on a website. # gem 'capybara' # Integration test tool to simulate a user on a website.
# gem 'capybara_minitest_spec' # MiniTest::Spec expectations for Capybara node matchers. # gem 'capybara_minitest_spec' # MiniTest::Spec expectations for Capybara node matchers.
gem 'rspec' gem 'rspec'
gem 'rspec-rails', '~> 3.0.1' gem 'capybara'
gem 'factory_girl_rails', '~> 4.4.1' gem 'factory_girl_rails', '~> 4.4.1'
gem 'faker' gem 'faker'
gem 'email_spec' gem 'email_spec'
gem 'database_cleaner' gem 'database_cleaner'
gem 'shoulda-matchers'
gem "turnip"
gem 'rspec-core'
gem 'rspec-expectations'
gem 'rspec-mocks'
gem 'rspec-rails', '~> 3.0.1'
gem 'rspec-support'
gem "pry"
end end
\ No newline at end of file
...@@ -53,6 +53,13 @@ GEM ...@@ -53,6 +53,13 @@ GEM
builder (3.2.2) builder (3.2.2)
byebug (5.0.0) byebug (5.0.0)
columnize (= 0.9.0) columnize (= 0.9.0)
capybara (2.4.4)
mime-types (>= 1.16)
nokogiri (>= 1.3.3)
rack (>= 1.0.0)
rack-test (>= 0.5.4)
xpath (~> 2.0)
coderay (1.1.0)
coffee-rails (4.1.0) coffee-rails (4.1.0)
coffee-script (>= 2.2.0) coffee-script (>= 2.2.0)
railties (>= 4.0.0, < 5.0) railties (>= 4.0.0, < 5.0)
...@@ -99,6 +106,8 @@ GEM ...@@ -99,6 +106,8 @@ GEM
thor (~> 0.14) thor (~> 0.14)
font-awesome-sass (4.3.2.1) font-awesome-sass (4.3.2.1)
sass (~> 3.2) sass (~> 3.2)
gherkin (2.12.2)
multi_json (~> 1.3)
globalid (0.3.5) globalid (0.3.5)
activesupport (>= 4.1.0) activesupport (>= 4.1.0)
i18n (0.7.0) i18n (0.7.0)
...@@ -121,6 +130,7 @@ GEM ...@@ -121,6 +130,7 @@ GEM
nokogiri (>= 1.5.9) nokogiri (>= 1.5.9)
mail (2.6.3) mail (2.6.3)
mime-types (>= 1.16, < 3) mime-types (>= 1.16, < 3)
method_source (0.8.2)
mime-types (2.6.1) mime-types (2.6.1)
mini_portile (0.6.2) mini_portile (0.6.2)
minitest (5.7.0) minitest (5.7.0)
...@@ -132,6 +142,11 @@ GEM ...@@ -132,6 +142,11 @@ GEM
nokogiri (1.6.6.2) nokogiri (1.6.6.2)
mini_portile (~> 0.6.0) mini_portile (~> 0.6.0)
orm_adapter (0.5.0) orm_adapter (0.5.0)
pr_geohash (1.0.0)
pry (0.10.1)
coderay (~> 1.1.0)
method_source (~> 0.8.1)
slop (~> 3.4)
rack (1.6.4) rack (1.6.4)
rack-test (0.6.3) rack-test (0.6.3)
rack (>= 1.0) rack (>= 1.0)
...@@ -164,6 +179,8 @@ GEM ...@@ -164,6 +179,8 @@ GEM
request_store (1.1.0) request_store (1.1.0)
responders (2.1.0) responders (2.1.0)
railties (>= 4.2.0, < 5) railties (>= 4.2.0, < 5)
rsolr (1.0.12)
builder (>= 2.1.2)
rspec (3.0.0) rspec (3.0.0)
rspec-core (~> 3.0.0) rspec-core (~> 3.0.0)
rspec-expectations (~> 3.0.0) rspec-expectations (~> 3.0.0)
...@@ -194,9 +211,12 @@ GEM ...@@ -194,9 +211,12 @@ GEM
sdoc (0.4.1) sdoc (0.4.1)
json (~> 1.7, >= 1.7.7) json (~> 1.7, >= 1.7.7)
rdoc (~> 4.0) rdoc (~> 4.0)
shoulda-matchers (2.8.0)
activesupport (>= 3.0.0)
slim (3.0.6) slim (3.0.6)
temple (~> 0.7.3) temple (~> 0.7.3)
tilt (>= 1.3.3, < 2.1) tilt (>= 1.3.3, < 2.1)
slop (3.6.0)
spring (1.3.6) spring (1.3.6)
sprockets (3.2.0) sprockets (3.2.0)
rack (~> 1.0) rack (~> 1.0)
...@@ -204,6 +224,14 @@ GEM ...@@ -204,6 +224,14 @@ GEM
actionpack (>= 3.0) actionpack (>= 3.0)
activesupport (>= 3.0) activesupport (>= 3.0)
sprockets (>= 2.8, < 4.0) sprockets (>= 2.8, < 4.0)
sunspot (2.2.0)
pr_geohash (~> 1.0)
rsolr (~> 1.0.7)
sunspot_rails (2.2.0)
nokogiri
rails (>= 3)
sunspot (= 2.2.0)
sunspot_solr (2.2.0)
temple (0.7.6) temple (0.7.6)
thin (1.6.3) thin (1.6.3)
daemons (~> 1.0, >= 1.0.9) daemons (~> 1.0, >= 1.0.9)
...@@ -214,6 +242,9 @@ GEM ...@@ -214,6 +242,9 @@ GEM
tilt (1.4.1) tilt (1.4.1)
turbolinks (2.5.3) turbolinks (2.5.3)
coffee-rails coffee-rails
turnip (1.3.1)
gherkin (>= 2.5)
rspec (>= 2.14.0, < 4.0)
tzinfo (1.2.2) tzinfo (1.2.2)
thread_safe (~> 0.1) thread_safe (~> 0.1)
uglifier (2.7.1) uglifier (2.7.1)
...@@ -229,6 +260,8 @@ GEM ...@@ -229,6 +260,8 @@ GEM
binding_of_caller (>= 0.7.2) binding_of_caller (>= 0.7.2)
railties (>= 4.0) railties (>= 4.0)
sprockets-rails (>= 2.0, < 4.0) sprockets-rails (>= 2.0, < 4.0)
xpath (2.0.0)
nokogiri (~> 1.3)
PLATFORMS PLATFORMS
ruby ruby
...@@ -238,6 +271,7 @@ DEPENDENCIES ...@@ -238,6 +271,7 @@ DEPENDENCIES
bootstrap3-datetimepicker-rails (~> 4.7.14) bootstrap3-datetimepicker-rails (~> 4.7.14)
breadcrumbs_on_rails breadcrumbs_on_rails
byebug byebug
capybara
coffee-rails (~> 4.1.0) coffee-rails (~> 4.1.0)
database_cleaner database_cleaner
devise (~> 3.4.1) devise (~> 3.4.1)
...@@ -253,15 +287,24 @@ DEPENDENCIES ...@@ -253,15 +287,24 @@ DEPENDENCIES
kaminari (= 0.16.3) kaminari (= 0.16.3)
momentjs-rails (>= 2.8.1) momentjs-rails (>= 2.8.1)
mysql2 mysql2
pry
rails (= 4.2.1) rails (= 4.2.1)
rspec rspec
rspec-core
rspec-expectations
rspec-mocks
rspec-rails (~> 3.0.1) rspec-rails (~> 3.0.1)
rspec-support
sass-rails (~> 5.0) sass-rails (~> 5.0)
sdoc (~> 0.4.0) sdoc (~> 0.4.0)
shoulda-matchers
slim slim
spring spring
sunspot_rails
sunspot_solr
thin thin
turbolinks turbolinks
turnip
uglifier (>= 1.3.0) uglifier (>= 1.3.0)
vacuum vacuum
web-console (~> 2.0) web-console (~> 2.0)
...@@ -11,8 +11,7 @@ class HomeController < ApplicationController ...@@ -11,8 +11,7 @@ class HomeController < ApplicationController
def search def search
add_breadcrumb "Search" add_breadcrumb "Search"
add_breadcrumb "Search", :search_path @products = Product.search_keyword(params)
@products = Product.search(params)
end end
def cart def cart
......
class Category < ActiveRecord::Base class Category < ActiveRecord::Base
has_many :products has_many :products
validates :name, presence: true
end end
...@@ -2,6 +2,7 @@ class LineItem < ActiveRecord::Base ...@@ -2,6 +2,7 @@ class LineItem < ActiveRecord::Base
belongs_to :order belongs_to :order
belongs_to :product belongs_to :product
validates :product_id, :order_id, :quantity, :price, presence: true
validate :check_product_stock validate :check_product_stock
after_save :trigger_recalculate after_save :trigger_recalculate
...@@ -16,8 +17,9 @@ class LineItem < ActiveRecord::Base ...@@ -16,8 +17,9 @@ class LineItem < ActiveRecord::Base
end end
def check_product_stock def check_product_stock
return false if quantity.blank? || product_id.blank?
change_square = quantity - quantity_was change_square = quantity - quantity_was
if change_square > product.stock if change_square > product.try(:stock)
self.errors.add(:quantity, "Product #{product.try(:title)} is not enough in stock") self.errors.add(:quantity, "Product #{product.try(:title)} is not enough in stock")
return false return false
end end
......
class Order < ActiveRecord::Base class Order < ActiveRecord::Base
enum state: [:card, :checkout] enum state: [:cart, :checkout]
has_many :line_items, dependent: :destroy has_many :line_items, dependent: :destroy
belongs_to :user belongs_to :user
......
...@@ -13,8 +13,23 @@ class Product < ActiveRecord::Base ...@@ -13,8 +13,23 @@ class Product < ActiveRecord::Base
scope :recommend, -> { where(recommend: true) } scope :recommend, -> { where(recommend: true) }
def self.search(params) searchable do
Product.order(release_date: :desc).page(params[:page]) text :pid, :title, :author, :publisher, :studio, :currency, :image_uid
boolean :recommend
double :price
integer :category_id, :user_id, :stock
date :release_date
end
def self.search_keyword(params)
where("title LIKE ?", "%#{params[:keyword]}%").order(release_date: :desc).page(params[:page])
# params[:page] ||= 1
# Product.search do
# fulltext params[:keyword]
# paginate :page => params[:page], :per_page => 5
# order_by(:release_date, :desc)
# end
end end
def can_buy?(quantity) def can_buy?(quantity)
......
h4= "Search with keyword '#{params[:keyword]}'"
- if @products.present? - if @products.present?
.products .products
= render partial: "products/item", collection: @products, as: :product = render partial: "products/item", collection: @products, as: :product
......
production:
solr:
hostname: localhost
port: 8983
log_level: WARNING
path: /solr/production
# read_timeout: 2
# open_timeout: 0.5
development:
solr:
hostname: localhost
port: 8982
log_level: INFO
path: /solr/development
test:
solr:
hostname: localhost
port: 8981
log_level: WARNING
path: /solr/test
\ No newline at end of file
class ChangeDefaultStateInOrders < ActiveRecord::Migration
def up
change_column_default :orders, :state, 0
end
def down
change_column_default :orders, :state, 1
end
end
<!--
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<!-- The content of this page will be statically included into the top
of the admin page. Uncomment this as an example to see there the content
will show up.
<hr>
<i>This line will appear before the first table</i>
<tr>
<td colspan="2">
This row will be appended to the end of the first table
</td>
</tr>
<hr>
-->
<?xml version="1.0" encoding="UTF-8" ?>
<!--
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<!-- If this file is found in the config directory, it will only be
loaded once at startup. If it is found in Solr's data
directory, it will be re-loaded every commit.
-->
<elevate>
<query text="foo bar">
<doc id="1" />
<doc id="2" />
<doc id="3" />
</query>
<query text="ipod">
<doc id="MA147LL/A" /> <!-- put the actual ipod at the top -->
<doc id="IW-02" exclude="true" /> <!-- exclude this cable -->
</query>
</elevate>
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# Syntax:
# "source" => "target"
# "source".length() > 0 (source cannot be empty.)
# "target".length() >= 0 (target can be empty.)
# example:
# "À" => "A"
# "\u00C0" => "A"
# "\u00C0" => "\u0041"
# "ß" => "ss"
# "\t" => " "
# "\n" => ""
# À => A
"\u00C0" => "A"
# Á => A
"\u00C1" => "A"
# Â => A
"\u00C2" => "A"
# Ã => A
"\u00C3" => "A"
# Ä => A
"\u00C4" => "A"
# Å => A
"\u00C5" => "A"
# Æ => AE
"\u00C6" => "AE"
# Ç => C
"\u00C7" => "C"
# È => E
"\u00C8" => "E"
# É => E
"\u00C9" => "E"
# Ê => E
"\u00CA" => "E"
# Ë => E
"\u00CB" => "E"
# Ì => I
"\u00CC" => "I"
# Í => I
"\u00CD" => "I"
# Î => I
"\u00CE" => "I"
# Ï => I
"\u00CF" => "I"
# IJ => IJ
"\u0132" => "IJ"
# Ð => D
"\u00D0" => "D"
# Ñ => N
"\u00D1" => "N"
# Ò => O
"\u00D2" => "O"
# Ó => O
"\u00D3" => "O"
# Ô => O
"\u00D4" => "O"
# Õ => O
"\u00D5" => "O"
# Ö => O
"\u00D6" => "O"
# Ø => O
"\u00D8" => "O"
# Œ => OE
"\u0152" => "OE"
# Þ
"\u00DE" => "TH"
# Ù => U
"\u00D9" => "U"
# Ú => U
"\u00DA" => "U"
# Û => U
"\u00DB" => "U"
# Ü => U
"\u00DC" => "U"
# Ý => Y
"\u00DD" => "Y"
# Ÿ => Y
"\u0178" => "Y"
# à => a
"\u00E0" => "a"
# á => a
"\u00E1" => "a"
# â => a
"\u00E2" => "a"
# ã => a
"\u00E3" => "a"
# ä => a
"\u00E4" => "a"
# å => a
"\u00E5" => "a"
# æ => ae
"\u00E6" => "ae"
# ç => c
"\u00E7" => "c"
# è => e
"\u00E8" => "e"
# é => e
"\u00E9" => "e"
# ê => e
"\u00EA" => "e"
# ë => e
"\u00EB" => "e"
# ì => i
"\u00EC" => "i"
# í => i
"\u00ED" => "i"
# î => i
"\u00EE" => "i"
# ï => i
"\u00EF" => "i"
# ij => ij
"\u0133" => "ij"
# ð => d
"\u00F0" => "d"
# ñ => n
"\u00F1" => "n"
# ò => o
"\u00F2" => "o"
# ó => o
"\u00F3" => "o"
# ô => o
"\u00F4" => "o"
# õ => o
"\u00F5" => "o"
# ö => o
"\u00F6" => "o"
# ø => o
"\u00F8" => "o"
# œ => oe
"\u0153" => "oe"
# ß => ss
"\u00DF" => "ss"
# þ => th
"\u00FE" => "th"
# ù => u
"\u00F9" => "u"
# ú => u
"\u00FA" => "u"
# û => u
"\u00FB" => "u"
# ü => u
"\u00FC" => "u"
# ý => y
"\u00FD" => "y"
# ÿ => y
"\u00FF" => "y"
# ff => ff
"\uFB00" => "ff"
# fi => fi
"\uFB01" => "fi"
# fl => fl
"\uFB02" => "fl"
# ffi => ffi
"\uFB03" => "ffi"
# ffl => ffl
"\uFB04" => "ffl"
# ſt => ft
"\uFB05" => "ft"
# st => st
"\uFB06" => "st"
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#-----------------------------------------------------------------------
# Use a protected word file to protect against the stemmer reducing two
# unrelated words to the same base word.
# Some non-words that normally won't be encountered,
# just to test that they won't be stemmed.
dontstems
zwhacky
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
user=
solr_hostname=localhost
solr_port=8983
rsyncd_port=18983
data_dir=
webapp_name=solr
master_host=
master_data_dir=
master_status_dir=
pizza
history
\ No newline at end of file
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#-----------------------------------------------------------------------
# a couple of test stopwords to test that the words are really being
# configured from this file:
stopworda
stopwordb
#Standard english stop words taken from Lucene's StopAnalyzer
a
an
and
are
as
at
be
but
by
for
if
in
into
is
it
no
not
of
on
or
s
such
t
that
the
their
then
there
these
they
this
to
was
will
with
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#-----------------------------------------------------------------------
#some test synonym mappings unlikely to appear in real input text
aaa => aaaa
bbb => bbbb1 bbbb2
ccc => cccc1,cccc2
a\=>a => b\=>b
a\,a => b\,b
fooaaa,baraaa,bazaaa
# Some synonym groups specific to this example
GB,gib,gigabyte,gigabytes
MB,mib,megabyte,megabytes
Television, Televisions, TV, TVs
#notice we use "gib" instead of "GiB" so any WordDelimiterFilter coming
#after us won't split it into two words.
# Synonym mappings can be used for spelling correction too
pixima => pixma
<?xml version="1.0" encoding="UTF-8" ?>
<solr persistent="false">
<cores adminPath="/admin/cores" host="${host:}" hostPort="${jetty.port:}">
<core name="default" instanceDir="." dataDir="default/data"/>
<core name="development" instanceDir="." dataDir="development/data"/>
<core name="test" instanceDir="." dataDir="test/data"/>
</cores>
</solr>
module HelperSteps
# include Rails.application.routes.url_helpers
step "there is a monster" do
puts "11111111"
end
step "I go to register page" do
visit root_path
end
step "I enter :value into :field field" do |value, field|
fill_in(field, with: value)
end
step "I press :btn" do |btn|
click_button(btn)
end
step "I should (not)? see :text" do |negate, text|
within('body') do
negate ? page.should_not(have_content(text)) : page.should(have_content(text))
end
end
end
RSpec.configure do |c|
c.include HelperSteps, :type => :feature
end
Feature: User register
Scenario: register with no name
When I go to register page
Then I enter abc@gmail.com into email field
And I enter abc123456 into password field
When I press 'Sign up'
Then I should see error
FactoryGirl.define do
factory :category do
name {Faker::Name.first_name}
end
end
\ No newline at end of file
FactoryGirl.define do
factory :line_item do
# product_id
# order_id
quantity 1
price 10
end
end
\ No newline at end of file
FactoryGirl.define do
factory :product do
pid SecureRandom.urlsafe_base64(nil, false)
title {Faker::Name.first_name}
author {Faker::Name.first_name}
publisher {Faker::Name.first_name}
studio {Faker::Name.first_name}
price 10
currency 'vnd'
image_uid File.new(File.join(Rails.root, "spec/fixtures/images", "ruby.png"))
release_date DateTime.now
public_date DateTime.now
recommend false
stock 10
# category_id
# user_id
end
end
\ No newline at end of file
FactoryGirl.define do
factory :user do
email {Faker::Internet.email}
password "12345678"
name {Faker::Name.first_name}
end
end
\ No newline at end of file
require 'spec_helper'
feature 'Visitor signs up' do
scenario 'with valid email and password' do
sign_up_with 'valid@example.com', 'password'
expect(page).to have_content('Sign out')
end
scenario 'with invalid email' do
sign_up_with 'invalid_email', 'password'
expect(page).to have_content('Sign in')
end
scenario 'with blank password' do
sign_up_with 'valid@example.com', ''
expect(page).to have_content('Sign in')
end
def sign_up_with(email, password)
visit "/users/sign_up"
fill_in 'Email', with: email
fill_in 'Password', with: password
click_button 'Sign up'
end
end
\ No newline at end of file
require "rails_helper"
describe Category, :type => :model do
it { should have_many(:products) }
it { should validate_presence_of(:name) }
end
\ No newline at end of file
require "rails_helper"
describe LineItem, :type => :model do
it { should belong_to(:order) }
it { should belong_to(:product) }
it { should validate_presence_of(:product_id) }
it { should validate_presence_of(:order_id) }
it { should validate_presence_of(:quantity) }
it { should validate_presence_of(:price) }
end
\ No newline at end of file
require "rails_helper"
describe Order, :type => :model do
it { should have_many(:line_items) }
it { should belong_to(:user) }
describe "Function test" do
before do
@user = create(:user)
@order = @user.orders.create
@category = create(:category)
@product = create(:product, {category_id: @category.id})
@line_item = create(:line_item, {order_id: @order.id, product_id: @product.id, price: @product.price})
end
it "should recalculate exactly total line_items" do
expect(@order.item_total).to eq 0
expect(@order.item_count).to eq 0
expect(@order.reload.item_total).to eq (@line_item.price * @line_item.quantity)
expect(@order.reload.item_count).to eq 1
end
it "should place order when checkout" do
expect(@order.pid).to eq nil
expect(@order.state).to eq "cart"
@order.checkout
expect(@order.reload.pid).not_to eq nil
expect(@order.reload.state).to eq "checkout"
end
end
end
\ No newline at end of file
require "rails_helper"
describe Product, :type => :model do
it { should belong_to(:category) }
it { should belong_to(:user) }
it { should validate_presence_of(:pid) }
it { should validate_presence_of(:title) }
it { should validate_presence_of(:price) }
it { should validate_presence_of(:category_id) }
it { should validate_presence_of(:image) }
it { should validate_presence_of(:stock) }
it { should validate_presence_of(:release_date) }
it { should validate_presence_of(:public_date) }
it { should validate_uniqueness_of(:pid) }
describe "Function test" do
before do
@category = create(:category)
@product = create(:product, {category_id: @category.id})
end
it "should return recommend product when call scope recommend" do
expect(Product.recommend).to eq []
@product.update_columns(recommend: true)
expect(Product.recommend).to eq [@product]
end
it "should return true/false when call can_buy?" do
expect(@product.can_buy?(@product.stock - 1)).to eq true
expect(@product.can_buy?(@product.stock + 1)).to eq false
end
it "should decrease stock when call substract_stock" do
stock = @product.stock
quantity = rand(10)
@product.substract_stock(quantity)
expect(@product.reload.stock).to eq (stock - quantity)
end
end
end
\ No newline at end of file
require "rails_helper"
describe User, :type => :model do
it { should have_many(:products) }
it { should have_many(:orders) }
it { should validate_presence_of(:name) }
end
\ No newline at end of file
# This file is copied to spec/ when you run 'rails generate rspec:install'
ENV["RAILS_ENV"] ||= 'test'
require 'spec_helper'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
require 'rspec/autorun'
require 'capybara/rspec'
require 'capybara/rails'
require 'turnip/capybara'
# 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`.
# Dir[Rails.root.join("spec/support/**/*.rb")].each { |f| require f }
# Checks for pending migrations before tests are run.
# If you are not using ActiveRecord, you can remove this line.
# ActiveRecord::Migration.maintain_test_schema!
RSpec.configure do |config|
config.include FactoryGirl::Syntax::Methods
config.include Devise::TestHelpers, :type => :controller
config.include Warden::Test::Helpers
# config.before(:suite) do
# begin
# DatabaseCleaner.start
# FactoryGirl.lint
# ensure
# DatabaseCleaner.clean
# end
# end
# Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
config.fixture_path = "#{::Rails.root}/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 = true
# 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!
config.include Capybara::DSL
# Added below myself, troubleshooting an 'undefined method 'patch'' message
# config.include Rails.application.routes.url_helpers
end
# 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, make a
# separate helper file that requires this one and then use it only in the specs
# that actually need it.
#
# The `.rspec` file also contains a few flags that are not defaults but that
# users commonly want.
#
require 'factory_girl_rails'
require "action_mailer"
require "email_spec"
require 'database_cleaner'
# require 'rspec/rails'
require 'rspec/autorun'
require 'capybara/rspec'
# require 'capybara/rails'
require 'turnip/capybara'
# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
RSpec.configure do |config|
# config.include(EmailSpec::Helpers)
# config.include(EmailSpec::Matchers)
# 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
# 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
# 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|
# Enable only the newer, non-monkey-patching expect syntax.
# For more details, see:
# - http://myronmars.to/n/dev-blog/2012/06/rspecs-new-expectation-syntax
expectations.syntax = :expect
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|
# Enable only the newer, non-monkey-patching expect syntax.
# For more details, see:
# - http://teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/
mocks.syntax = :expect
# Prevents you from mocking or stubbing a method that does not exist on
# a real object. This is generally recommended.
mocks.verify_partial_doubles = true
end
=end
binding.pry
config.include Rails.application.routes.url_helpers
end
Dir.glob("spec/acceptance/steps/*_steps.rb") { |f| load f, true }
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