私の contract_controller_spec.rb テストは、1 つを除いてすべて合格しています。
これで失敗します:
ContractsController #create redirects to show page when given valid params.
Failure/Error: expect(assigns[:contract].valid?).to be_true # factory is missing
# code_id and maybe others, check model validations
expected: true value
got: false
これが私のモデルです:
class Contract < ActiveRecord::Base
belongs_to :employee
belongs_to :client
belongs_to :primary_care_manager
belongs_to :code
has_many :schedules
attr_accessible :authorization_number, :start_date, :end_date, :client_id,
:units, :contracted_units, :frequency, :code_id, :primary_care_manager_id,
:employee_id, :employee_flag
validates_presence_of :authorization_number, :start_date, :end_date,
:units, :contracted_units, :frequency, :code_id
validates_presence_of :client_id, :primary_care_manager_id, unless: Proc.new { |a|
a.employee_flag }
validates_presence_of :employee_id, if: Proc.new { |a| a.employee_flag }
そして、失敗した私のcontracts_controller_spec.rbテストの例を次に示します。
it "#create redirects to show page when given valid params." do
contract = attributes_for(:contract)
post :create, contract: contract
expect(assigns[:contract]).to be_a Contract
expect(assigns[:contract].valid?).to be_true # factory is missing code_id and maybe
# others, check model validations
expect(response).to be_redirect
expect(response).to redirect_to contract_path(id: assigns[:contract].id)
end
最後に、これが私の factory.rb ファイルです
factory :contract do
association :employee, factory: :employee
association :client, factory: :client
association :code, factory: :code
association :primary_care_manager, factory: :primary_care_manager
sequence(:authorization_number) { |n| "20007000-#{'%03d' % n}" }
start_date Date.today
end_date Date.today.next_month
units "15 minutes"
contracted_units "20"
frequency "weekly"
employee_flag true
end
tail.test.log を確認したところ、外部キーが作成されている間、この例を実行した時点では利用できないことがわかりました。
it "#create redirects to show page when given valid params." do
コントローラーテストで上記の例を実行したときに外部キーが表示されるようにタイミングを合わせるために、このテストの書き方を理解するのを手伝ってくれませんか。
ありがとう。