我有一个名为ticket的模型如下:
ActiveRecord::Schema.define(version: 20151231072055) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
create_table "tickets", force: :cascade do |t|
t.string "movie_name"
t.integer "price"
t.boolean "is_3d"
t.string "theatre_name"
t.string "seller_name"
t.string "seller_email"
t.string "seller_mobile"
t.string "preferred_places"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.datetime "date_and_time"
end
end
我试图验证所有字段的存在,因此我对我的票证模型进行了以下测试:
require 'test_helper'
class TicketTest < ActiveSupport::TestCase
def setup
@ticket = tickets(:one)
end
def teardown
@ticket = nil
end
test 'should not save ticket without movie name' do
@ticket.movie_name = nil
assert_not(@ticket.save, 'Saved the ticket without movie name')
end
test 'should not save ticket without price' do
@ticket.price = nil
assert_not(@ticket.save, 'Saved the ticket without price')
end
test 'should not save ticket without theatre name' do
@ticket.theatre_name = nil
assert_not(@ticket.save, 'Saved the ticket without theatre name')
end
test 'should not save ticket without seller name' do
@ticket.seller_name = nil
assert_not(@ticket.save, 'Saved the ticket without seller name')
end
test 'should not save ticket without seller email' do
@ticket.seller_email = nil
assert_not(@ticket.save, 'Saved the ticket without seller email')
end
test 'should not save ticket without seller mobile' do
@ticket.seller_mobile = nil
assert_not(@ticket.save, 'Saved the ticket without seller mobile')
end
test 'should not save ticket without preferred places' do
@ticket.preferred_places = nil
assert_not(@ticket.save, 'Saved the ticket without preferred places')
end
test 'should not save ticket without date and time' do
@ticket.date_and_time = nil
assert_not(@ticket.save, 'Saved the ticket without date and time')
end
test 'should not save ticket without 3D status' do
@ticket.is_3d = nil
assert_not(@ticket.save, 'Saved the ticket without 3D status')
end
end
我在我的故障单模型中编写了以下代码,以便通过测试:
class Ticket < ActiveRecord::Base
validates_presence_of :is_3d
end
这使得所有测试用例都能通过。我一直注意到的是,如果我单独检查is_3d
的存在,一切都在通过(异常),如果我单独检查是否存在任何其他字段,那么单独对该特定字段的测试正在通过(这是正常的)。