Rails 4 控制器测试“未定义方法许可" [英] Rails 4 Controller Test "Undefined Method Permit"

查看:34
本文介绍了Rails 4 控制器测试“未定义方法许可"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个基于 Rails 4 文档的简单控制器测试(使用 Test::Unit):

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) 错误:UsersControllerTest#test_should_create_user:NoMethodError: 未定义方法 permit' for "832959492":Stringapp/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 '

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 '

出于某种原因,该测试无法识别新的 Rails 4 permit 方法.现在,我将 user_params 作为控制器中的私有方法.但是,我尝试将其移动到实际的 create 操作并收到相同的错误.用户创建也是 100% 标准的(一点也不疯狂——就像 Rails 文档一样).

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?

推荐答案

看起来正在 POSTed 到您的 create 路由的数据只是一个字符串,而不是属性的散列.users(:sample_user) 返回什么?它只是一个 ID,还是一个没有正确序列化的 ActiveRecord 对象?如果是 AR,也许您需要发送 users(:sample_user).attributes 作为有效负载?

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?

这篇关于Rails 4 控制器测试“未定义方法许可"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