Remove minitest and add rspec with factories

This commit is contained in:
benji 2015-09-02 20:33:35 +02:00
parent 3f43dd8cd5
commit ae28317b07
37 changed files with 274 additions and 291 deletions

3
.rspec Normal file
View file

@ -0,0 +1,3 @@
--color
--require spec_helper
--require rails_helper

View file

@ -37,7 +37,10 @@ end
group :test do group :test do
gem 'capybara' gem 'capybara'
gem 'launchy' gem 'launchy'
gem "codeclimate-test-reporter", require: nil gem 'codeclimate-test-reporter', require: nil
gem 'rspec-rails'
gem 'factory_girl_rails'
gem 'faker', '1.4.2'
end end
group :development do group :development do
@ -60,9 +63,6 @@ end
# Airbrake # Airbrake
gem 'airbrake' gem 'airbrake'
# Generate seed data
gem 'faker', '1.4.2'
# Debug stuff # Debug stuff
gem 'byebug' gem 'byebug'

View file

@ -112,9 +112,15 @@ GEM
responders responders
thread_safe (~> 0.1) thread_safe (~> 0.1)
warden (~> 1.2.3) warden (~> 1.2.3)
diff-lcs (1.2.5)
docile (1.1.5) docile (1.1.5)
erubis (2.7.0) erubis (2.7.0)
execjs (2.3.0) execjs (2.3.0)
factory_girl (4.5.0)
activesupport (>= 3.0.0)
factory_girl_rails (4.5.0)
factory_girl (~> 4.5.0)
railties (>= 3.0.0)
faker (1.4.2) faker (1.4.2)
i18n (~> 0.5) i18n (~> 0.5)
faraday (0.9.1) faraday (0.9.1)
@ -222,6 +228,23 @@ GEM
ffi (~> 1.9) ffi (~> 1.9)
mime-types (>= 1.16, < 3.0) mime-types (>= 1.16, < 3.0)
netrc (~> 0.7) netrc (~> 0.7)
rspec-core (3.3.2)
rspec-support (~> 3.3.0)
rspec-expectations (3.3.1)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.3.0)
rspec-mocks (3.3.2)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.3.0)
rspec-rails (3.3.3)
actionpack (>= 3.0, < 4.3)
activesupport (>= 3.0, < 4.3)
railties (>= 3.0, < 4.3)
rspec-core (~> 3.3.0)
rspec-expectations (~> 3.3.0)
rspec-mocks (~> 3.3.0)
rspec-support (~> 3.3.0)
rspec-support (3.3.0)
sass (3.2.19) sass (3.2.19)
sass-rails (4.0.5) sass-rails (4.0.5)
railties (>= 4.0.0, < 5.0) railties (>= 4.0.0, < 5.0)
@ -298,6 +321,7 @@ DEPENDENCIES
coffee-rails (~> 4.0.0) coffee-rails (~> 4.0.0)
coveralls coveralls
devise devise
factory_girl_rails
faker (= 1.4.2) faker (= 1.4.2)
identicon identicon
jbuilder (~> 2.0) jbuilder (~> 2.0)
@ -309,6 +333,7 @@ DEPENDENCIES
paperclip paperclip
rails (= 4.2) rails (= 4.2)
responders (~> 2.0) responders (~> 2.0)
rspec-rails
sass-rails (~> 4.0.3) sass-rails (~> 4.0.3)
sdoc (~> 0.4.0) sdoc (~> 0.4.0)
spring spring
@ -320,4 +345,4 @@ DEPENDENCIES
will_paginate (= 3.0.7) will_paginate (= 3.0.7)
BUNDLED WITH BUNDLED WITH
1.10.5 1.10.6

View file

@ -13,7 +13,8 @@
# avatar_updated_at :datetime # avatar_updated_at :datetime
# category :integer default("0") # category :integer default("0")
# stock :integer default("0"), not null # stock :integer default("0"), not null
# calories :integer default("0") // expressed in kcal # calories :integer
# deleted :boolean default("f")
# #
class Product < ActiveRecord::Base class Product < ActiveRecord::Base

View file

@ -22,7 +22,8 @@
# koelkast :boolean default("f") # koelkast :boolean default("f")
# provider :string # provider :string
# uid :string # uid :string
# encrypted_password :string # encrypted_password :string default(""), not null
# private :boolean default("f")
# #
require 'identicon' require 'identicon'

View file

@ -8,4 +8,9 @@
# count :integer default("0") # count :integer default("0")
# #
# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html FactoryGirl.define do
factory :order_item do
order
product
end
end

22
spec/factories/orders.rb Normal file
View file

@ -0,0 +1,22 @@
# == Schema Information
#
# Table name: orders
#
# id :integer not null, primary key
# user_id :integer
# price_cents :integer
# created_at :datetime not null
# updated_at :datetime not null
# cancelled :boolean default("f")
#
require 'faker'
FactoryGirl.define do
factory :order do
user
before(:create) do |order|
order.order_items << build(:order_item, order: order)
end
end
end

View file

@ -13,30 +13,19 @@
# avatar_updated_at :datetime # avatar_updated_at :datetime
# category :integer default("0") # category :integer default("0")
# stock :integer default("0"), not null # stock :integer default("0"), not null
# calories :integer
# deleted :boolean default("f")
# #
# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html require 'faker'
require 'identicon'
fanta: FactoryGirl.define do
id: 1 factory :product do
name: 'Fanta' name { Faker::Name.name }
price_cents: 60 price_cents { rand 120 }
stock: 100 stock { rand 30 }
calories { rand 20 }
cola: avatar { Identicon.data_url_for name }
id: 2 end
name: 'Cola' end
price_cents: 60
stock: 0
mate:
id: 3
name: 'Club Mate'
price_cents: 120
stock: 100
bueno:
id: 4
name: 'Kinder Bueno'
stock: 50
price_cents: 120

View file

@ -22,22 +22,16 @@
# koelkast :boolean default("f") # koelkast :boolean default("f")
# provider :string # provider :string
# uid :string # uid :string
# encrypted_password :string # encrypted_password :string default(""), not null
# private :boolean default("f")
# #
# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html require 'faker'
require 'identicon'
benji: FactoryGirl.define do
uid: benji factory :user do
dagschotel_id: 1 uid { Faker::Name.name }
avatar { Identicon.data_url_for uid }
iasoon: end
uid: iasoon end
admin:
uid: admin
admin: 1
koelkast:
uid: koelkast
koelkast: 1

View file

@ -0,0 +1,9 @@
describe OrderItem do
before :each do
@order_item = create :order_item
end
it 'has a valid factory' do
expect(@order_item).to be_valid
end
end

View file

@ -10,4 +10,12 @@
# cancelled :boolean default("f") # cancelled :boolean default("f")
# #
# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html describe Order do
before :each do
@order = create :order
end
it 'has a valid factory' do
expect(@order).to be_valid
end
end

View file

@ -13,20 +13,16 @@
# avatar_updated_at :datetime # avatar_updated_at :datetime
# category :integer default("0") # category :integer default("0")
# stock :integer default("0"), not null # stock :integer default("0"), not null
# calories :integer
# deleted :boolean default("f")
# #
require 'test_helper' describe Product do
before :each do
@product = create :product
end
class ProductTest < ActiveSupport::TestCase it 'has a valid factory' do
test "price behaves correctly" do expect(@product).to be_valid
p = products(:fanta)
assert_equal p.price_cents, 60
assert_equal p.price, 0.6
p.price = 1.3
assert_equal p.price, 1.3
assert_equal p.price_cents, 130
end end
end end

View file

@ -22,17 +22,16 @@
# koelkast :boolean default("f") # koelkast :boolean default("f")
# provider :string # provider :string
# uid :string # uid :string
# encrypted_password :string # encrypted_password :string default(""), not null
# private :boolean default("f")
# #
require 'test_helper' describe User do
before :each do
class UserTest < ActiveSupport::TestCase @user = create :user
def setup
@user = users(:benji)
end end
test "to_param" do it 'has a valid factory' do
assert_equal @user.to_param, "#{@user.id}-benji" expect(@user).to be_valid
end end
end end

52
spec/rails_helper.rb Normal file
View file

@ -0,0 +1,52 @@
# This file is copied to spec/ when you run 'rails generate rspec:install'
ENV['RAILS_ENV'] ||= 'test'
require File.expand_path('../../config/environment', __FILE__)
# Prevent database truncation if the environment is production
abort("The Rails environment is running in production mode!") if Rails.env.production?
require 'spec_helper'
require 'rspec/rails'
# Add additional requires below this line. Rails is not loaded until this point!
# 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`.
#
# The following line is provided for convenience purposes. It has the downside
# of increasing the boot-up time by auto-requiring all files in the support
# directory. Alternatively, in the individual `*_spec.rb` files, manually
# require only the support files necessary.
#
# 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|
# 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!
end

101
spec/spec_helper.rb Normal file
View file

@ -0,0 +1,101 @@
require 'coveralls'
Coveralls.wear!
# 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
require 'factory_girl'
require 'devise'
RSpec.configure do |config|
config.include FactoryGirl::Syntax::Methods
config.include Devise::TestHelpers, type: :controller
# 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

View file

View file

@ -1,7 +0,0 @@
require 'test_helper'
class AdminsControllerTest < ActionController::TestCase
# test "the truth" do
# assert true
# end
end

View file

@ -1,7 +0,0 @@
require 'test_helper'
class CallbacksControllerTest < ActionController::TestCase
# test "the truth" do
# assert true
# end
end

View file

@ -1,7 +0,0 @@
require 'test_helper'
class OrdersControllerTest < ActionController::TestCase
# test "the truth" do
# assert true
# end
end

View file

@ -1,4 +0,0 @@
require 'test_helper'
class ProductsControllerTest < ActionController::TestCase
end

