Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/62.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 为什么我的RSpec测试失败_Ruby On Rails_Rspec_Rspec Rails - Fatal编程技术网

Ruby on rails 为什么我的RSpec测试失败

Ruby on rails 为什么我的RSpec测试失败,ruby-on-rails,rspec,rspec-rails,Ruby On Rails,Rspec,Rspec Rails,这是我的模型: class Restaurant < ActiveRecord::Base validate :name, presence: true, uniqueness: { case_sensitive: false } end 当我运行测试时,我会失败: $ bundle exec rspec spec/models ..FF Failures: 1) Restaurant when name is not present should not be vali

这是我的模型:

class Restaurant < ActiveRecord::Base
    validate :name, presence: true, uniqueness: { case_sensitive: false }
end
当我运行测试时,我会失败:

$ bundle exec rspec spec/models
..FF

Failures:

  1) Restaurant when name is not present should not be valid
     Failure/Error: it { should_not be_valid}
       expected #<Restaurant id: nil, name: " ", created_at: nil, updated_at: nil> not to be valid
     # ./spec/models/restaurant_spec.rb:15:in `block (3 levels) in <top (required)>'

  2) Restaurant when name is already taken should not be valid
     Failure/Error: it { should_not be_valid }
       expected #<Restaurant id: nil, name: "Momofuku", created_at: nil, updated_at: nil> not to be valid
     # ./spec/models/restaurant_spec.rb:25:in `block (3 levels) in <top (required)>'

Finished in 0.01879 seconds
4 examples, 2 failures

Failed examples:

rspec ./spec/models/restaurant_spec.rb:15 # Restaurant when name is not present should not be valid
rspec ./spec/models/restaurant_spec.rb:25 # Restaurant when name is already taken should not be valid

Randomized with seed 39746
$bundle exec rspec规格/型号
..FF
失败:
1) 名称不存在的餐厅应无效
失败/错误:它{应该是无效的}
应为无效
#./spec/models/restaurant_spec.rb:15:in'block(3层)in'
2) 已使用名称的餐厅应无效
失败/错误:它{应该是无效的}
应为无效
#./spec/models/restaurant_spec.rb:25:in'block(3层)in'
以0.01879秒完成
4个示例,2个故障
失败的示例:
rspec./spec/models/restaurant_spec.rb:15#名称不存在的餐厅应无效
rspec./spec/models/restaurant_spec.rb:25#已使用名称的餐厅应无效
随机化种子39746

为什么?

使用验证而不是验证

$ bundle exec rspec spec/models
..FF

Failures:

  1) Restaurant when name is not present should not be valid
     Failure/Error: it { should_not be_valid}
       expected #<Restaurant id: nil, name: " ", created_at: nil, updated_at: nil> not to be valid
     # ./spec/models/restaurant_spec.rb:15:in `block (3 levels) in <top (required)>'

  2) Restaurant when name is already taken should not be valid
     Failure/Error: it { should_not be_valid }
       expected #<Restaurant id: nil, name: "Momofuku", created_at: nil, updated_at: nil> not to be valid
     # ./spec/models/restaurant_spec.rb:25:in `block (3 levels) in <top (required)>'

Finished in 0.01879 seconds
4 examples, 2 failures

Failed examples:

rspec ./spec/models/restaurant_spec.rb:15 # Restaurant when name is not present should not be valid
rspec ./spec/models/restaurant_spec.rb:25 # Restaurant when name is already taken should not be valid

Randomized with seed 39746