ArticleslgStudy

science

Mock trainwreck

Mock trainwreck is a science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand Mock trainwreck rather than just read about it. In short: In computer science, the term Mock Trainwreck refers to the difficulty of mocking a deeply nested model structure. Mocking is the creation of mock objects which can be used to mimic the behavior of real objects, often because it is hard to test with the real objects.

Key takeaways

  • Mock trainwreck belongs to science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect Mock trainwreck to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Mock trainwreck from memory before moving on to harder problems.

Reference excerpt

In computer science, the term Mock Trainwreck refers to the difficulty of mocking a deeply nested model structure. Mocking is the creation of mock objects which can be used to mimic the behavior of real objects, often because it is hard to test with the real objects. A trainwreck is multiple levels of method calls (called a chain), which each return objects upon which new methods can be called. Deeply nested models go against the Law of Demeter because the property's property must be accessed. The Law of Demeter, also known as the principle of least knowledge, is a design guideline to promote loose coupling of data structures that are not closely related, and thus should probably not be coupled together. In addition, this level of coupling can be considered an inappropriate intimacy code smell. Mock trainwrecks should be avoided when possible. This is because not only does it makes it harder to test the code which uses them, but also because they are harder to work with from a design standpoint. In addition, it increases the amount of information an object can access, due to its close relation with other parameters that are not related to its main functionality.

Example of a trainwreck If someone wanted to write a test looking for a library that receives public funding, or by its head librarian, he or she might use code like the following: Java

Ruby

To mock up an object that matches the search result, they would have to have mocking code like what follows: Java

Ruby This is an example of a mock trainwreck, because it is a mock up of two unrelated objects, but it relies on a class, Library, to point to them both.

Ways to avoid the trainwreck A mock trainwreck can be avoided by making general code changes or by more specific changes through the use of dependency injection and libraries. General code changes allow a nested model to be made more simple, primarily though the creation of an assessor to access the sub property. This prevents the deep nesting which causes the mock trainwreck, and this assessor can be mocked easily.

Dependency injection Dependency injection (DI), the process by which a dependency is passed to the client which will use it, can be used to soften the trainwreck. One method of DI that is easy to use is a location object to reduce the complexity of building the mock object. Below is the example above reworked with a DI build_mock method to help set mock values on dependent objects. While for this simple example it doesn't appear to help much, in a more complicated scenario it could reduce complexity in manually setting the values.

Libraries

Mockito Testing library in various languages can make the mock trainwreck easier to navigate with helpers. An example is Mockito that provides annotations to assist in injecting mocks into objects. Using the @InjectMocks annotation and @Mock annotation, when Mockito initializes all the mocks it will inject Library with the mocks for funding and head librarian.

Demeter The use of libraries can also be used to address mock trainwreck. One such library is called demeter, and it can be used to provide Law of Demeter duck typing assessors that automatically creates assessors for single level nested models. By using this library, a person can mock the assessors that their code uses of the child function, as seen in the example below.

References

Worked examples

Example 1 — a first encounter with Mock trainwreck

Start with the simplest possible case. Write down what Mock trainwreck claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to Mock trainwreck before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about Mock trainwreck ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of Mock trainwreck

In research
Mock trainwreck appears in science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses Mock trainwreck in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
Mock trainwreck is common in secondary-school and first-year university syllabi. It links to neighbouring topics Anti-patterns, Metaphors referring to transport, Unit testing, so understanding it makes those chapters shorter.
In everyday life
Look for Mock trainwreck outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.
Ask Teacher Smith questions about this articleOpens your AI tutor with a question about “Mock trainwreck” →

How to study Mock trainwreck in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Mock trainwreck means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain Mock trainwreck out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Mock trainwreck in simple terms?

In computer science, the term Mock Trainwreck refers to the difficulty of mocking a deeply nested model structure. Mocking is the creation of mock objects which can be used to mimic the behavior of real objects, often because it is hard to test with the real objects.

Why does Mock trainwreck matter?

Because it connects several science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study Mock trainwreck?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on Mock trainwreck.

Tags

  • Anti-patterns
  • Metaphors referring to transport
  • Unit testing

Keep exploring