require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')

describe AccountsController do

  # I left this in, but I'd be willing to remove it - it just ends up adding some noise to
  # some of the examples if we do
  def mock_account(stubs={})
    @mock_account ||= mock_model(Account, stubs)
  end

  describe "responding to GET /accounts" do

    # here I want to be specific about (:find).with(:all)
    it "should assign all accounts for the view" do
      Account.should_receive(:find).with(:all).and_return([mock_account])
      get :index
      assigns[:accounts].should == [mock_account]
    end

  end

  describe "responding to GET /accounts.xml" do

    it "should render all accounts as xml" do
      request.env["HTTP_ACCEPT"] = "application/xml"
      Account.should_receive(:find).with(:all).and_return(accounts = mock("Array of Accounts"))
      accounts.should_receive(:to_xml).and_return("generated XML")
      get :index
      response.body.should == "generated XML"
    end

  end

  describe "responding to GET /accounts/1" do

    it "should assign the identified account for the view" do
      Account.should_receive(:find).with("37").and_return(mock_account)
      get :show, :id => "37"
      assigns[:account].should equal(mock_account)
    end

  end

  describe "responding to GET /accounts/1.xml" do

    it "should render the identified account as xml" do
      request.env["HTTP_ACCEPT"] = "application/xml"
      Account.should_receive(:find).with("37").and_return(mock_account)
      mock_account.should_receive(:to_xml).and_return("generated XML")
      get :show, :id => "37"
      response.body.should == "generated XML"
    end

  end

  describe "responding to GET /accounts/new" do

    it "should assign a new account for the view" do
      Account.should_receive(:new).and_return(mock_account)
      get :new
      assigns[:account].should equal(mock_account)
    end

  end

  describe "responding to GET /accounts/1/edit" do

    it "should assign the requested Account for the view" do
      Account.should_receive(:find).with("37").and_return(mock_account)
      get :edit, :id => "37"
      assigns[:account].should equal(mock_account)
    end

  end

  describe "responding to POST /accounts" do

    describe "with successful save" do

      it "should assign a newly created account for the view" do
        Account.should_receive(:new).with({'these' => 'params'}).and_return(mock_account(:save => true))
        post :create, :account => {:these => 'params'}
        assigns(:account).should equal(mock_account)
      end

      it "should redirect to the created account" do
        Account.stub!(:new).and_return(mock_account(:save => true))
        post :create, :account => {}
        response.should redirect_to(account_url(mock_account))
      end

    end

    describe "with failed save" do

      it "should assign an unsaved account for the view" do
        Account.stub!(:new).with({'these' => 'params'}).and_return(mock_account(:save => false))
        post :create, :account => {:these => 'params'}
        assigns(:account).should equal(mock_account)
      end

      it "should re-render the 'new' template" do
        Account.stub!(:new).and_return(mock_account(:save => false))
        post :create, :account => {}
        response.should render_template('new')
      end

    end

  end

  describe "responding to PUT /accounts/1" do

    describe "with successful update" do

      it "should update the requested account" do
        Account.should_receive(:find).with("37").and_return(mock_account)
        mock_account.should_receive(:update_attributes).with({'these' => 'params'})
        put :update, :id => "37", :account => {:these => 'params'}
      end

      it "should assign the found account for the view" do
        Account.stub!(:find).and_return(mock_account(:update_attributes => true))
        put :update, :id => "1"
        assigns(:account).should equal(mock_account)
      end

      it "should redirect to the account" do
        Account.stub!(:find).and_return(mock_account(:update_attributes => true))
        put :update, :id => "1"
        response.should redirect_to(account_url(mock_account))
      end

    end

    describe "with failed update" do

      it "should try to update the requested account" do
        Account.should_receive(:find).with("37").and_return(mock_account)
        mock_account.should_receive(:update_attributes).with({'these' => 'params'})
        put :update, :id => "37", :account => {:these => 'params'}
      end

      it "should assign the account for the view" do
        Account.stub!(:find).and_return(mock_account(:update_attributes => false))
        put :update, :id => "1"
        assigns(:account).should equal(mock_account)
      end

      it "should re-render the 'edit' template" do
        Account.stub!(:find).and_return(mock_account(:update_attributes => false))
        put :update, :id => "1"
        response.should render_template('edit')
      end

    end

  end

  describe "responding to DELETE /accounts/1" do

    it "should destroy the requested account" do
      Account.should_receive(:find).with("37").and_return(mock_account)
      mock_account.should_receive(:destroy)
      delete :destroy, :id => "37"
    end

    it "should redirect to the accounts list" do
      Account.stub!(:find).and_return(mock_account(:destroy => true))
      delete :destroy, :id => "1"
      response.should redirect_to(accounts_url)
    end

  end

end