Add test for placing order

This commit is contained in:
benji 2015-03-09 13:17:41 +01:00
parent ec597ff1aa
commit 35fc8ecc77
7 changed files with 81 additions and 23 deletions

View file

@ -41,6 +41,11 @@ group :production do
gem 'mysql2' # Database
end
group :test do
gem 'capybara'
gem 'launchy'
end
#bootstrap
gem 'bootstrap-sass', '3.2.0.0'

View file

@ -36,6 +36,7 @@ GEM
minitest (~> 5.1)
thread_safe (~> 0.3, >= 0.3.4)
tzinfo (~> 1.1)
addressable (2.3.7)
annotate (2.6.5)
activerecord (>= 2.3.0)
rake (>= 0.8.7)
@ -67,6 +68,12 @@ GEM
capistrano (~> 3.0)
sshkit (~> 1.2)
capistrano-stats (1.1.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)
climate_control (0.0.3)
activesupport (>= 3.0)
cocaine (0.5.5)
@ -114,6 +121,8 @@ GEM
railties (>= 4.2.0)
thor (>= 0.14, < 2.0)
json (1.8.2)
launchy (2.4.3)
addressable (~> 2.3)
loofah (2.0.1)
nokogiri (>= 1.5.9)
mail (2.6.3)
@ -228,6 +237,8 @@ GEM
warden (1.2.3)
rack (>= 1.0)
will_paginate (3.0.7)
xpath (2.0.0)
nokogiri (~> 1.3)
PLATFORMS
ruby
@ -242,6 +253,7 @@ DEPENDENCIES
capistrano (~> 3.1)
capistrano-rails (~> 1.1)
capistrano-rvm
capybara
codeclimate-test-reporter
coffee-rails (~> 4.0.0)
coveralls
@ -249,6 +261,7 @@ DEPENDENCIES
faker (= 1.4.2)
jbuilder (~> 2.0)
jquery-rails
launchy
mysql2
paper_trail (~> 4.0.0.beta)
paperclip

View file

@ -83,6 +83,20 @@ class FormattedFormBuilder < ActionView::Helpers::FormBuilder
end
end
def error_header
content_tag(:div, class: "panel-heading") do
"#{pluralize(object.errors.count, "error")} prohibited this #{object.class.name.downcase} from being saved:"
end
end
def error_messages
content_tag :ul do
object.errors.full_messages.map do |msg|
content_tag :li, msg
end.join.html_safe
end
end
private
def label_class
"control-label"
@ -162,18 +176,4 @@ class FormattedFormBuilder < ActionView::Helpers::FormBuilder
end
end
end
def error_header
content_tag(:div, class: "panel-heading") do
"#{pluralize(object.errors.count, "error")} prohibited this #{object.class.name.downcase} from being saved:"
end
end
def error_messages
content_tag :ul do
object.errors.full_messages.map do |msg|
content_tag :li, msg
end.join.html_safe
end
end
end

View file

@ -7,7 +7,7 @@
<%= csrf_meta_tags %>
</head>
<% if current_user && current_user.koelkast? %>
<% if current_user && current_user.koelkast? && !Rails.env.development? %>
<body oncontextmenu="return false">
<% else %>
<body>

View file

@ -18,21 +18,25 @@
# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html
fanta:
name: 'Fanta'
id: 1
name: 'Fanta'
price_cents: 60
stock: 100
stock: 100
cola:
name: 'Cola'
id: 2
name: 'Cola'
price_cents: 60
stock: 0
stock: 0
mate:
name: 'Club Mate'
id: 3
name: 'Club Mate'
price_cents: 120
stock: 100
stock: 100
bueno:
name: 'Kinder Bueno'
stock: 50
id: 4
name: 'Kinder Bueno'
stock: 50
price_cents: 120

View file

@ -0,0 +1,22 @@
require 'test_helper'
class OrderIntegrationTest < ActionDispatch::IntegrationTest
test 'orders are saved for the right user' do
allproducts = [products(:fanta), products(:cola), products(:mate), products(:bueno)]
allproducts.each do |product|
product.avatar = File.new('public/seeds/products/fanta.jpg', 'r')
product.save
end
sign_in users(:koelkast)
visit new_user_order_path(users(:benji))
assert page.has_content? 'Order for benji'
assert_difference "User.find(users(:benji).id).balance_cents", -240 do
fill_in 'order_order_items_attributes_2_count', with: 2
click_button "Order!"
end
end
end

View file

@ -8,9 +8,23 @@ ENV['RAILS_ENV'] ||= 'test'
require File.expand_path('../../config/environment', __FILE__)
require 'rails/test_help'
require 'capybara/rails'
class ActiveSupport::TestCase
# Setup all fixtures in test/fixtures/*.yml for all tests in alphabetical order.
fixtures :all
# Add more helper methods to be used by all tests here...
end
class ActionDispatch::IntegrationTest
include Warden::Test::Helpers
Warden.test_mode!
def sign_in(user)
login_as user, scope: :user
end
# Make the Capybara DSL available in all integration tests
include Capybara::DSL
end