Spec:
require 'spec_helper'
describe CameraReport do
let(:call_sheet) { create(:call_sheet) }
let(:camera_report) { create(:camera_report, call_sheet: call_sheet) }
subject { camera_report }
describe "associations" do
it { should belong_to(:call_sheet) }
it { should have_many(:camera_logs).dependent(:destroy) }
end
describe "validations" do
it { should_not allow_mass_assignment_of(:call_sheet_id) }
it { should validate_presence_of(:date) }
end
end
Factory:
FactoryGirl.define do
factory :call_sheet do
company_name "company"
association :draft
association :stage
association :category
end
factory :camera_report do
shoot_day 1
association :call_sheet
end
end
这个测试 花费了 20 多秒,如果把 call_sheet
换成 let(:call_sheet) { build_stubbed(:call_sheet) }
数据库会报错,因为有 foreign_key call_sheet_id
如果数据库中没有 foreign_key,就可以把 call_sheet
用 stubbed 代替,测试时间大概 0.x 秒
问题是 我应该保留数据库中的 foreign_key 么?还是我测试的写法有问题?怎样做可以使测试不至于如此慢?