Domanda

Ho un test che fallisce anche se l'operazione funziona effettivamente quando lo collaudo nel browser. Qualcosa non va nel mio test, sembra.

Sto usando Shoulda e infissi.

require 'test_helper'

class AddressesControllerTest < ActionController::TestCase
  context 'on PUT to :update' do
    setup do
      put(:update, {
          :id => addresses(:mary_publics_address).id,
          :street1 => '123 Now St.'
        }, { :user_id => users(:stan).id})
    end
    should 'update the Address' do
      a = Address.find(addresses(:mary_publics_address).id)
      assert(a.street1 == '123 Now St.', 'Attribute did not get updated.')
    end
  end
end

Errore con " L'attributo non è stato aggiornato. "

Ecco il codice del controller in prova:

class AddressesController < ApplicationController
  def update
    @address = Address.find(params[:id])
    @address.update_attributes!(params[:address])
    render(:text => "Address with ID #{params[:id]} updated")
  end
end
È stato utile?

Soluzione

Non riesco a vedere un parametro [: address] specificato nei parametri che stai passando alla tua azione nel test. Mi sembra che dovrebbe essere:

put(:update, {
        :id => addresses(:mary_publics_address).id,
        :address => { :street1 => '123 Now St.' }
      }, { :user_id => users(:stan).id})

Sospetto che il tuo campo dell'indirizzo street1 sia stato nominato correttamente nel tuo modulo come address [street1] , motivo per cui funziona tramite il browser.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top