views:

123

answers:

1

I have a model called "Post" which is a nested resource under a model called "Project" and I'm trying to test the controller. The code works find in my browser, but I can't get it to work in the test. Here's the test

context "on POST to :edit" do
  setup do
    post( :edit,
      :project_id => @project1.to_param, 
      :id => @post1.to_param, 
      :post => { :title => 'new title', :text => 'other text' } 
    )
  end
  should_assign_to :post
  should_assign_to :project
  should_respond_with :success

  should "update post values" do
    assert_equal 'other text', assigns['post'].text
  end

Any idea how I'm screwing this up?

A: 

This was the result of me not understanding Rails' REST architecture -or- the post syntax. I should have been using PUT instead of POST, and the call should have looked like this:

context "on PUT to :update" do
  setup do
    put :update, { 
      :project_id => @project1.to_param, 
      :id => @post1.to_param, 
      :post => { :title => 'new title', :text => 'other text' } 
    } 
  end

  should_assign_to :post
  should_assign_to :project
  should_respond_with :success

  should "update post values" do
    assert_equal 'new title', assigns['post'].title
    assert_equal 'other text', assigns['post'].text
  end
end

I had been using the wrong syntax because for some reason it was still handling my nested id's correctly.

Ryan