Topic: Functional testing a create method

I'm trying to functional test my create method but it doesn't seem to be setting the parameters.  What am I doing wrong?

The controller is:

    def create
    @theme = Theme.new
    if @theme.save(params[:theme])
      flash[:notice] = 'Theme was successfully created.'
      redirect_to :action => 'list'
    else
      render :action => 'new'
    end
  end

And the test is:

    def test_create   
    post :create,  :theme => {:title => "title", :description => "description"}
    assert assigns(:theme).valid?
  end

However the theme isn't valid.  If I do
puts assigns(:theme).errors.full_messages

then it tells me that title and description are empty.

I can't seem to find any good examples of similar functional tests to see what I might be doing wrong.

Re: Functional testing a create method

First off, are your unit tests for your Theme model passing? Check there first before you go rewriting your functional test.

vinnie - rails forum admin

Re: Functional testing a create method

Yup.  All my unit tests work fine.

Re: Functional testing a create method

You need to pass the params in the "new" method, not the "save" method:

  def create
    @theme = Theme.new(params[:theme])
    if @theme.save
      flash[:notice] = 'Theme was successfully created.'
      redirect_to :action => 'list'
    else
      render :action => 'new'
    end
  end

Railscasts - Free Ruby on Rails Screencasts

Re: Functional testing a create method

Oooops!  Thanks. smile