6
votes

I have a simple controller test based on the Rails 4 docs (using Test::Unit):

test "should create user" do
  assert_difference('User.count') do
    post :create, user: users(:sample_user)
  end
  assert_redirected_to user_path(assigns(:user))
end

And, despite the actual user being created just fine, the test fails, saying:

1) Error: UsersControllerTest#test_should_create_user: NoMethodError: undefined method permit' for "832959492":String app/controllers/users_controller.rb:23:inuser_params' app/controllers/users_controller.rb:8:in create' test/controllers/users_controller_test.rb:11:inblock (2 levels) in ' test/controllers/users_controller_test.rb:10:in `block in '

For some reason, the test isn't recognizing the new Rails 4 permit method. Right now, I have the user_params as a private method in the controller. But, I tried moving it up to the actual create action and received the same error. The user creation is also 100% standard (nothing crazy at all- just like the Rails docs).

Does anyone know how I can rewrite or otherwise make this test pass?

1

1 Answers

19
votes

It seems like the data that is being POSTed to your create route is just a string, instead of a hash of attributes. What does users(:sample_user) return? Is it just an ID, or maybe an ActiveRecord object that is not being serialized correctly? If it's AR, maybe you need to send users(:sample_user).attributes as the payload?