【发布时间】:2021-02-21 11:26:03
【问题描述】:
我是编程新手,尝试编写测试但出现错误:
员工:
belongs_to :contact
belongs_to :account
帐户:
has_many :staff
联系
has_many :staff
我试着写
subject(:service) { SomeService.instance }
let(:room) { create(:chat) }
let(:account) { create(:account) }
let(:contact) { create(:contact) }
let(:consultant) { create(:staff, account: account).contact}
# or i try to write something like this
let(:consultant) { create(:staff, account_id: account.id, contact_id: contact.id)}
context do
subject { service.call(account: account, consultant: consultant)}
it { is_expected.to respond_to(:call).with_keywords(:account, :consultant)}
end
错误:
consultant = account.staff.find_by!(contact_id: consultant.id)
ActiveRecord::RecordNotFound:
Couldn't find Staff with [WHERE "staff"."account_id" = $1 AND "staff"."contact_id" = $2]
我认为我不了解一些基本的东西。请您给我一些建议好吗?
【问题讨论】:
-
抱歉,我想我解决了这个问题:let(:consultant) { create(:staff, account: room.account).contact}
标签: ruby-on-rails rspec