Sha256: 9aa9011536539568fddb82823ad65489860719f1025e672061ca49129f07c089

Contents?: true

Size: 1.79 KB

Versions: 2

Compression:

Stored size: 1.79 KB

Contents

require File.dirname(__FILE__) + '/test_helper'

class Pet
  def self.human_name; 'Pet'; end
end

class PetsController < InheritedResources::Base
  attr_accessor :current_user
  
  def edit
    @pet = 'new pet'
    edit!
  end

  protected
    def collection
      @pets ||= end_of_association_chain.all
    end

    def begin_of_association_chain
      @current_user
    end
end

class AssociationChainBaseHelpersTest < ActionController::TestCase
  tests PetsController

  def setup
    @controller.current_user = mock()
  end

  def test_begin_of_association_chain_is_called_on_index
    @controller.current_user.expects(:pets).returns(Pet)
    Pet.expects(:all).returns(mock_pet)
    get :index
    assert_response :success
    assert 'Index HTML', @response.body.strip
  end

  def test_begin_of_association_chain_is_called_on_new
    @controller.current_user.expects(:pets).returns(Pet)
    Pet.expects(:build).returns(mock_pet)
    get :new
    assert_response :success
    assert 'New HTML', @response.body.strip
  end

  def test_begin_of_association_chain_is_called_on_show
    @controller.current_user.expects(:pets).returns(Pet)
    Pet.expects(:find).with('47').returns(mock_pet)
    get :show, :id => '47'
    assert_response :success
    assert 'Show HTML', @response.body.strip
  end

  def test_instance_variable_should_not_be_set_if_already_defined
    @controller.current_user.expects(:pets).never
    Pet.expects(:find).never
    get :edit
    assert_response :success
    assert_equal 'new pet', assigns(:pet)
  end

  def test_model_is_not_initialized_with_nil
    @controller.current_user.expects(:pets).returns(Pet)
    Pet.expects(:build).with({}).returns(mock_pet)
    get :new
    assert mock_pet, assigns(:pet)
  end

  protected
    def mock_pet(stubs={})
      @mock_pet ||= mock(stubs)
    end

end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
emmanuel-inherited_resources-0.9.1 test/base_helpers_test.rb
emmanuel-inherited_resources-0.9.2 test/base_helpers_test.rb