如何使用 Minitest 为 Rails 中具有嵌套属性的控制器设置单元测试
How to setup unit tests for a controller with nested attributed in Rails with Minitest
我有一个 person
模型,它是我 Rails 4 应用程序中所有类型用户的基础。我有一个 administrator
模型,一个 worker
模型,一个 client
模型,它们都接受嵌套属性。这是我的 administrator
模型
class Administrator < ActiveRecord::Base
belongs_to :person
accepts_nested_attributes_for :person
... other model code here
end
在我的 SQLite 数据库中,我有一个约束,表明 person_id
不能为空。
administrator
的控制器测试失败并显示消息'
ActiveRecord::StatementInvalid: SQLite3::ConstraintException: NOT NULL
constraint failed: administrators.person_id: INSERT INTO "administrators"
("created_at", "updated_at") VALUES (?, ?)
administrators_controller_test.rb
包含以下内容,
setup do
@administrator = administrators(:administrator_montgomery_burns)
@person = people(:person_montgomery_burns)
end
test "should create administrator" do
assert_difference('Administrator.count') do
post :create,
administrator: {
person_id: @administrator.person_id
}
end
assert_redirected_to administrator_path(assigns(:administrator))
end
我也试过了,
test "should create administrator" do
assert_difference('Administrator.count') do
post :create,
administrator: {
person: {
first_name: @person.first_name,
... all other person attributes
}
}
end
assert_redirected_to administrator_path(assigns(:administrator))
end
知道应该怎么做吗?
我已经解决了。我在开发模式(它正在工作的地方)和测试模式(它不工作的地方)查看了控制器中的日志文件 params
哈希。而不是
test "should create administrator" do
assert_difference('Administrator.count') do
post :create,
administrator: {
person: {
first_name: @person.first_name,
... all other person attributes
}
}
end
assert_redirected_to administrator_path(assigns(:administrator))
end
使用
test "should create administrator" do
assert_difference('Administrator.count') do
post :create,
administrator: {
person_attributes: {
first_name: @person.first_name,
... all other person attributes
}
}
end
assert_redirected_to administrator_path(assigns(:administrator))
end
只需将 person:
更改为 person_attributes:
。
我有一个 person
模型,它是我 Rails 4 应用程序中所有类型用户的基础。我有一个 administrator
模型,一个 worker
模型,一个 client
模型,它们都接受嵌套属性。这是我的 administrator
模型
class Administrator < ActiveRecord::Base
belongs_to :person
accepts_nested_attributes_for :person
... other model code here
end
在我的 SQLite 数据库中,我有一个约束,表明 person_id
不能为空。
administrator
的控制器测试失败并显示消息'
ActiveRecord::StatementInvalid: SQLite3::ConstraintException: NOT NULL
constraint failed: administrators.person_id: INSERT INTO "administrators"
("created_at", "updated_at") VALUES (?, ?)
administrators_controller_test.rb
包含以下内容,
setup do
@administrator = administrators(:administrator_montgomery_burns)
@person = people(:person_montgomery_burns)
end
test "should create administrator" do
assert_difference('Administrator.count') do
post :create,
administrator: {
person_id: @administrator.person_id
}
end
assert_redirected_to administrator_path(assigns(:administrator))
end
我也试过了,
test "should create administrator" do
assert_difference('Administrator.count') do
post :create,
administrator: {
person: {
first_name: @person.first_name,
... all other person attributes
}
}
end
assert_redirected_to administrator_path(assigns(:administrator))
end
知道应该怎么做吗?
我已经解决了。我在开发模式(它正在工作的地方)和测试模式(它不工作的地方)查看了控制器中的日志文件 params
哈希。而不是
test "should create administrator" do
assert_difference('Administrator.count') do
post :create,
administrator: {
person: {
first_name: @person.first_name,
... all other person attributes
}
}
end
assert_redirected_to administrator_path(assigns(:administrator))
end
使用
test "should create administrator" do
assert_difference('Administrator.count') do
post :create,
administrator: {
person_attributes: {
first_name: @person.first_name,
... all other person attributes
}
}
end
assert_redirected_to administrator_path(assigns(:administrator))
end
只需将 person:
更改为 person_attributes:
。