Rails 4 minitest functional test failed for create method

687 views Asked by At

Here's my Code for controller

def create
  @post = Post.new(post_params)
    if @post.save
    redirect_to @post
  else
    render 'new'
  end
end

and my code for testing

test "should get create post" do
  assert_difference('Post.count') do
    post :create, post: {title: @post.title, content: @post.content}
  end
  assert_redirected_to post_path(assigns{:post})
end

The error that I'm getting is

ArgumentError: comparison of Array with Array failed test/controllers/posts_controller_test.rb:22:in `block in '

if i remove (assigns{:post}) and try the test

i get this error

ActionController::UrlGenerationError: No route matches {:action=>"show", :controller=>"posts"} missing required keys: [:id] Also how to test the if fails render 'new' part in controller.

Thanks

2

There are 2 answers

2
rkamun1 On

@post is not instantiated anywhere in the test. You need to pass in params like so

post :create, post: {title: 'title', content: 'content'}

0
Ben_Mir On

In the following line

assert_redirected_to post_path(assigns{:post})

use parenthesis instead of curly braces. The code should look like this:

assert_redirected_to post_path(assigns(:post))