Mocks in the project #132
Aurelien9Code
announced in
Announcements
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Handling circular dependency
If you check the implementation of
MockEvent
,MockUser
, andMockAssociation
, you will see that:MockEvent
implements mock data structures related to event details, including a list ofMockUser
instances as participants and organizers, and references toMockAssociation
instances for tagged associations.MockUser
implements user-related mock data, such as their joined and savedMockEvent
instances, as well as their associatedMockAssociation
instances.MockAssociation
implements association mock data, which includes a list ofMockUser
instances as members and references to the events they organize or tag.These interconnected references can lead to a situation known as circular dependency, where creating an instance of one mock requires the instantiation of others, potentially leading to an infinite loop or stack overflow during test execution.
Example :
MockUser.createMockUser()
is called. This function, by default, generatesfollowedAssociations
andjoinedAssociations
usingMockAssociation.createAllMockAssociations()
.MockAssociation.createAllMockAssociations()
is executed, it callsMockAssociation.createMockAssociation()
, which includes a list of members created by callingMockUser.createAllMockUsers()
.MockEvent.createAllMockEvents()
is called, it might include users (as organizers or participants) and tagged associations created byMockAssociation
.Because each mock class depends on others to generate its full structure, calling one mock generation function can indirectly trigger the instantiation of others, creating a loop:
MockUser.createMockUser()
→ callsMockAssociation.createAllMockAssociations()
MockAssociation.createMockAssociation()
→ callsMockUser.createAllMockUsers()
MockUser.createMockUser()
callsMockEvent.createAllMockEvents()
, which includesMockAssociation
referencesThis cycle can keep repeating, leading to an infinite loop or a stack overflow error when trying to create the mock data.
To prevent circular dependency:
Conditional flags such as
associationDependency
andeventDependency
are used in the mock creation methods. For example,MockUser.createMockUser()
uses these flags to decide whether to create actual associations or events or to leave them as empty lists.By allowing references to be left out or simplified when dependencies are true, the data generation avoids deep nesting loops.
Beta Was this translation helpful? Give feedback.
All reactions