Forum: Ruby on Rails Passing an object to post :create

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Commander J. (Guest)
on 2009-04-23 18:29
(Received via mailing list)
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
Commander J. (Guest)
on 2009-04-25 23:24
(Received via mailing list)
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. <
Marnen L. (Guest)
on 2009-04-26 00:52
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
This topic is locked and can not be replied to.