add some rspec tests for supplier and article

This commit is contained in:
wvengen 2013-07-14 02:49:40 +02:00
parent 7fa8193010
commit 3c264f6225
8 changed files with 110 additions and 2 deletions

View file

@ -69,4 +69,5 @@ end
group :development, :test do group :development, :test do
gem 'rspec-rails' gem 'rspec-rails'
gem 'factory_girl_rails', '~> 4.0' gem 'factory_girl_rails', '~> 4.0'
gem 'faker'
end end

View file

@ -94,6 +94,8 @@ GEM
factory_girl_rails (4.2.1) factory_girl_rails (4.2.1)
factory_girl (~> 4.2.0) factory_girl (~> 4.2.0)
railties (>= 3.0.0) railties (>= 3.0.0)
faker (1.1.2)
i18n (~> 0.5)
haml (3.1.7) haml (3.1.7)
haml-rails (0.3.5) haml-rails (0.3.5)
actionpack (>= 3.1, < 4.1) actionpack (>= 3.1, < 4.1)
@ -287,6 +289,7 @@ DEPENDENCIES
daemons daemons
exception_notification exception_notification
factory_girl_rails (~> 4.0) factory_girl_rails (~> 4.0)
faker
haml-rails haml-rails
inherited_resources inherited_resources
jquery-rails jquery-rails

20
spec/factories/article.rb Normal file
View file

@ -0,0 +1,20 @@
require 'factory_girl'
FactoryGirl.define do
factory :article do
name { Faker::Lorem.words(rand(2..5)).join(' ') }
unit { Faker::Unit.unit }
price { rand(2600) / 100 }
tax { [6, 21].sample }
deposit { rand(10) < 8 ? 0 : [0.0, 0.80, 1.20, 12.00].sample }
unit_quantity { rand(5) < 3 ? 1 : rand(1..20) }
#supplier_id
article_category { FactoryGirl.create :article_category }
end
factory :article_category do
name { Faker::Lorem.characters(rand(2..20)) }
end
end

View file

@ -0,0 +1,19 @@
require 'factory_girl'
FactoryGirl.define do
factory :supplier do
name { Faker::Company.name }
phone { Faker::PhoneNumber.phone_number }
address { Faker::Address.street_address }
ignore do
article_count 0
end
after :create do |supplier, evaluator|
FactoryGirl.create_list :article, evaluator.article_count, supplier: supplier
end
end
end

View file

@ -4,8 +4,8 @@ FactoryGirl.define do
factory :user do factory :user do
sequence(:nick) { |n| "user#{n}"} sequence(:nick) { |n| "user#{n}"}
first_name 'John' first_name { Faker::Name.first_name }
email { "#{nick}@foodcoop.test" } email { Faker::Internet.email }
password { new_random_password } password { new_random_password }
factory :admin do factory :admin do

View file

@ -0,0 +1,44 @@
require 'spec_helper'
describe Article do
let(:supplier) { FactoryGirl.create :supplier }
let(:article) { FactoryGirl.create :article, supplier: supplier }
it 'has a unique name' do
article2 = FactoryGirl.build :article, supplier: supplier, name: article.name
article2.should_not be_valid
end
it 'computes the gross price correctly' do
article.deposit = 0
article.tax = 12
article.gross_price.should == (article.price * 1.12).round(2)
article.deposit = 1.20
article.gross_price.should == ((article.price + 1.20) * 1.12).round(2)
end
it 'gross price >= net price' do
article.gross_price.should >= article.price
end
it 'fc-price > gross price' do
article.fc_price.should > article.gross_price
end
it 'knows when it is deleted' do
supplier.deleted?.should be_false
supplier.mark_as_deleted
supplier.deleted?.should be_true
end
it 'keeps a price history' do
article.article_prices.count.should == 1
oldprice = article.price
article.price += 1
article.save!
article.article_prices.count.should == 2
article.article_prices[0].price.should == article.price
article.article_prices[-1].price.should == oldprice
end
end

View file

@ -0,0 +1,11 @@
require 'spec_helper'
describe Supplier do
let(:supplier) { FactoryGirl.create :supplier }
it 'has a unique name' do
supplier2 = FactoryGirl.build :supplier, name: supplier.name
supplier2.should_not be_valid
end
end

View file

@ -36,3 +36,13 @@ RSpec.configure do |config|
# --seed 1234 # --seed 1234
config.order = "random" config.order = "random"
end end
module Faker
class Unit
class << self
def unit
['kg', '1L', '100ml', 'piece', 'bunch', '500g'].sample
end
end
end
end