Forum: Ruby on Rails test:integration Expected response to be a <:redirect>, but was <404>

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Fc6f5b908cfa12868292f985e0de2788?d=identicon&s=25 elle (Guest)
on 2009-04-20 05:14
(Received via mailing list)
Hello,

I'm trying to write some tests and I get a failure error on one test,
which I am not sure why or where is my mistake.

In my test/integration/product_test.rb I have:

require 'test_helper'

class ProductTest < ActionController::IntegrationTest
  fixtures :all

  test "product_administartion_by_admin" do
    category = Category.create(:name => 'Parts')

    quentin = new_session_as(:quentin)
    product = quentin.add_product :product => {
      :title => 'Clip',
      :category_id => category.id,
      :sku => 54321,
      :desctription => 'some text',
      :price => 8.71
    }
  end

  private

  module ProductTestDSL
    attr_writer :name

    def add_product(parameters)
      post "/product/create", parameters
      assert_response :redirect
      follow_redirect!
      assert_response :success
      assert_template "/product/show"
      return Product.find_by_title(parameters[:product][:title])
    end
  end

  def new_session_as(name)
    open_session do |session|
      session.extend(ProductTestDSL)
      session.name = name
      yield session if block_given?
    end
  end
end
------------------------------------

The error I get is:
 1) Failure:
test_product_administartion_by_admin(ProductTest)
    [/test/integration/product_test.rb:26:in `add_product'
     /test/integration/product_test.rb:10:in
`test_product_administartion_by_admin']:
Expected response to be a <:redirect>, but was <404>

1 tests, 1 assertions, 1 failures, 0 errors
rake aborted!
------------------------------------

Looking around -- someone had a problem that the new object was not
created or saved. One of the tests in products_controller_test.rb has
no problem creating a new product.

What am I missing?

TIA,
Elle
Fc6f5b908cfa12868292f985e0de2788?d=identicon&s=25 elle (Guest)
on 2009-04-20 11:37
(Received via mailing list)
Changing add_product method to this passes without failure. I've
noticed before that I don't use urls with /show anymore -- therefore
eliminated it from the method. What I wanted to ask is: is this code
good enough? or am I missing something important?
TIA

def add_product(parameters)
      category = Category.find(:all).first

      get 'products/new'
      assert_response :success
      assert_template "products/new.html.erb"
      assert_tag :tag => 'option', :attributes => { :value =>
category.id }

      post 'products/create', parameters
      assert_response :redirect
      follow_redirect!
      assert_response :success
      assert_template "products/show.html.erb"
      return Product.find_by_title(parameters[:product][:title])
    end
This topic is locked and can not be replied to.