Test Couldn't find User without an ID

I am trying to run this test
setup do

@comment = comments(:hello)
@comment.commenter = "cleb"
@comment.body = "hello"
@comment.user_id = 1

end
test “should create comment” do

assert_difference('Comment.count') do
  post :create, :comment => @comment.attributes
end

assert_redirected_to comment_path(assigns(:comment))

end

It comes up with

test_should_create_comment(CommentsControllerTest):
ActiveRecord::RecordNotFound: Couldn’t find User without an ID

This is from comments.yml

hello:
commenter: cleb
body: hello
user_id: 1

This is from comments.controller

def create
@cuser = @current_user
@user = User.find(params[:user_id])
@user.comments.create(:user_id => @user.id, :commenter =>
@cuser.login, :body => params[:comment][:body])
respond_to do |format|
format.js
format.html {redirect_to user_path}
end
end

What am I doing wrong?

Check the params[:user_id] i think it was nil

Ahmy Y.

Cpx Cpx wrote in post #1053539:

Check the params[:user_id] i think it was nil

Ahmy Y.

So it is. How can I introduce a user in the fixtures or setup to give it
a value?

Cpx Cpx wrote in post #1053588:

This line:
post :create, :comment => @comment.attributes

The second parameter (hash) is the content of params in your controller.
So
basically your params[:comment] would be @comment.attributes

So if you expected params[:user_id] then you should pass it to the post
call

Ahmy Y.

:user_id is one of the attributes

setup do
@comment = comments(:hello)
@comment.commenter = “cleb”
@comment.body = “hello”
@comment.user_id = 1
end

Neil

so basically its not params[:user_id] it’s params[:comment][:user_id]
Ahmy Y.

This line:
post :create, :comment => @comment.attributes

The second parameter (hash) is the content of params in your controller.
So
basically your params[:comment] would be @comment.attributes

So if you expected params[:user_id] then you should pass it to the post
call

Ahmy Y.

Cpx Cpx wrote in post #1053588:

This line:
post :create, :comment => @comment.attributes

The second parameter (hash) is the content of params in your controller.
So
basically your params[:comment] would be @comment.attributes

So if you expected params[:user_id] then you should pass it to the post
call

Ahmy Y.
This did it

assert_difference(‘Comment.count’) do
post :create, :comment => @comment.attributes, :user_id => 2
end

Neil

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs