title: How to test the update method in Rails
body: <p>I am struggling with testing the update method in Rails. I am using the standard built in testing framework (<code>test::unit</code>) and Rails 3.0.8. I have created a minimum application for testing this now, but I can't get it to work. Here is what I do:</p>
  
  <p>I create a new blank rails application:</p>
  
  <pre><code>rails new testapp
  </code></pre>
  
  <p>Create a model called Collection:</p>
  
  <pre><code>rails generate model collection name:string
  </code></pre>
  
  <p>Run rake db:migrate:</p>
  
  <pre><code>rake db:migrate
  </code></pre>
  
  <p>Create a controller called Collections with an update method:</p>
  
  <pre><code>rails generate controller collections update
  </code></pre>
  
  <p>In collection_controller.rb I add this minimum update method:</p>
  
  <pre><code>def update
    @collection = Collection.find(params[:id])
    @collection.update_attributes(params[:collection])
  end
  </code></pre>
  
  <p>The test fixtures are the defaults (collections.yml):</p>
  
  <pre><code>one:
    name: MyString
  
  two:
    name: MyString
  </code></pre>
  
  <p>Then I add this to the collection_controller_test.rb under functional:</p>
  
  <pre><code>test "should update collection" do
    put :update, :id =&gt; collections(:one), :collection =&gt; {:name =&gt; 'MyString2'}
    assert_equal "MyString2", collections(:one).name
  end
  </code></pre>
  
  <p>When I run the test:</p>
  
  <pre><code>rake test:functionals
  </code></pre>
  
  <p>It fails with this message:</p>
  
  <pre><code>test_should_update_collection(CollectionsControllerTest) [/Users/atle/Documents/Rails/testapp/test/functional/collections_controller_test.rb:6]:
  &lt;"MyString2"&gt; expected but was
  &lt;"MyString"&gt;.
  </code></pre>
  
  <p>Here are the output from test.log:</p>
  
  <pre><code>[1m[36mSQL (0.2ms)[0m  [1m SELECT name
  FROM sqlite_master
  WHERE type = 'table' AND NOT name = 'sqlite_sequence'
  [0m
  [1m[35mCollection Load (0.1ms)[0m  SELECT "collections".* FROM "collections" WHERE "collections"."id" = 980190962 LIMIT 1
  Processing by CollectionsController#update as HTML
  Parameters: {"id"=&gt;#&lt;Collection id: 980190962, name: "MyString", created_at: "2011-06-12 13:14:13", updated_at: "2011-06-12 13:14:13"&gt;, "collection"=&gt;{"name"=&gt;"MyString2"}}
  [1m[36mCollection Load (0.2ms)[0m  [1mSELECT "collections".* FROM "collections" WHERE "collections"."id" = 980190962 LIMIT 1[0m
  [1m[35mAREL (0.2ms)[0m  UPDATE "collections" SET "name" = 'MyString2', "updated_at" = '2011-06-12 13:14:13.394135' WHERE "collections"."id" = 980190962
  Rendered collections/update.html.erb within layouts/application (1.5ms)
  Completed 200 OK in 9ms (Views: 4.5ms | ActiveRecord: 1.2ms)
  </code></pre>
  
  <p>In the log I can see the <code>UPDATE</code> statement, but I never see a new <code>SELECT</code> statement.</p>
  
  <p>Could someone explain to me what I am doing wrong please?</p>