Commit 8eb2b408 by vulehuan

rspec: card - optomize code: use shared example

parent 8a8d8cc1
require 'spec_helper' require 'spec_helper'
require 'set' require 'set'
shared_examples "calculate_price_list" do
it do
total = 0
Product.all.each do |product|
should have_xpath("//div[@id='cart-quantity-#{product.id}'][text()='#{amount}']")
should have_selector("#cart-unit-price-#{product.id}", text: ActionController::Base.helpers.number_with_delimiter(product.price))
should have_selector("#cart-total-price-#{product.id}", text: ActionController::Base.helpers.number_with_delimiter(product.price * amount))
total += product.price
end
should have_selector('#card-must-paid', text: ActionController::Base.helpers.number_with_delimiter(total))
end
end
def fill_in_order_form() def fill_in_order_form()
fill_in "Full name", with: "Joseph Vu" fill_in "Full name", with: "Joseph Vu"
...@@ -278,20 +291,12 @@ describe "CardPages" do ...@@ -278,20 +291,12 @@ describe "CardPages" do
end end
describe "calculate price" do describe "calculate price" do
let(:amount) { 1 }
before do before do
visit url_for(controller: 'cards', action: 'checkout') visit url_for(controller: 'cards', action: 'checkout')
end end
it do
total = 0
Product.all.each do |product|
should have_xpath("//div[@id='cart-quantity-#{product.id}'][text()='#{amount}']")
should have_selector("#cart-unit-price-#{product.id}", text: ActionController::Base.helpers.number_with_delimiter(product.price))
should have_selector("#cart-total-price-#{product.id}", text: ActionController::Base.helpers.number_with_delimiter(product.price * amount))
total += product.price
end
should have_selector('#card-must-paid', text: ActionController::Base.helpers.number_with_delimiter(total)) it_behaves_like "calculate_price_list" do
let(:amount) { 1 }
end end
end end
end end
...@@ -355,17 +360,8 @@ describe "CardPages" do ...@@ -355,17 +360,8 @@ describe "CardPages" do
end end
describe "calculate price" do describe "calculate price" do
let(:amount) { 1 } it_behaves_like "calculate_price_list" do
it do let(:amount) { 1 }
total = 0
Product.all.each do |product|
should have_xpath("//div[@id='cart-quantity-#{product.id}'][text()='#{amount}']")
should have_selector("#cart-unit-price-#{product.id}", text: ActionController::Base.helpers.number_with_delimiter(product.price))
should have_selector("#cart-total-price-#{product.id}", text: ActionController::Base.helpers.number_with_delimiter(product.price * amount))
total += product.price
end
should have_selector('#card-must-paid', text: ActionController::Base.helpers.number_with_delimiter(total))
end end
end end
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