Topic: drying up the testing code

Hi, this is the file thermo_detector_controller_test.rb:

  def test_edit
    get :edit, :id => @don_dafii_order.id
    assert_response :success
  end

  def test_send_invalid_stk
    file_uploaded =
uploaded_file("#{File.expand_path(RAILS_ROOT)}/test/fixtures/img/david_beckham.jpg",
'image/jpeg')
    post :create_stk, { :id => @don_dafii_order.id, :thermo_detector =>
{ :stk => file_uploaded }}
    assert_redirected_to :action => 'new_stk'
    assert_equal 'Dokumen yang dikirim harus berformat pdf.',
flash[:notice]
  end


and this is the file cartridge_heater_controller_test.rb:
  def test_edit
    get :edit, :id => @don_dafii_order.id
    assert_response :success
  end

  def test_send_invalid_stk
    file_uploaded =
uploaded_file("#{File.expand_path(RAILS_ROOT)}/test/fixtures/img/david_beckham.jpg",
'image/jpeg')
    post :create_stk, { :id => @don_dafii_order.id, :cartridge_heater =>
{ :stk => file_uploaded }}
    assert_redirected_to :action => 'new_stk'
    assert_equal 'Dokumen yang dikirim harus berformat pdf.',
flash[:notice]
  end


How do you drying up these methods? The test_edit method is same line by line so I hope I can put this method in test_helper.rb but yeah, the method is called when you run the ruby file but the context is invalid because @don_dafii_order is from fixtures which is different for both side (thermo and cartridge) and the test_helper does not have the fixtures. There are duplications in model testing code too.

Here is the file cartridge_heater_order_letter_test.rb

def test_update
    assert_equal @don_dafii_order.sales_name, @cartridge.sales_name
    @cartridge.sales_name = 'James Bond'
    assert @cartridge.save, @cartridge.errors.full_messages.join("; ")
    @cartridge.reload
    assert_equal 'James Bond', @cartridge.sales_name
  end

Here is the file thermo_detector_order_letter_test.rb
def test_update
    assert_equal @don_dafii_order.sales_name,
@thermo_detector.sales_name
    @thermo_detector.sales_name = 'James Bond'
    assert @thermo_detector.save,
@thermo_detector.errors.full_messages.join("; ")
    @thermo_detector.reload
    assert_equal 'James Bond', @thermo_detector.sales_name
  end

Again, I hope I can move this methods to another file and when it's called, the context will be for the called file. Thank you.