Rails controller test error: count didn't change by 1
Rails controller test error: count didn't change by 1
我正在为一个项目编写一些模型。我有一个 User.rb
class,它具有以下属性:
- first_name: string
- last_name: string
- address_line_1: string
- address_line_2: string
- town: string
- post_code: string
- tel_no: string
- email: string
- password_digest: string
- type:
我有第二个模型 Employee.rb
继承自 User.rb
。
User.rb
和 Employee.rb
都搭建了脚手架。 User
首先搭建,Employee
使用 --parent=User
选项搭建。
当运行测试时,我得到以下错误:
Failure:
EmployeesControllerTest#test_should_create_employee [filepath]:
"Employee.count" didn't change by 1.
Expected: 3
Actual :2
这是 employees_controller_test 中失败的代码:
setup do
@employee = employees(:employee_one)
end
test "should create employee" do
assert_difference('Employee.count') do
post employees_url, params: { employee: { first_name: @employee.first_name, last_name: @employee.last_name, address_line_1: @employee.address_line_1, address_line_2: @employee.address_line_2, town: @employee.town, post_code: @employee.post_code, tel_no: @employee.tel_no, email: @employee.email, password_digest: "@employee.password_digest", type: @employee.type } }
end
assert_redirected_to employee_url(Employee.last)
end
这是我的员工固定装置,在 employees.yml
employee_one:
first_name: "Employee1"
last_name: "Example"
address_line_1: "3 High Street"
address_line_2: "Flat 3"
town: "Glasgow"
post_code: "G15 9BL"
tel_no: "0123847439"
email: "employee1@employee1.com"
password_digest: "password"
type: "Employee"
我想我的 User.rb
也很重要,包括:
class User < ApplicationRecord
validates :first_name, presence: true, length: { maximum: 50 }
validates :last_name, presence: true, length: { maximum: 50 }
validates :address_line_1, presence: true, length: { maximum: 50 }
validates :address_line_2, presence: true, length: { maximum: 50 }, :allow_nil => true
validates :town, presence: true, length: { maximum: 50 }
validates :post_code, presence: true, length: { maximum: 10 }
validates :tel_no, presence: true, length: { maximum: 14 }
validates :email, presence: true, length: { maximum: 50 }
validates :password_digest, presence: true, length: { maximum: 256 }
validates :type, presence: true, length: { maximum: 15 }
end
我已经在这里待了 4 个小时左右,我想我只需要一双新鲜的眼睛。
如果我继续rails console --sandbox
并使用User.create单独输入2个用户,然后单独输入2个员工,没有问题。
什么导致了错误?
使用 --parent=User
选项搭建 employee.rb
模型,不会自动添加从 user.rb
到 employee.rb
的继承参数。
这意味着在 employees_controller.rb
中,定义允许参数的 employee_params
方法是:
def employee_params
params.require(:employee).permit(:type)
end
这意味着当测试在 Employees table 中创建一个条目时,除了 type
之外的每个参数都没有被插入。
我通过将 employee_params
更改为:
来修复它
def employee_params
params.require(:employee).permit(:first_name, :last_name, :address_line_1, :address_line_2, :town, :post_code, :tel_no , :email, :password_digest, :type)
end
现在所有测试都通过了。
我正在为一个项目编写一些模型。我有一个 User.rb
class,它具有以下属性:
- first_name: string
- last_name: string
- address_line_1: string
- address_line_2: string
- town: string
- post_code: string
- tel_no: string
- email: string
- password_digest: string
- type:
我有第二个模型 Employee.rb
继承自 User.rb
。
User.rb
和 Employee.rb
都搭建了脚手架。 User
首先搭建,Employee
使用 --parent=User
选项搭建。
当运行测试时,我得到以下错误:
Failure:
EmployeesControllerTest#test_should_create_employee [filepath]:
"Employee.count" didn't change by 1.
Expected: 3
Actual :2
这是 employees_controller_test 中失败的代码:
setup do
@employee = employees(:employee_one)
end
test "should create employee" do
assert_difference('Employee.count') do
post employees_url, params: { employee: { first_name: @employee.first_name, last_name: @employee.last_name, address_line_1: @employee.address_line_1, address_line_2: @employee.address_line_2, town: @employee.town, post_code: @employee.post_code, tel_no: @employee.tel_no, email: @employee.email, password_digest: "@employee.password_digest", type: @employee.type } }
end
assert_redirected_to employee_url(Employee.last)
end
这是我的员工固定装置,在 employees.yml
employee_one:
first_name: "Employee1"
last_name: "Example"
address_line_1: "3 High Street"
address_line_2: "Flat 3"
town: "Glasgow"
post_code: "G15 9BL"
tel_no: "0123847439"
email: "employee1@employee1.com"
password_digest: "password"
type: "Employee"
我想我的 User.rb
也很重要,包括:
class User < ApplicationRecord
validates :first_name, presence: true, length: { maximum: 50 }
validates :last_name, presence: true, length: { maximum: 50 }
validates :address_line_1, presence: true, length: { maximum: 50 }
validates :address_line_2, presence: true, length: { maximum: 50 }, :allow_nil => true
validates :town, presence: true, length: { maximum: 50 }
validates :post_code, presence: true, length: { maximum: 10 }
validates :tel_no, presence: true, length: { maximum: 14 }
validates :email, presence: true, length: { maximum: 50 }
validates :password_digest, presence: true, length: { maximum: 256 }
validates :type, presence: true, length: { maximum: 15 }
end
我已经在这里待了 4 个小时左右,我想我只需要一双新鲜的眼睛。
如果我继续rails console --sandbox
并使用User.create单独输入2个用户,然后单独输入2个员工,没有问题。
什么导致了错误?
使用 --parent=User
选项搭建 employee.rb
模型,不会自动添加从 user.rb
到 employee.rb
的继承参数。
这意味着在 employees_controller.rb
中,定义允许参数的 employee_params
方法是:
def employee_params
params.require(:employee).permit(:type)
end
这意味着当测试在 Employees table 中创建一个条目时,除了 type
之外的每个参数都没有被插入。
我通过将 employee_params
更改为:
def employee_params
params.require(:employee).permit(:first_name, :last_name, :address_line_1, :address_line_2, :town, :post_code, :tel_no , :email, :password_digest, :type)
end
现在所有测试都通过了。