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
|