为什么 validate_acceptance_of 没有破坏我的功能测试?
Why doesn't validate_acceptance_of break my functional tests?
在 Rails 4.2.0.rc2 上使用 Ruby 我在用户注册中添加了一个 'Accept terms of service' 复选框
在我添加的用户模型中
attr_accessor :terms_of_service
validates_acceptance_of :terms_of_service, acceptance: true
在视图中
<%= f.check_box :terms_of_service %>
最后在控制器中我将其添加到参数列表
def user_params
params.require(:user).permit(:name, :email, :password, :password_confirmation, :terms_of_service)
end
这按预期工作,但由于我对实现进行了更改,我预计相关测试会出现红色。然而,这个测试通过了,我不明白为什么:
assert_difference 'User.count', 1 do
post users_path, user: { name: "Example User",
email: "user@example.com",
password: "password",
password_confirmation: "password" }
end
我可以像这样重写我的测试
test "accept terms of service" do
get signup_path
assert_no_difference 'User.count' do
post users_path, user: { name: "Example User",
email: "user@example.com",
password: "password",
password_confirmation: "password",
terms_of_service: "0" }
end
assert_difference 'User.count', 1 do
post users_path, user: { name: "Example User",
email: "user@example.com",
password: "password",
password_confirmation: "password",
terms_of_service: "1" }
end
end
但我很好奇为什么原来的测试失败了。我从中得到的是 validates_acceptance_of 为零。
这是预期的行为吗?
简而言之,是的,nil
是允许的。我以前遇到过同样的问题。
active_model/validations/acceptance.rb
module ActiveModel
module Validations
class AcceptanceValidator < EachValidator # :nodoc:
def initialize(options)
super({ allow_nil: true, accept: "1" }.merge!(options))
setup!(options[:class])
end
# ...
end
# ...
end
# ...
end
在初始化器中,它将 allow_nil
与选项合并,所以是的,nil
(或者我应该说缺少值)是允许的。 They mention it in the Rails Guide for acceptance
,但是我错过了
这在我的测试中也让我很痛苦——当我确定他们不应该通过时,我一直在通过验证。现在我们知道为什么了!
在 Rails 4.2.0.rc2 上使用 Ruby 我在用户注册中添加了一个 'Accept terms of service' 复选框
在我添加的用户模型中
attr_accessor :terms_of_service
validates_acceptance_of :terms_of_service, acceptance: true
在视图中
<%= f.check_box :terms_of_service %>
最后在控制器中我将其添加到参数列表
def user_params
params.require(:user).permit(:name, :email, :password, :password_confirmation, :terms_of_service)
end
这按预期工作,但由于我对实现进行了更改,我预计相关测试会出现红色。然而,这个测试通过了,我不明白为什么:
assert_difference 'User.count', 1 do
post users_path, user: { name: "Example User",
email: "user@example.com",
password: "password",
password_confirmation: "password" }
end
我可以像这样重写我的测试
test "accept terms of service" do
get signup_path
assert_no_difference 'User.count' do
post users_path, user: { name: "Example User",
email: "user@example.com",
password: "password",
password_confirmation: "password",
terms_of_service: "0" }
end
assert_difference 'User.count', 1 do
post users_path, user: { name: "Example User",
email: "user@example.com",
password: "password",
password_confirmation: "password",
terms_of_service: "1" }
end
end
但我很好奇为什么原来的测试失败了。我从中得到的是 validates_acceptance_of 为零。
这是预期的行为吗?
简而言之,是的,nil
是允许的。我以前遇到过同样的问题。
active_model/validations/acceptance.rb
module ActiveModel
module Validations
class AcceptanceValidator < EachValidator # :nodoc:
def initialize(options)
super({ allow_nil: true, accept: "1" }.merge!(options))
setup!(options[:class])
end
# ...
end
# ...
end
# ...
end
在初始化器中,它将 allow_nil
与选项合并,所以是的,nil
(或者我应该说缺少值)是允许的。 They mention it in the Rails Guide for acceptance
,但是我错过了
这在我的测试中也让我很痛苦——当我确定他们不应该通过时,我一直在通过验证。现在我们知道为什么了!