Skip to content
Permalink
Browse files

Remove mocks from CartLinesController specs

  • Loading branch information
philou committed Aug 28, 2013
1 parent 4e7edfa commit 2c9fce17f9b59d0b3828f309015c07b17cceddf4
@@ -9,49 +9,45 @@
ignore_user_authentication ignore_user_authentication


before(:each) do before(:each) do
@cart = stub_model(Cart) capture_result_from(Cart, :create, into: :cart)
@cart.stub(:save!)
@cart.stub(:add_dish)
Cart.stub(:create).and_return(@cart)

@store = stub_model(Store, :url => "http://www.mega-store.com")
@stores = [@store]
Store.stub(:all).and_return(@stores)
Store.stub(:find_by_id).with(@store.id).and_return(@store)
end end


context "session handling" do context "session handling" do


before :each do before :each do
Item.stub(:find).and_return(stub_model(Item)) @item = FactoryGirl.create(:item)
Dish.stub(:find).and_return(stub_model(Dish)) @dish = FactoryGirl.create(:dish)
end end


def self.it_should_find_or_create_a_session_when_answering (action, &http_request) def self.it_should_find_or_create_a_session_when_answering (action, &request)
context "when answering #{action}" do context "when answering #{action}" do


it "should create a new cart in session if no one exists" do it "should create a new cart in session if no one exists" do
instance_eval(&http_request) http(&request)


expect(session[:cart_id]).to eq @cart.id expect(session[:cart_id]).to eq @cart.id
end end


it "should reuse existing cart from session if one exists" do it "should reuse existing cart from session if one exists" do
session[:cart_id] = @cart.id session[:cart_id] = FactoryGirl.create(:cart).id
Cart.stub(:find_by_id).with(@cart.id).and_return(@cart)


expect(Cart).not_to receive(:create) expect { http(&request) }.not_to change { carts_ids }
end


instance_eval(&http_request) def http(&request)
instance_eval(&request)
end end


def carts_ids
Cart.all.map &:id
end
end end
end end


it_should_find_or_create_a_session_when_answering(:index) { get :index } it_should_find_or_create_a_session_when_answering(:index) { get :index }
it_should_find_or_create_a_session_when_answering(:destroy_all) { delete :destroy_all} it_should_find_or_create_a_session_when_answering(:destroy_all) { delete :destroy_all}
it_should_find_or_create_a_session_when_answering(:create) { post :create} it_should_find_or_create_a_session_when_answering(:create) { post :create, id: @item.id}
it_should_find_or_create_a_session_when_answering(:add_dish) { post :add_dish, :id => @cart.id } it_should_find_or_create_a_session_when_answering(:add_dish) { post :add_dish, id: @dish.id }


end end


@@ -69,25 +65,27 @@ def self.it_should_find_or_create_a_session_when_answering (action, &http_reques
end end


it "should assign stores with index" do it "should assign stores with index" do
store = FactoryGirl.create(:store)

get :index get :index


expect(assigns(:stores)).to eq @stores expect(assigns(:stores)).to eq [store]
end end


# The 3 following contexts look a lot like each other, there were factored out before, but it was unreadable ... # The 3 following contexts look a lot like each other, there were factored out before, but it was unreadable ...


context "when destroying the cart" do context "when destroying the cart" do


it "should empty the session cart" do before :each do
expect(@cart).to receive(:empty) @cart = FactoryGirl.create(:cart, :with_items)

session[:cart_id] = @cart.id
delete :destroy_all
end end


it "should save the modified cart" do it "should empty the session cart" do
expect(@cart).to receive(:save!)

delete :destroy_all delete :destroy_all

@cart.reload
expect(@cart).to be_empty
end end


it "should redirect to show" do it "should redirect to show" do
@@ -100,23 +98,17 @@ def self.it_should_find_or_create_a_session_when_answering (action, &http_reques
context "when adding item to cart" do context "when adding item to cart" do


before(:each) do before(:each) do
@item = stub_model(Item) @item = FactoryGirl.create(:item)
Item.stub(:find).and_return(@item)
end end


it "should delegate to the cart" do it "adds a line to the cart" do
expect(@cart).to receive(:add_item).with(@item)

post_add_item post_add_item
end

it "should save the modified cart" do
expect(@cart).to receive(:save!)


post_add_item @cart.reload
expect(@cart.lines.map &:item).to include(@item)
end end


it "should redirect to items" do it "redirects to items" do
post_add_item post_add_item


expect(response).to redirect_to(root_item_category_path) expect(response).to redirect_to(root_item_category_path)
@@ -136,20 +128,14 @@ def post_add_item
context "when adding dish to cart" do context "when adding dish to cart" do


before(:each) do before(:each) do
@dish = stub_model(Dish) @dish = FactoryGirl.create(:dish)
Dish.stub(:find).and_return(@dish)
end end


it "should delegate to the cart" do it "adds a dish to the cart" do
expect(@cart).to receive(:add_dish).with(@dish)

post_add_dish post_add_dish
end

it "should save the modified cart" do
expect(@cart).to receive(:save!)


post_add_dish @cart.reload
expect(@cart.dishes).to include(@dish)
end end


it "should redirect to products" do it "should redirect to products" do
@@ -169,5 +155,4 @@ def post_add_dish
end end
end end



end end
@@ -4,6 +4,11 @@
FactoryGirl.define do FactoryGirl.define do


factory :cart do factory :cart do
trait :with_items do
after :create do |cart|
cart.lines = FactoryGirl.create_list(:cart_line, 2, cart: cart)
end
end
end end


end end
@@ -8,7 +8,7 @@ def notice
end end


def buying_confirmation_notice(name) def buying_confirmation_notice(name)
notice.that("has text '#{text}", "/p[contains(text(),'#{name}') and contains(text(),'#{CartLinesController.BUYING_CONFIRMATION_MESSAGE}')]") notice.that("has text '#{text}", "/p[contains(text(),'#{name}') and contains(text(),'#{CartLinesController::BUYING_CONFIRMATION_NOTICE}')]")
end end


def items_panel def items_panel

0 comments on commit 2c9fce1

Please sign in to comment.
You can’t perform that action at this time.