Passing an object to post :create


#1

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


#2

Commander J. 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,

Marnen Laibow-Koser
http://www.marnen.org
removed_email_address@domain.invalid


#3

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 => ‘removed_email_address@domain.invalid’, :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.

On Thu, Apr 23, 2009 at 4:27 PM, Commander J. <