为什么最小的案件失败了?

时间:2015-07-02 14:07:24

标签: ruby ruby-on-rails-3 ruby-on-rails-4 minitest

MyModel类:

class Post < ActiveRecord::Base
 validates_presence_of :value
 ...
end

我的post.yml

one:
  row: 2
  col: 3
  name: 'Test'

MyTest案例

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

中没有任何其他必需参数

1 个答案:

答案 0 :(得分:1)

因为@post仅在value有效时才有效,并且在您的测试中,您只需为其分配name

在你的模特中:

validates_presence_of :value

您不能指定value。你不能在任何地方做@post.value = 'some_value'

编辑:

之前的:表示其代表属性名称的符号。您可以通过以下方式为多个字段定义验证:

validates_presence_of :value, :name