为什么迷你测试用例失败了?
Why minitest case are failing?
我的模型class:
class Post < ActiveRecord::Base
validates_presence_of :value
...
end
我的post.yml
one:
row: 2
col: 3
name: 'Test'
我的测试用例
require 'test_helper'
class SimulationsModelTest < ActiveSupport::TestCase
def setup
@post = Post.new
end
test 'post must have name' do
@post.name = ' '
assert @post.valid?
end
结束
为什么上面的测试用例都失败了?然而,以下测试用例正在通过
test 'post must have name' do
@post.name = ' '
assert_not @post.valid?
结束
在我的模型中,我没有为名称设置 presence => true
,那为什么第一个测试用例失败了?
编辑-1
如果我有两个 validates_presence of : value , :name
怎么办?我的测试用例是:
test 'post must have name' do
@post.name = 'some'
@post.value = 'test'
assert @post.valid?
end
它必须通过测试用例,但它失败了。我在 models
中没有任何其他必需参数
因为 @post
只有当它有一个 value
时才有效,而在你的测试中你只分配一个 name
给它。
在您的模型中:
validates_presence_of :value
您没有分配 value
。你不会在任何地方做 @post.value = 'some_value'
。
编辑:
前面的:
表示它是一个代表属性名的符号。您可以通过这种方式为多个字段定义验证:
validates_presence_of :value, :name
我的模型class:
class Post < ActiveRecord::Base
validates_presence_of :value
...
end
我的post.yml
one:
row: 2
col: 3
name: 'Test'
我的测试用例
require 'test_helper'
class SimulationsModelTest < ActiveSupport::TestCase
def setup
@post = Post.new
end
test 'post must have name' do
@post.name = ' '
assert @post.valid?
end
结束
为什么上面的测试用例都失败了?然而,以下测试用例正在通过
test 'post must have name' do
@post.name = ' '
assert_not @post.valid?
结束
在我的模型中,我没有为名称设置 presence => true
,那为什么第一个测试用例失败了?
编辑-1
如果我有两个 validates_presence of : value , :name
怎么办?我的测试用例是:
test 'post must have name' do
@post.name = 'some'
@post.value = 'test'
assert @post.valid?
end
它必须通过测试用例,但它失败了。我在 models
因为 @post
只有当它有一个 value
时才有效,而在你的测试中你只分配一个 name
给它。
在您的模型中:
validates_presence_of :value
您没有分配 value
。你不会在任何地方做 @post.value = 'some_value'
。
编辑:
前面的:
表示它是一个代表属性名的符号。您可以通过这种方式为多个字段定义验证:
validates_presence_of :value, :name