From 7dafcf714a6a5496193857afe1effe214a38ba40 Mon Sep 17 00:00:00 2001 From: wvengen Date: Thu, 25 Jul 2013 11:16:39 +0200 Subject: [PATCH] spec move functionality into factory --- spec/factories/order.rb | 15 ++++++++++++++- spec/models/group_order_article_spec.rb | 8 ++++---- spec/models/group_order_spec.rb | 3 +-- spec/models/order_spec.rb | 8 +++----- 4 files changed, 22 insertions(+), 12 deletions(-) diff --git a/spec/factories/order.rb b/spec/factories/order.rb index ac2182b5..27af29d6 100644 --- a/spec/factories/order.rb +++ b/spec/factories/order.rb @@ -2,12 +2,25 @@ require 'factory_girl' FactoryGirl.define do - # requires articles from single supplier, or supplier (with all its articles) factory :order do starts { Time.now } + supplier { FactoryGirl.create :supplier, article_count: (article_count.nil? ? true : article_count) } + article_ids { supplier.articles.map(&:id) unless supplier.nil? } + ignore do + article_count true + end + + # for an order from stock; need to add articles factory :stock_order do supplier_id 0 + # article_ids needs to be supplied + end + + # In the order's after_save callback order articles are created, so + # until the order is saved, these articles do not yet exist. + after :create do |order| + order.reload end end diff --git a/spec/models/group_order_article_spec.rb b/spec/models/group_order_article_spec.rb index c20542a7..759e2afc 100644 --- a/spec/models/group_order_article_spec.rb +++ b/spec/models/group_order_article_spec.rb @@ -2,8 +2,7 @@ require 'spec_helper' describe GroupOrderArticle do let(:user) { FactoryGirl.create :user, groups: [FactoryGirl.create(:ordergroup)] } - let(:supplier) { FactoryGirl.create :supplier, article_count: true } - let(:order) { FactoryGirl.create(:order, supplier: supplier, article_ids: supplier.articles.map(&:id)).reload } + let(:order) { FactoryGirl.create(:order).reload } let(:go) { FactoryGirl.create :group_order, order: order, ordergroup: user.ordergroup } let(:goa) { FactoryGirl.create :group_order_article, group_order: go, order_article: order.order_articles.first } @@ -14,8 +13,9 @@ describe GroupOrderArticle do it 'has zero total price by default' do expect(goa.total_price).to eq(0) end describe do - let(:article) { FactoryGirl.create :article, supplier: supplier, unit_quantity: 1 } - let(:goa) { article; FactoryGirl.create :group_order_article, group_order: go, order_article: order.order_articles.find_by_article_id(article.id) } + let(:article) { FactoryGirl.create :article, supplier: order.supplier, unit_quantity: 1 } + let(:oa) { order.order_articles.create(:article => article) } + let(:goa) { FactoryGirl.create :group_order_article, group_order: go, order_article: oa } it 'can be ordered by piece' do goa.update_quantities(1, 0) diff --git a/spec/models/group_order_spec.rb b/spec/models/group_order_spec.rb index ebb0eae4..04f69c33 100644 --- a/spec/models/group_order_spec.rb +++ b/spec/models/group_order_spec.rb @@ -2,8 +2,7 @@ require 'spec_helper' describe GroupOrder do let(:user) { FactoryGirl.create :user, groups: [FactoryGirl.create(:ordergroup)] } - let(:supplier) { FactoryGirl.create :supplier, article_count: true } - let(:order) { FactoryGirl.create(:order, supplier: supplier, article_ids: supplier.articles.map(&:id)).reload } + let(:order) { FactoryGirl.create :order } # the following two tests are currently disabled - https://github.com/foodcoops/foodsoft/issues/158 diff --git a/spec/models/order_spec.rb b/spec/models/order_spec.rb index 486996a5..4a8b7ebc 100644 --- a/spec/models/order_spec.rb +++ b/spec/models/order_spec.rb @@ -3,7 +3,7 @@ require 'spec_helper' describe Order do it 'needs a supplier' do - expect(FactoryGirl.build(:order)).to be_invalid + expect(FactoryGirl.build(:order, supplier: nil)).to be_invalid end it 'needs order articles' do @@ -12,13 +12,11 @@ describe Order do end it 'can be created' do - supplier = FactoryGirl.create :supplier, article_count: 1 - expect(FactoryGirl.build(:order, supplier: supplier, article_ids: supplier.articles.map(&:id))).to be_valid + expect(FactoryGirl.build(:order, article_count: 1)).to be_valid end describe 'with articles' do - let(:supplier) { FactoryGirl.create :supplier, article_count: true } - let(:order) { FactoryGirl.create(:order, supplier: supplier, article_ids: supplier.articles.map(&:id)).reload } + let(:order) { FactoryGirl.create :order } it 'is open by default' do expect(order).to be_open end it 'is not finished by default' do expect(order).to_not be_finished end