1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
|
if RUBY_ENGINE != "truffleruby"
require_relative "attributes_for_destructuring"
end
describe "a generated attributes hash" do
include FactoryBot::Syntax::Methods
before do
define_model("User")
define_model("Comment")
define_model("Post", title: :string,
body: :string,
summary: :string,
user_id: :integer) do
belongs_to :user
has_many :comments
end
FactoryBot.define do
factory :user
factory :comment
factory :post do
title { "default title" }
body { "default body" }
summary { title }
user
comments do |c|
[c.association(:comment)]
end
end
end
end
subject { attributes_for(:post, title: "overridden title") }
it "assigns an overridden value" do
expect(subject[:title]).to eq "overridden title"
end
it "assigns a default value" do
expect(subject[:body]).to eq "default body"
end
it "assigns a lazy, dependent attribute" do
expect(subject[:summary]).to eq "overridden title"
end
it "doesn't assign associations" do
expect(subject).not_to have_key(:user_id)
expect(subject).not_to have_key(:user)
end
end
describe "calling `attributes_for` with a block" do
include FactoryBot::Syntax::Methods
before do
define_model("Company", name: :string)
FactoryBot.define do
factory :company
end
end
it "passes the hash of attributes" do
attributes_for(:company, name: "thoughtbot") do |attributes|
expect(attributes[:name]).to eq("thoughtbot")
end
end
it "returns the hash of attributes" do
expected = nil
result = attributes_for(:company) { |attributes|
expected = attributes
"hello!"
}
expect(result).to eq expected
end
end
describe "`attributes_for` for a class whose constructor has required params" do
before do
define_model("User", name: :string) do
def initialize(arg1, arg2)
# Constructor requesting params to be used for testing
end
end
FactoryBot.define do
factory :user do
name { "John Doe" }
end
end
end
subject { FactoryBot.attributes_for(:user) }
its([:name]) { should eq "John Doe" }
end
|