View file

@ -1,7 +0,0 @@
require 'test_helper'
class SessionsControllerTest < ActionController::TestCase
# test "the truth" do
# assert true
# end
end

View file

@ -1,7 +0,0 @@
require 'test_helper'
class StockControllerTest < ActionController::TestCase
# test "the truth" do
# assert true
# end
end

View file

@ -1,4 +0,0 @@
require 'test_helper'
class UsersControllerTest < ActionController::TestCase
end

View file

@ -1,7 +0,0 @@
require 'test_helper'
class WelcomeControllerTest < ActionController::TestCase
# test "the truth" do
# assert true
# end
end

0
test/fixtures/.keep vendored
View file

View file

View file

View file

@ -1,19 +0,0 @@
require 'test_helper'
class GenerateOrderItemsTest < ActiveSupport::TestCase
test "g_order_items works" do
order = Order.new
products = Product.all.where("stock > 0")
size = products.size
order.order_items.build(product: products(:fanta), count: 150)
order.order_items.build(product: products(:mate), count: 50)
order.g_order_items(products)
assert_equal order.order_items.size, size
assert_equal order.order_items.select { |oi| oi.product == products(:fanta) }.first.count, products(:fanta).stock
assert_equal order.order_items.select { |oi| oi.product == products(:cola) }.size, 0
assert_equal order.order_items.select { |oi| oi.product == products(:mate) }.first.count, 50
assert_equal order.order_items.select { |oi| oi.product == products(:bueno) }.first.count, 0
end
end

View file

@ -1,39 +0,0 @@
require 'test_helper'
class OrderIntegrationTest < ActionDispatch::IntegrationTest
def setup
Product.all.each do |product|
product.avatar = File.new('public/seeds/products/1.jpg', 'r')
product.save
end
sign_in users(:koelkast)
end
# test 'orders are saved for the right user' do
# visit new_user_order_path(users(:benji))
# assert page.has_content? 'Order for benji'
# assert_difference "User.find(users(:benji).id).debt_cents", 240 do
# fill_in 'order_order_items_attributes_2_count', with: 2
# click_button "Order!"
# end
# end
# test 'quickpay' do
# assert_difference "User.find(users(:benji).id).debt_cents", User.find(users(:benji).id).dagschotel.price_cents do
# visit user_quickpay_path(users(:benji))
# assert page.has_content? 'Success!'
# end
# end
# test 'cancelling quickpay' do
# visit user_quickpay_path(users(:benji))
# assert_difference "User.find(users(:benji).id).debt_cents", -User.find(users(:benji).id).dagschotel.price_cents do
# click_link 'Undo'
# assert page.has_content? 'Success!'
# end
# end
end

View file

@ -1,20 +0,0 @@
require 'test_helper'
class ProductAttributesTest < ActiveSupport::TestCase
test "product_attributes are read correctly" do
params = {
order_items_attributes: [
{
count: "5",
product_attributes: {
id: products(:fanta).id
}
}
]
}
o = Order.create(params)
assert_equal o.order_items.first.product, products(:fanta)
end
end

View file

@ -1,16 +0,0 @@
require 'test_helper'
class StockTest < ActiveSupport::TestCase
test "creating and deleting orders updates stock of products" do
order = users(:benji).orders.build
order.order_items.build(product: products(:fanta), count: 2)
assert_difference "products(:fanta).stock", -2 do
order.save(validate: false)
end
assert_difference "products(:fanta).stock", +2 do
order.cancel
end
end
end

View file

@ -1,12 +0,0 @@
require 'test_helper'
class StockValidatorTest < ActiveSupport::TestCase
test "form gives error when product out of stock" do
order = users(:benji).orders.build
order.order_items.build(product: products(:cola), count: 10)
order.save
assert_includes order.errors[:base], "There is not enough stock for your order of the following products: Cola"
end
end

View file

View file

View file

@ -1,29 +0,0 @@
# == Schema Information
#
# Table name: orders
#
# id :integer not null, primary key
# user_id :integer
# price_cents :integer
# created_at :datetime not null
# updated_at :datetime not null
# cancelled :boolean default("f")
#
require 'test_helper'
class OrderTest < ActiveSupport::TestCase
def setup
@order = Order.new
@order.order_items.build(product: products(:fanta), count: 1)
@order.order_items.build(product: products(:bueno), count: 2)
end
test "order total price is correct" do
assert_equal @order.price, 3.00
end
test "to_sentence is correct" do
assert_equal @order.to_sentence, "1 Fanta and 2 Kinder Buenos"
end
end

View file

@ -1,7 +0,0 @@
require 'test_helper'
class StockEntryTest < ActiveSupport::TestCase
# test "the truth" do
# assert true
# end
end

View file

@ -1,30 +0,0 @@
require "codeclimate-test-reporter"
CodeClimate::TestReporter.start
require 'coveralls'
Coveralls.wear!
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