Passing an object to post :create

Hi,

In a Rails functional test, you can say this:

post :create, :post => { :body => ‘This is my post.’, :title => ‘Welcome to my post’ }

This works until you change the Post class’ validation properties. Is there a way to convert an object (that happens to be already valid) to the hash form required by the functional test?

CmdJohnson

Let me clarify:

The moment you add any validations to a scaffold model the functional tests start to fail.

I currently use this solution:

class CommentsControllerTest < ActionController::TestCase def comment { :body => ‘Hello!’, :email => ‘commanderjohnson@gmail.com’, :name => ‘CmdJohnson’ } end

def test_should_create_comment assert_difference(‘Comment.count’) do post :create, :comment => comment end

assert_redirected_to comment_path(assigns(:comment))

end

Further tests here …

end

Possible solutions include:

  1. Keep a hash ‘role model’ that is always valid and must be changed with model validations
  2. Convert an existing object to a Hash that can be passed to the ‘get’ method.

Commander Johnson wrote:

Let me clarify:

The moment you add any validations to a scaffold model the functional tests start to fail.

[...]

Try using factories. There's a recent Railscast on the subject.

Best,