Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save gamov/5848212 to your computer and use it in GitHub Desktop.

Select an option

Save gamov/5848212 to your computer and use it in GitHub Desktop.

Revisions

  1. gamov created this gist Jun 24, 2013.
    32 changes: 32 additions & 0 deletions Capybara + rails: how to get the id of a newly created record
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,32 @@
    test 'create a standard po with items' do

    po_count = PurchaseOrder.count

    visit new_purchase_order_path
    page.select po.supplier.name, from: 'purchase_order_supplier_id'
    page.select po.company_profile.name, from: 'purchase_order_company_profile_id'
    page.select po.delivery_site.name, from: 'purchase_order_delivery_site_id'
    page.click_button 'purchase_order_submit'

    page.has_content? 'PurchaseOrder was successfully created.'
    assert_equal po_count + 1, PurchaseOrder.count

    #How can I get the newly created po....
    po = ?

    #adding items
    page.click_link 'Add items to this purchase order'
    #select all
    assert_equal 0, PurchasedItem.where(purchase_order_id: po.to_param).count
    page.all("table#new_purchased_items input[type='checkbox']").each{|cb| page.check(cb[:id])}
    page.click_button 'purchase_order_submit'
    #errors
    assert_equal 0, PurchasedItem.where(purchase_order_id: po.to_param).count
    page.has_content? 'error'

    page.all('table#new_purchased_items input[id$="unit_price"]').each{|ip| ip.set(3.5)}
    page.all('table#new_purchased_items input[id$="quantity"]').each{|ip| ip.set(5)}
    page.click_button 'purchase_order_submit'

    assert_not_equal 0, PurchasedItem.where(purchase_order_id: po.to_param).count
    end