Push several request specs down to controller level, replace with scenario. Set order's distributor when adding product to cart

This commit is contained in:
Rohan Mitchell
2012-06-24 13:21:36 +10:00
parent fc5795173e
commit 3219006423
3 changed files with 44 additions and 7 deletions

View File

@@ -4,7 +4,14 @@ Spree::OrdersController.class_eval do
def populate_order_distributor
@distributor = Spree::Distributor.find params[:distributor_id]
redirect_to cart_path unless populate_valid? @distributor
if populate_valid? @distributor
order = current_order(true)
order.distributor = @distributor
order.save!
else
redirect_to cart_path
end
end
private

View File

@@ -1,6 +1,9 @@
require 'spec_helper'
require 'spree/core/current_order'
describe Spree::OrdersController do
include Spree::Core::CurrentOrder
context "adding the first product to the cart" do
it "does not add the product if the user does not specify a distributor" do
create(:distributor)
@@ -20,5 +23,18 @@ describe Spree::OrdersController do
spree_put :populate, :variants => {p.id => 1}, :distributor_id => distributor_no_product.id
end.to change(Spree::LineItem, :count).by(0)
end
it "sets the order's distributor" do
# Given a product in a distributor
d = create(:distributor)
p = create(:product, :distributors => [d])
# When we add the product to our cart
spree_put :populate, :variants => {p.id => 1}, :distributor_id => d.id
# Then our order should have its distributor set to the chosen distributor
current_order(false).distributor.should == d
end
end
end

View File

@@ -8,13 +8,27 @@ feature %q{
include AuthenticationWorkflow
include WebHelper
context "adding the first product to the cart" do
it "requires the user choose a distributor" do
# Tested in orders_controller_spec.rb
end
scenario "adding the first product to the cart" do
# Given a product and some distributors
d1 = create(:distributor)
d2 = create(:distributor)
p = create(:product, :distributors => [d1])
it "sets the order's distributor"
it "sets the user's distributor when adding a product at a remote distributor"
# When I choose a distributor
visit spree.root_path
click_link d2.name
# When I add an item to my cart from a different distributor
visit spree.product_path p
select d1.name, :from => 'distributor_id'
click_button 'Add To Cart'
# Then the item should be in my cart
order = Spree::Order.last
order.line_items.first.product.should == p
# And my order should have its distributor set to the chosen distributor
order.distributor.should == d1
end
it "does not allow the user to change distributor after a product has been added to the cart"