Testing Private Attribute Handling in factory_bot
This test suite examines FactoryBot’s handling of private attributes in Ruby classes. It specifically verifies that attempting to set private attributes through factory definitions raises appropriate errors, ensuring proper encapsulation is maintained.
Test Coverage Overview
Implementation Analysis
Technical Details
Best Practices Demonstrated
thoughtbot/factory_bot
spec/acceptance/private_attributes_spec.rb
describe "setting private attributes" do
it "raises a NoMethodError" do
define_class("User") do
private
attr_accessor :foo
end
FactoryBot.define do
factory :user do
foo { 123 }
end
end
expect {
FactoryBot.build(:user)
}.to raise_error NoMethodError, /foo=/
end
end