Skip to content

Instantly share code, notes, and snippets.

@feroult
Created February 7, 2011 21:35
Show Gist options
  • Save feroult/815267 to your computer and use it in GitHub Desktop.
Save feroult/815267 to your computer and use it in GitHub Desktop.
Macro template example
require 'spec_helper'
describe LineItemsController do
mock :order
mock :customer
# GET /orders/11/line_items
get :index, :order_id => 11 do
default :stub => :off
before(:each) { LineItem.stub(:find_all_by_order_id).and_return([mock_line_item]) }
end
# GET /customers/22/line_items/new
get :new, :customer_id => 22
# GET /orders/11/line_items/new
get :new, :order_id => 11
macro_template :create do
default :stub => :off, :redirect => :off
before(:each) { LineItem.stub(:new).and_return(mock_line_item(:order_id => 11)) }
with_valid_params do
it { should redirect_to(order_line_items_url(:order_id => 11)) }
end
it "associates the line item with the order" do
mock_line_item.should_receive("order=".to_sym).with(mock_order)
subject
end
end
# POST /customers/22/line_items
post :create, :customer_id => 22 do
description("under customer's path")
before(:each) { Order.stub(:new).with(:customer_id => 22).and_return(mock_order(:id => 11)) }
end
# POST /orders/11/line_items
post :create, :order_id => 11 do
description("under order's path")
before(:each) { Order.stub(:find).with(11).and_return(mock_order(:id =>11)) }
end
# GET /orders/11/line_items/6
get :edit, :order_id => 11, :id => 6
# PUT /orders/11/line_items/6
put :update, :order_id => 11, :id => 6 do
default :stub => :off, :redirect => :off
before(:each) { LineItem.stub(:find).with(6).and_return(mock_line_item(:order_id => 11)) }
with_valid_params do
it { should redirect_to(order_line_items_url(:order_id => 11)) }
end
end
# DELETE /orders/11/line_items/6
delete :destroy, :order_id => 11, :id => 6 do
default :stub => :off, :redirect => :off
before(:each) { LineItem.stub(:find).with(6).and_return(mock_line_item(:order_id => 11)) }
it { should redirect_to(order_line_items_url(:order_id => 11)) }
end
context "helpers" do
describe ".current_customer" do
let(:customer) { mock_customer(:id => 22) }
let(:order) { mock_order(:customer => customer) }
context "under customer's path" do
it "returns the current customer based on customer_id param" do
controller.stub!(:params => {:customer_id => 22})
Customer.stub(:find).with(22).and_return(customer)
controller.current_customer.should == customer
end
end
context "under order's path" do
it "returns the current customer based on customer_id param" do
controller.stub!(:params => {:order_id => 11})
Order.stub(:find).with(11).and_return(order)
controller.current_customer.should == customer
end
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment