J86
J86

Reputation: 15237

assert_redirected_to in Rails Testing

I am trying to run some tests on my Customers controller. When I manually test the controller, all is working fine, however when I do written Integration tests on it, I get an error. Here is my Test code:

context "non-empty Customer model" do
  setup do
    @customer = Customer.first || Customer.create(:name => "John", :address => "123 Street Cool", :telephone => "01484349361", :email => "[email protected]")
  end

  should "be able to create" do
    get "/customers/new"
    assert_response :success
    post "/customers/create", :post => @customer
    # assert_response :success
    assert_redirected_to "/customers/list"
  end

And the error I am getting is on the assert_redirected_to line and it says:

Expected block to return true value.

Here is my controller code the for new/create actions:

  def new
    @customer = Customer.new

    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @customer }
    end
  end

  def create
    # Instantiate a new object using form params
    @customer = Customer.new(params[:customer])
    # Save the object
    if @customer.save
      # If save succeeds, redirect to the list action
      flash[:notice] = "Customer created."
      redirect_to(:action => 'list')
    else
      # If save fails, redisplay the form so user can fix problems
      render('new')
    end
  end

How can I get the tests working?

Upvotes: 3

Views: 6550

Answers (1)

Unixmonkey
Unixmonkey

Reputation: 18784

Looks like you are sending the wrong info as params.

should "be able to create" do
  get "/customers/new"
  assert_response :success
  post "/customers/create", :customer => @customer.attributes
  assert_redirected_to "/customers/list"
end

Upvotes: 4

Related Questions