Rails 4 Controller Test "Undefined Method Resolution"

I have a simple controller test based on 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 fact that the actual user is created just fine, the test does not work, saying:

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

For some reason, the test does not recognize the new Rails 4 permit method. Right now I have user_params as a private method in the controller. But I tried moving it to a valid create action and got the same error. User creation is also 100% standard (not crazy at all - just like Rails docs).

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

+6
source share
1 answer

It seems that the data that POST ed for your create route is just a string, not an attribute hash. What returns users(:sample_user) ? Is it just an identifier or an ActiveRecord object that is not serializing correctly? If it's AR, maybe you need to send users(:sample_user).attributes as a payload?

+19
source

Source: https://habr.com/ru/post/954725/


All Articles