收录日期:2019/03/20 19:53:44 时间:2009-07-29 19:00:52 标签:ruby-on-rails,unit-testing,tdd,shoulda

I have a test that's failing even though the operation actually works when I test it in the browser. Something wrong with my test, looks like.

I'm using Shoulda and fixtures.

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

Fails with "Attribute did not get updated."

Here is the controller code under test:

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

I can't see a params[:address] specified in the parameters you are passing to your action in the test. It looks to me like it should be:

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

I suspect your street1 address field is named correctly in your form as address[street1] which is why it is working via the browser.