# Passing an object to post :create

**URL:** <https://www.ruby-forum.com/t/passing-an-object-to-post-create/166292>\
**Category:** Rails\
**Created:** [April 23, 2009, 4:29pm UTC](https://www.ruby-forum.com/t/passing-an-object-to-post-create/166292 "2009-04-23T16:29:01Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Commander\_J](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/a8b319/32.png) [@Commander\_J](https://www.ruby-forum.com/u/Commander_J)\
**Post date:** [April 23, 2009, 4:29pm UTC](https://www.ruby-forum.com/t/passing-an-object-to-post-create/166292/1 "2009-04-23T16:29:01Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Commander\_J](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/a8b319/32.png) [@Commander\_J](https://www.ruby-forum.com/u/Commander_J)\
**Post date:** [April 25, 2009, 10:52pm UTC](https://www.ruby-forum.com/t/passing-an-object-to-post-create/166292/2 "2009-04-25T22:52:56Z")

</div>

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](http://www.marnen.org)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![Commander\_J](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/a8b319/32.png) [@Commander\_J](https://www.ruby-forum.com/u/Commander_J)\
**Post date:** [April 25, 2009, 9:24pm UTC](https://www.ruby-forum.com/t/passing-an-object-to-post-create/166292/3 "2009-04-25T21:24:41Z")

</div>

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. \<
