2011-08-10 16:26:29 -04:00
|
|
|
describe "a stubbed instance" do
|
2017-10-20 15:20:28 -04:00
|
|
|
include FactoryBot::Syntax::Methods
|
2011-08-10 16:26:29 -04:00
|
|
|
|
|
|
|
before do
|
2018-10-07 21:45:51 -04:00
|
|
|
define_model("User")
|
2011-08-10 16:26:29 -04:00
|
|
|
|
2018-10-07 21:45:51 -04:00
|
|
|
define_model("Post", user_id: :integer) do
|
2011-08-10 16:26:29 -04:00
|
|
|
belongs_to :user
|
|
|
|
end
|
|
|
|
|
2017-10-20 15:20:28 -04:00
|
|
|
FactoryBot.define do
|
2011-08-10 16:26:29 -04:00
|
|
|
factory :user
|
|
|
|
|
|
|
|
factory :post do
|
|
|
|
user
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
subject { build_stubbed(:post) }
|
|
|
|
|
|
|
|
it "acts as if it came from the database" do
|
|
|
|
should_not be_new_record
|
|
|
|
end
|
|
|
|
|
|
|
|
it "assigns associations and acts as if it is saved" do
|
2013-01-18 13:27:57 -05:00
|
|
|
expect(subject.user).to be_kind_of(User)
|
|
|
|
expect(subject.user).not_to be_new_record
|
2011-08-10 16:26:29 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-02-17 11:29:11 -05:00
|
|
|
describe "a stubbed instance overriding strategy" do
|
2017-10-20 15:20:28 -04:00
|
|
|
include FactoryBot::Syntax::Methods
|
2011-08-10 16:26:29 -04:00
|
|
|
|
2012-03-09 15:42:28 -05:00
|
|
|
before do
|
2018-10-07 21:45:51 -04:00
|
|
|
define_model("User")
|
|
|
|
define_model("Post", user_id: :integer) do
|
2012-03-09 15:42:28 -05:00
|
|
|
belongs_to :user
|
2011-08-10 16:26:29 -04:00
|
|
|
end
|
|
|
|
|
2017-10-20 15:20:28 -04:00
|
|
|
FactoryBot.define do
|
2011-08-10 16:26:29 -04:00
|
|
|
factory :user
|
|
|
|
|
|
|
|
factory :post do
|
2012-03-09 17:20:38 -05:00
|
|
|
association(:user, strategy: :build)
|
2011-08-10 16:26:29 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-03-09 15:42:28 -05:00
|
|
|
subject { build_stubbed(:post) }
|
2012-02-17 11:29:11 -05:00
|
|
|
|
2012-03-09 15:42:28 -05:00
|
|
|
it "acts as if it is saved in the database" do
|
|
|
|
should_not be_new_record
|
2011-08-10 16:26:29 -04:00
|
|
|
end
|
|
|
|
|
2012-03-09 15:42:28 -05:00
|
|
|
it "assigns associations and acts as if it is saved" do
|
2013-01-18 13:27:57 -05:00
|
|
|
expect(subject.user).to be_kind_of(User)
|
|
|
|
expect(subject.user).not_to be_new_record
|
2012-02-17 11:29:11 -05:00
|
|
|
end
|
2011-08-10 16:26:29 -04:00
|
|
|
end
|
2019-07-12 13:32:50 -04:00
|
|
|
|
|
|
|
describe "a stubbed instance with no primary key" do
|
|
|
|
it "builds a stubbed instance" do
|
|
|
|
using_model_without_pk do
|
|
|
|
FactoryBot.define do
|
|
|
|
factory :model_without_pk
|
|
|
|
end
|
|
|
|
|
|
|
|
model = FactoryBot.build_stubbed(:model_without_pk)
|
|
|
|
expect(model).to be_truthy
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it "behaves like a persisted record" do
|
|
|
|
using_model_without_pk do
|
|
|
|
FactoryBot.define do
|
|
|
|
factory :model_without_pk
|
|
|
|
end
|
|
|
|
|
|
|
|
model = FactoryBot.build_stubbed(:model_without_pk)
|
|
|
|
expect(model).to be_persisted
|
|
|
|
expect(model).not_to be_new_record
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def using_model_without_pk
|
|
|
|
define_class("ModelWithoutPk", ActiveRecord::Base)
|
|
|
|
|
|
|
|
connection = ActiveRecord::Base.connection
|
|
|
|
begin
|
|
|
|
clear_generated_table("model_without_pks")
|
|
|
|
connection.create_table("model_without_pks", id: false) do |t|
|
|
|
|
t.column :updated_at, :datetime
|
|
|
|
end
|
|
|
|
|
|
|
|
yield
|
|
|
|
ensure
|
|
|
|
clear_generated_table("model_without_pks")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|