# -*- encoding : utf-8 -*-
#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 the Rails when you ran the scaffold generator.

describe SendersController do
  login_admin
  render_views

  before(:each) do
    @sender = Factory(:sender)
  end


  describe "GET index" do
    it "assigns all senders as @senders" do
      get :index
      response.should be_success
    end
  end

  describe "GET show" do

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

    it "assigns the requested sender as @sender" do
      get :show, :id => @sender
      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
    it "assigns the requested sender as @sender" do
      get :edit, :id => @sender
      response.should render_template('edit')
    end
  end

  describe "POST create" do
    before(:each) do
      @attr = Factory.build(:sender).attributes
    end
    describe "success" do
      it "能够成功保存送货信息" do
        lambda do
          post :create, :sender => @attr
        end.should change(Sender,:count).by(1)
      end

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

    describe "with invalid params" do

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

  end

  describe "PUT update" do
    before :each do
      @attr = {:name => 'updated sender name'}
    end

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


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

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

  end

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

    it "redirects to the senders list" do
      delete :destroy, :id => @sender 
      response.should redirect_to(senders_url)
    end
  end
end

