rails rspec 代码`to_not be_valid` 是什么意思?
what's the mean about the rails rspec code`to_not be_valid`?
当我在 rails 上使用 ruby 来学习如何使用 rails rspec
测试代码时,有些代码我无法理解,这里是代码,它只是一个关于停车计时器测试的代码。
db/migrate/xxxxx_create_parking.rb
class CreateParkings < ActiveRecord::Migration[5.0]
def change
create_table :parkings do |t|
t.string :parking_type #guest, short-term, long-term
t.datetime :start_at
t.datetime :end_at
t.integer :amount
t.integer :user_id, index: true
app/spec/models/parking_spec.rb
require 'rails_helper'
RSpec.describe Parking, type: :model do
#pending "add some examples to (or delete) #{__FILE__}"
describe ".validate_end_at_with_amount" do
it "is invalid without amount" do
parking = Parking.new( :parking_type => "guest",
:start_at => Time.now - 6.hours,
:end_at => Time.now)
expect(parking).to_not be_valid
end
it "is invalid without end_at" do
parking = Parking.new( :parking_type => "guest",
:start_at => Time.now - 6.hours,
:amount => 999)
expect(parking).to_not be_valid
end
end
end
那么,to_not be_valid
和 t.integer :user_id, index: true
的意思是什么?谢谢。
对于be_valid
它正在测试新记录的ActiveModel::Validations
,它像valid?
函数一样工作,通过输入not_to
你的意思是当我插入这条记录时parking = Parking.new( :parking_type => "guest",:start_at => Time.now - 6.hours,:amount => 999)
没有 end_at
它不应该工作,通过这个你检查你的代码在无效情况下是否正常工作,这是一个关于 be_valid.
的好资源
对于第二部分 t.integer :user_id
,这是 ActiveRecord
中的一个命令,它在 table 中添加了一个新的 column/field,名为 user_id
并且
index: true
在其上放置一个索引(这是数据库世界中的一个概念,有助于在更短的时间内检索记录),user_id
通常用作外键 index。
当我在 rails 上使用 ruby 来学习如何使用 rails rspec
测试代码时,有些代码我无法理解,这里是代码,它只是一个关于停车计时器测试的代码。
db/migrate/xxxxx_create_parking.rb
class CreateParkings < ActiveRecord::Migration[5.0]
def change
create_table :parkings do |t|
t.string :parking_type #guest, short-term, long-term
t.datetime :start_at
t.datetime :end_at
t.integer :amount
t.integer :user_id, index: true
app/spec/models/parking_spec.rb
require 'rails_helper'
RSpec.describe Parking, type: :model do
#pending "add some examples to (or delete) #{__FILE__}"
describe ".validate_end_at_with_amount" do
it "is invalid without amount" do
parking = Parking.new( :parking_type => "guest",
:start_at => Time.now - 6.hours,
:end_at => Time.now)
expect(parking).to_not be_valid
end
it "is invalid without end_at" do
parking = Parking.new( :parking_type => "guest",
:start_at => Time.now - 6.hours,
:amount => 999)
expect(parking).to_not be_valid
end
end
end
那么,to_not be_valid
和 t.integer :user_id, index: true
的意思是什么?谢谢。
对于be_valid
它正在测试新记录的ActiveModel::Validations
,它像valid?
函数一样工作,通过输入not_to
你的意思是当我插入这条记录时parking = Parking.new( :parking_type => "guest",:start_at => Time.now - 6.hours,:amount => 999)
没有 end_at
它不应该工作,通过这个你检查你的代码在无效情况下是否正常工作,这是一个关于 be_valid.
对于第二部分 t.integer :user_id
,这是 ActiveRecord
中的一个命令,它在 table 中添加了一个新的 column/field,名为 user_id
并且
index: true
在其上放置一个索引(这是数据库世界中的一个概念,有助于在更短的时间内检索记录),user_id
通常用作外键 index。