jeudi 23 avril 2015

More idiomatic way of testing that a database entry has been modified in Rails 3

In the following integration test using RSpec in Rails 3, I have to use new_product = Product.find(product) to get the results of changing what's in the database. This feels a little unidiomatic.

Is there any way of telling an ActiveRecord object to go to the database and get any changes that have happened since the object was created?

Alternatively, is there a more natural way of specifying that the database has been modified for this entry?

require 'rails_helper'

describe 'admin' do
  it 'should edit a product' do
    original_product = create(:product)

    visit products_path
    # ASSUMPTION: Only one product on page.
    click_link 'Edit product'

    fill_in 'product_name', with: 'Foomaster 9000'
    fill_in 'product_price', with: '12.34'

    click_button('Save product')

    # REVIEW: Can the following line be improved?
    new_product = Product.find(original_product)

    # original_product still has old values for product_name and price
    expect(new_product.product_name).to eq('Foomaster 9000')
    expect(new_product.price).to eq(12_34)
  end
end

Aucun commentaire:

Enregistrer un commentaire