Skip to content

Fix embedded factory build and chaos mode support - #66

Merged
BillyRuffian merged 2 commits into
BillyRuffian:masterfrom
mjrisaac:bugfix/embedded-factory-support
Jul 3, 2026
Merged

Fix embedded factory build and chaos mode support#66
BillyRuffian merged 2 commits into
BillyRuffian:masterfrom
mjrisaac:bugfix/embedded-factory-support

Conversation

@mjrisaac

@mjrisaac mjrisaac commented Jul 3, 2026

Copy link
Copy Markdown

Bug fixes in factory.rb for manufacture_from_embedded_factory and assert_only_known_and_optional_attributes_for_chaos methods


  1. Fix NoMethodError when building factories with multiple embedded factories

manufacture_from_embedded_factory called f.attributes.map(&:name) on embedded factories. When an embedded factory itself contains embedded factories .attributes returns a mix of Attribute objects and Hash objects. This caused NoMethodError: undefined method 'name' for an instance of Hash.

Fix: Use f.attributes(include_embeddings: false).map(&:name) which guarantees only Attribute objects are returned.


  1. Fix NoSuchAttributeError when using chaos mode with embedded factory attributes

assert_only_known_and_optional_attributes_for_chaos didn't recognise embedded factory attribute names as valid because they are stored inside Hashes rather than as plain symbols in attribute_names.

Fix: Extract attribute names from Hashes before comparison which matches the approach in assert_only_known_attributes_for_override.


  1. Fix chaos attribute names leaking to child factories

manufacture_from_embedded_factory passed the chaos: argument directly to embedded factory builds. When chaos was an array of attribute names (e.g. chaos: [:child_obj]), those parent-specific names were passed to the child factory, which then failed validation because it doesn't have attributes with those names.

Fix: Convert chaos to a boolean before passing to embedded factories, so children inherit random chaos behaviour without receiving attribute names that are only relevant to the parent.


Added RSpec test coverage of:

  1. Building a factory with multiple embedded factories
  2. Chaos mode with embedded factories
  3. Chaos mode targeting an embedded factory attribute by name
  4. Verifying chaos attribute names don't leak to child factories

Mark Isaac added 2 commits July 3, 2026 17:00
… validation for embedded attributes --fix: chaos attribute leaking to child factories
@BillyRuffian
BillyRuffian marked this pull request as ready for review July 3, 2026 17:30
@BillyRuffian
BillyRuffian merged commit 518c457 into BillyRuffian:master Jul 3, 2026
2 checks passed
@mjrisaac
mjrisaac deleted the bugfix/embedded-factory-support branch July 6, 2026 14:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants