2017-10-20 15:20:28 -04:00
|
|
|
describe FactoryBot::Attribute::Association do
|
2020-06-05 15:15:18 -04:00
|
|
|
let(:name) { :author }
|
|
|
|
let(:factory) { :user }
|
|
|
|
let(:overrides) { {first_name: "John"} }
|
2017-06-01 12:54:02 -04:00
|
|
|
let(:association) { double("association") }
|
2009-04-11 11:27:23 -04:00
|
|
|
|
2017-10-20 15:20:28 -04:00
|
|
|
subject { FactoryBot::Attribute::Association.new(name, factory, overrides) }
|
2017-07-14 11:45:59 -04:00
|
|
|
|
|
|
|
before do
|
|
|
|
# Define an '#association' instance method allowing it to be mocked.
|
2022-06-08 07:01:10 -04:00
|
|
|
# Usually this is determined via '#method_missing'
|
2021-02-20 16:55:25 -05:00
|
|
|
missing_methods = Module.new {
|
|
|
|
def association(*args)
|
|
|
|
end
|
|
|
|
}
|
|
|
|
subject.extend(missing_methods)
|
2017-07-14 11:45:59 -04:00
|
|
|
|
2020-06-05 15:15:18 -04:00
|
|
|
allow(subject)
|
|
|
|
.to receive(:association).with(any_args).and_return association
|
2017-06-01 12:54:02 -04:00
|
|
|
end
|
2009-04-11 11:27:23 -04:00
|
|
|
|
2020-06-05 15:15:18 -04:00
|
|
|
it { should be_association }
|
2013-01-18 13:27:57 -05:00
|
|
|
its(:name) { should eq name }
|
2011-05-19 10:56:45 -04:00
|
|
|
|
2011-11-22 18:01:01 -05:00
|
|
|
it "builds the association when calling the proc" do
|
2013-01-18 13:27:57 -05:00
|
|
|
expect(subject.to_proc.call).to eq association
|
2011-11-22 18:01:01 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it "builds the association when calling the proc" do
|
2011-11-29 15:20:08 -05:00
|
|
|
subject.to_proc.call
|
2013-01-18 13:27:57 -05:00
|
|
|
expect(subject).to have_received(:association).with(factory, overrides)
|
2009-04-11 11:27:23 -04:00
|
|
|
end
|
2011-08-13 01:03:12 -04:00
|
|
|
end
|
2009-04-11 11:27:23 -04:00
|
|
|
|
2017-10-20 15:20:28 -04:00
|
|
|
describe FactoryBot::Attribute::Association, "with a string name" do
|
2020-06-05 15:15:18 -04:00
|
|
|
subject { FactoryBot::Attribute::Association.new("name", :user, {}) }
|
2013-01-18 13:27:57 -05:00
|
|
|
its(:name) { should eq :name }
|
2009-04-11 11:27:23 -04:00
|
|
|
end
|