#coding: utf-8
require 'spec_helper'

# This spec was generated by rspec-rails when you ran the scaffold generator.
# It demonstrates how one might use RSpec to specify the controller code that
# was generated by Rails when you ran the scaffold generator.
#
# It assumes that the implementation code is generated by the rails scaffold
# generator.  If you are using any extension libraries to generate different
# controller code, this generated spec may or may not pass.
#
# It only uses APIs available in rails and/or rspec-rails.  There are a number
# of tools you can use to make these specs even more expressive, but we're
# sticking to rails and rspec-rails APIs to keep things simple and stable.
#
# Compared to earlier versions of this generator, there is very limited use of
# stubs and message expectations in this spec.  Stubs are only used when there
# is no simpler way to get a handle on the object needed for the example.
# Message expectations are only used when there is no simpler way to specify
# that an instance is receiving a specific message.

describe HandInnerTransitBillsController do
  login_admin
  render_views

  describe "GET index" do
    before(:each) do
      @hand_inner_transit_bill = Factory(:hand_inner_transit_bill)
    end

    it "assigns all hand_inner_transit_bills as @hand_inner_transit_bills" do
      get :index
      response.should be_success
    end
  end

  describe "GET show" do
    before(:each) do
      @hand_inner_transit_bill = Factory(:hand_inner_transit_bill)
    end


    it "should be success" do
      get :show, :id => @hand_inner_transit_bill
      response.should be_success
    end

    it "assigns the requested hand_inner_transit_bill as @hand_inner_transit_bill" do
      get :show, :id => @hand_inner_transit_bill
      response.should render_template('show')
    end
  end

  describe "GET new" do
    it "should be success" do
      get :new
      response.should be_success
    end
  end

  describe "GET edit" do
    before(:each) do
      @hand_inner_transit_bill = Factory(:hand_inner_transit_bill)
    end

    it "assigns the requested hand_inner_transit_bill as @hand_inner_transit_bill" do
      get :edit, :id => @hand_inner_transit_bill
      response.should render_template('edit')
    end
  end

  describe "POST create" do
    before(:each) do
      @attr = Factory.build(:hand_inner_transit_bill).attributes
      delete_attrs = %w[original_carrying_fee original_goods_fee original_from_short_carrying_fee insured_rate original_insured_amount id type original_insured_fee original_to_short_carrying_fee original_carrying_fee]
      @attr.delete_if { |key,value| delete_attrs.include?(key)}
    end
    describe "success" do
      it "能够成功保存票据信息" do
        lambda do
          post :create, :hand_inner_transit_bill => @attr
        end.should change(HandInnerTransitBill,:count).by(1)
      end

      it "redirects to the created hand_inner_transit_bill" do
        post :create, :hand_inner_transit_bill => @attr
        response.should redirect_to(hand_inner_transit_bill_path(assigns(:hand_inner_transit_bill)))
      end
    end

    describe "with invalid params" do
      it "re-renders the 'new' template" do
        post :create, :hand_inner_transit_bill => {}
        response.should render_template("new")
      end
    end

  end

  describe "PUT update" do
    before :each do

      @hand_inner_transit_bill = Factory(:hand_inner_transit_bill)
      @attr = {:goods_info => 'change_goods_info',:from_customer_name => 'changed customer name'}
    end

    describe "with valid params" do
      it "updates the requested hand_inner_transit_bill" do
        put :update, :id => @hand_inner_transit_bill, :hand_inner_transit_bill => @attr
        @hand_inner_transit_bill.reload
        @hand_inner_transit_bill.goods_info.should == @attr[:goods_info]
        @hand_inner_transit_bill.from_customer_name.should == @attr[:from_customer_name]
      end


      it "redirects to the hand_inner_transit_bill" do
        put :update, :id => @hand_inner_transit_bill,:hand_inner_transit_bill => @attr
        response.should redirect_to(hand_inner_transit_bill_path(@hand_inner_transit_bill))
      end
    end

    describe "with invalid params" do
      it "re-renders the 'edit' template" do
        put :update, :id => @hand_inner_transit_bill,:hand_inner_transit_bill => {:from_org_id => nil}
        response.should render_template("edit")
      end
    end

  end

  describe "DELETE destroy" do
    before(:each) do
      @hand_inner_transit_bill = Factory(:hand_inner_transit_bill)
    end

    it "destroys the requested hand_inner_transit_bill" do
      lambda do
        delete :destroy, :id => @hand_inner_transit_bill
      end.should change(HandInnerTransitBill,:count).by(-1)
    end

    it "redirects to the hand_inner_transit_bills list" do
      delete :destroy, :id => @hand_inner_transit_bill
      response.should redirect_to(root_url)
    end
  end
  describe "GET search" do
    it "should be success" do
      get :search,:format => :js
      response.should be_success
    end
    it "should render '/shared/carrying_bills/search'" do
      get :search,:format => :js
      response.should render_template('shared/carrying_bills/_search')
    end
  end
end
