Sergio Cambra .:: entreCables S.L. ::. wrote:
> >
> > How stable is the Master tree, are test being written to ensure stuff
> > does not break with each update/commit ?
>
>
> There are too few tests. We should write more tests, all help is welcome.


I agree.

I posted a starting point for this some months back - this is what I use 
in my frontend functional tests to do a simple CRUD test on the major 
actions.  It's waaay too simple and could easily be improved, but I 
think it's an interesting starting point that at least the whole 
application ain't broken.

Ideally some unit tests for the individual stuff would be great - I 
think anyone with basic testing ability could pitch in here.  Start with 
the list view and just test out the basic helper functions and view 
functions?

OK my active_scaffold_test_base.rb is attached.  Then in any functional 
test I want to run I simply do:


  include ActiveScaffoldTestBase
 
  def setup
    @controller = DomainsController.new
    @request    = ActionController::TestRequest.new
    @response   = ActionController::TestResponse.new
 
    @as_new_record = {:domain=>"abc.com"} # or whatever you need to 
setup a new record
    assert_not_nil login_as(:admin_mailasail)
  end


It would be great if someone could take this and adapt it so that the 
list of actions is derived from the config, etc.  Also I think a cleaned 
up version of this could be interesting to put into AS itself as a 
helper function?

Cheers

Ed W




--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"ActiveScaffold : Ruby on Rails plugin" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/activescaffold?hl=en
-~----------~----~----~----~------~----~------~--~---

module ActiveScaffoldTestBase 
  
  def test_as_index
    get :index
    assert_response :success
    assert_template 'list'
  end

  def test_as_list
    get :list

    assert_response :success
    assert_template 'list'

    assert_not_nil assigns(:page)
    assert_not_nil assigns(:records)
  end
  
  def test_as_show
    get :show, :id => find_random_id

    assert_response :success
    assert_template 'show'

    assert_not_nil assigns(:record)
    assert assigns(:record).valid?, "Record failed validation - id: 
#{assigns(:record).id}\r\nErrors: 
#{assigns(:record).errors.full_messages.to_sentence}"
  end

  def test_as_show_js
    xhr :get, :show, :id => find_random_id

    assert_response :success
    assert_match /text\/javascript/, @response.content_type
    assert_match /loading-indicator/, @response.body
    assert_template '_show'

    assert_valid_assigns_record
  end

  def test_as_new
    get :new

    assert_response :success
    assert_template 'create'

    assert_not_nil assigns(:record)
  end

  def test_as_new_js
    xhr :get, :new

    assert_response :success
    assert_match /text\/javascript/, @response.content_type
    assert_match /loading-indicator/, @response.body
    assert_template '_create_form'

    assert_not_nil assigns(:record)
  end

  def test_as_create
    num_records = find_model.count

    as_new_record = @as_new_record || {}
    post :create, :record => as_new_record

    assert_response :redirect, assigns(:record).errors.full_messages.to_sentence
    assert_redirected_to :action => 'index'

    assert_equal num_records + 1, find_model.count
  end

  def test_as_create_js
    num_records = find_model.count

    as_new_record = @as_new_record || {}
    xhr :post, :create, :record => as_new_record

    assert_response :success
    assert_match /text\/javascript/, @response.content_type
    assert_match /Element\.insert\(/, @response.body
#    assert_match /link\.close_with_refresh/, @response.body

    assert_equal num_records + 1, find_model.count
  end

  def test_as_edit
    get :edit, :id => find_random_id

    assert_response :success
    assert_template 'update_form'

    assert_valid_assigns_record
  end

  def test_as_edit
    xhr :get, :edit, :id => find_random_id

    assert_response :success
    assert_template '_update_form'

    assert_valid_assigns_record
  end

  def test_as_edit_js
    xhr :get, :edit, :id => find_random_id

    assert_response :success
    assert_match /text\/javascript/, @response.content_type
    assert_match /loading-indicator/, @response.body
    assert_template '_update_form'

    assert_valid_assigns_record
  end

  def test_as_update
    # This gets us the default record for an edit action
    id = find_random_id
    get :edit, :id => id
    record = assigns(:record)
    assert_not_nil assigns(:record)
    
    as_update_record = @as_update_record || {}
    post :update, :id => id, :record => as_update_record
    assert_response :redirect, assigns(:record).errors.full_messages.to_sentence
    assert_redirected_to :action => 'index', :id => id
  end

  def test_as_update_js
    # This gets us the default record for an edit action
    id = find_random_id
    xhr :get, :edit, :id => id
    record = assigns(:record)
    assert_not_nil assigns(:record)
    
    as_update_record = @as_update_record || {}
    xhr :post, :update, :id => id, :record => as_update_record
    assert_match /text\/javascript/, @response.content_type
    assert_match /link\.close_with_refresh/, @response.body
  end

  def test_as_destroy
    id = find_random_id
    assert_not_nil(id, "No records found to delete!")
    assert_not_nil find_model.find(id)

    post :destroy, :id => id
    assert_response :redirect
    assert_redirected_to :action => 'index'

    assert_raise(ActiveRecord::RecordNotFound) {
      find_model.find(id)
    }
  end

  def test_as_destroy_js
    id = find_random_id
    assert_not_nil(id, "No records found to delete!")
    assert_not_nil find_model.find(id)

    xhr :post, :destroy, :id => id
    assert_match /text\/javascript/, @response.content_type
    assert_match /Element\.remove/, @response.body
    assert_match /ActiveScaffold\.reload_if_empty/, @response.body

    assert_raise(ActiveRecord::RecordNotFound) {
      find_model.find(id)
    }
  end

private
  def find_model
    @controller.active_scaffold_config.model
  end
  
  def find_random_id
    find_model.find(:first).id
  end

  def assert_valid_assigns_record
    assert_not_nil assigns(:record)
    assert assigns(:record).valid?, "Record failed validation - id: 
#{assigns(:record).id}\r\nErrors: 
#{assigns(:record).errors.full_messages.to_sentence}"
  end
end

Reply via email to