---
title: "Controlling Time"
url: "https://courses.osherove.com/courses/tdd-in-net-in-hebrew/controlling-time.md"
canonical: "https://courses.osherove.com/courses/tdd-in-net-in-hebrew/controlling-time/"
tags: []
author: "Roy Osherove"
description: "Lesson 13 of 22 in TDD in .NET in Hebrew (Day 2 - Legacy Code Tools), 6:44. Free video by Roy Osherove."
usage: "Licensed under CC BY 4.0 — reuse, quote and adapt with attribution (Roy Osherove, link to the original page)."
license: "https://creativecommons.org/licenses/by/4.0/"
---

# Controlling Time

1. [Courses](https://courses.osherove.com/#courses)
2. [TDD in .NET in Hebrew](https://courses.osherove.com/courses/tdd-in-net-in-hebrew/)
3. Day 2 - Legacy Code Tools

Lesson 13 of 22

Duration: 6:44 [Watch on YouTube](https://www.youtube.com/watch?v=cmaydKpLtJ4) Spoken in Hebrew

## In this lesson

Roy Osherove applies [extract and override](https://livebook.manning.com/book/the-art-of-unit-testing-second-edition/chapter-3#ch03lev1sec5 "Variations on refactoring techniques (The Art of Unit Testing, 2nd ed., ch. 3)") to the machine name as well, then explains why it does not suit time, which is used throughout a system. Instead he adds an API to a [central system time class](https://livebook.manning.com/book/the-art-of-unit-testing-second-edition/chapter-7#ch07lev1sec5 "Cross-cutting concerns injection (The Art of Unit Testing, 2nd ed., ch. 7)") so tests can set the current time, and resets it after each test because the change is static and affects other tests. He argues that a static class is not bad design in itself, suggests making the setter [internal and visible to the test assembly](https://livebook.manning.com/book/the-art-of-unit-testing-second-edition/chapter-3#ch03lev1sec6 "Overcoming the encapsulation problem (The Art of Unit Testing, 2nd ed., ch. 3)"), and sends the group to implement it in the exercises.

## Transcript

683 words

[\[00:04\]](https://www.youtube.com/watch?v=cmaydKpLtJ4&t=4s) אני רוצה קצת לדבר על הנושא של איך להכניס זמן ולשלוס לו זמן בטestטים שלנו כי דלת בבקשה >> אבל קודם כל אני אני אעבוד על הנושא של הכל ובסרביes אני קצת אראה את הכיוון קודם כל עשיתי אקסטרctט ועכשיו אני אעשה אקסטר גם למשינר שמת לב דרך אגב ברגע שעשיתם אקסטר פעם אחת כל שני נהיה די פשוט ברגע שהפטרן מובן אז פתאום אוקיי יאללה משינה עם עודual מט לא כזה נורא אז זה מטיק לא נורא מטו כן

[\[00:44\]](https://www.youtube.com/watch?v=cmaydKpLtJ4&t=44s) אולי נעשה גט משימה name ככה שאני יכול לקצג את הlogin maner זה חשוב מאוד

[\[01:17\]](https://www.youtube.com/watch?v=cmaydKpLtJ4&t=77s) Verion אף פעם לא עושה בשום דבר לדוגמה אני רוצה להגיד name will אוקיי עכשיו יכולתי גם את אותו דבר לעשות עם מזמן create field לעשות פה

[\[01:50\]](https://www.youtube.com/watch?v=cmaydKpLtJ4&t=110s) שם יותר ש אוקיי אבל מה לגבי זמן לגבי זמן הנושא של אקסט לא כל כך מתאים כי זמן כמשהו שמשתמשים בו במערכת משתמשים בו בצורה רוכבית אז לעשות אקסטרד עכשיו בכל הקלאסים שמשתמשים בזמן יכול לקחת הרבה זמן אז במקום זה מה שנעשה זה נחשוב על פאטרן אחר.

[\[02:21\]](https://www.youtube.com/watch?v=cmaydKpLtJ4&t=141s) אחד הדברים הכי בסיסיים שיש לכם כבר היום בדיזיין זה קלאס שנקרא time אז מה שהייתי רוצה לעשות בטסט שלי זה לכתוב ככה time אליי אני קורא לזה syיסטם time אצלכם זה יקרה time set new time

[\[02:57\]](https://www.youtube.com/watch?v=cmaydKpLtJ4&t=177s) ובעצם מה שאני עושה פה אני מוסיף API מיוחד לטיים שלי במיוחד כדי שבטestטים שלי אני אוכל לשלוט בזמן מאותו רגע כל מי שמש בתים יהיה לו את הזמן שאני קבעתי אבל יש פה סכנה כי מן הסתם אני משפיע על טestטים אחרים אז כשאני משנה משהו במשהו ברמה סטטית אז אני צריך לעשות לו אנדו אחרי כל ‏public after test

[\[03:28\]](https://www.youtube.com/watch?v=cmaydKpLtJ4&t=208s) לשמור שט כל מה שאני צריך זה להגיד syסם time reset ומאותו רגע הוא יחזור להחזיר את הD time now בדיוק כמו שהוא עשה קודם כל מה שיש לו זה איזשהו פלי עכשיו אם אני באמת מפחד שיגעו בזה וזה משהו שאני יכול לראות אולי למ שיפדו לגעת בו לעשות הנג אז אז אני את זה אני באמת עושה internal ו משתמש ב-InternalsVisibleTo לtest assembly שלי אז תשימו לב שה הפטר פה הוא שאני בכלל לא עושה inject שום dependency.

[\[03:59\]](https://www.youtube.com/watch?v=cmaydKpLtJ4&t=239s) כל מה שאני עושה זה אני הולך למקום שבו גם ככה יש לי מקום מרכזי שממנו אני לוקח את ה-tיים דווקא בגלל שזה משהו כל כך מרכזי במערכת אז זה שזה סטטי לא אומר שזה דיזיין גרוע שימו לב אני פשוט הופך את הדיזיין לtestable ועל ידי זה שאני מוסיף איזשהו setר ורסט רק חשוב לא לשכוח לעשות את הטרדאן הזה בסוף כל אחד מהם שאלות את הסט >> מה >> איך מישמים את הסט >> את הסט זה יש מיליון דרכים ליישם את זה.

[\[04:31\]](https://www.youtube.com/watch?v=cmaydKpLtJ4&t=271s) זה יכול להיות כן בהנחה שהדבר הזה היה בפרודקשן זה יכול להיות ברמה של פילד שהוא נלב זה יכול להיות ברמה של איזשהו פלאג כן [fake](https://livebook.manning.com/book/the-art-of-unit-testing-second-edition/chapter-4#ch04lev1sec2 "The difference between mocks and stubs (The Art of Unit Testing, 2nd ed., ch. 4)") time שווה day time ומה אותו רגע

[\[05:04\]](https://www.youtube.com/watch?v=cmaydKpLtJ4&t=304s) כן כן אפשר להגיד ישפ אפשר גם לעשות אותו ואז לבדוק אם הוא לא נעל ואז להחזיר את הטיים בדוק זה משנה העיקר שזה יעבוד משתמשים בקלאס הזה במקום כאילו בד time >> אצלך כבר יש את זה >> יש לכם קלאס שנקרא אתם לא צריכים לסוריס במקום אתם פשוט מוסיפים set לטלק מבחרים במ >> אפשר אולי לעשות אפילו כדי לאחוב את זה שאף אחד לא ישכח את ה אבל using דורש instance כלשהו כדי שתוכל לעשות לו dispose time by default הוא סוג שלו להמציא אסנס לצורך זה להמציא אסance ואיך והאינס הזה יעשה setט לזה ולעשות

[\[05:43\]](https://www.youtube.com/watch?v=cmaydKpLtJ4&t=343s) אחלה מצוין אין שום בעיה מה שחשוב זה שהכדי לשלוט במשהו מרכזי אז פשוט מוסיפים למרכזי הזה יכולת להחליף את המידע שלו זה לא כזה נורא להפך זה אפילו מומלץ אם גם ככה כל המערכת משתמשת בזה אז אם אם לא כל המערכת משתמשת לזה אז מאותו רגע צריך לוודא שבכל המערכת בכל מקום שיוצים די פשוט משתמשים בטיים בסדר עוד משהו כשקוד רביו מאוד יכול לעזור פה דרך אגב אוקיי אני ממליץ בחום

[\[06:14\]](https://www.youtube.com/watch?v=cmaydKpLtJ4&t=374s) אה שאלות לגבי זה אוקיי אמ אז מה שאני רוצה זה שבהתאם לטכניקה הזאת לכו תלמשו את את ה את הדרישה האחרונה של די טיים באמצעות הטכניקה הזאת תיצרו אצלכם את הקסטר הזה ואז תמשיכו גם לתרגילים של הogin manager with future בסדר

## Related

- [Cross-cutting concerns injection](https://livebook.manning.com/book/the-art-of-unit-testing-second-edition/chapter-7#ch07lev1sec5) · The Art of Unit Testing, 2nd ed., ch. 7

    Cross-cutting concerns injection, the settable central system-time approach the lesson builds for time used everywhere.

- [Stubbing out time with parameter injection](https://livebook.manning.com/book/the-art-of-unit-testing-third-edition/chapter-3#sigil_toc_id_64) · The Art of Unit Testing, 3rd ed., ch. 3

    Stubbing out time with parameter injection, another way to control the current time in tests.

### Also linked in this lesson

#### The Art of Unit Testing, 2nd ed.

- [Variations on refactoring techniques](https://livebook.manning.com/book/the-art-of-unit-testing-second-edition/chapter-3#ch03lev1sec5), ch. 3
- [Overcoming the encapsulation problem](https://livebook.manning.com/book/the-art-of-unit-testing-second-edition/chapter-3#ch03lev1sec6), ch. 3
- [The difference between mocks and stubs](https://livebook.manning.com/book/the-art-of-unit-testing-second-edition/chapter-4#ch04lev1sec2), ch. 4

[Previous lesson Legacy Refactoring intro](https://courses.osherove.com/courses/tdd-in-net-in-hebrew/legacy-refactoring-intro/) [Next lesson Working with Typemock Isolator](https://courses.osherove.com/courses/tdd-in-net-in-hebrew/working-with-typemock-isolator/)


---

## About the author

**Roy Osherove**: Senior Solutions Architect at AWS working on AI for production and AI-driven development; author of The Art of Unit Testing and Elastic Leadership, with about 20 years of consulting and training on unit testing, TDD, team leadership and continuous delivery.

### Sites

- [RobotPaper](https://robotpaper.ai/): This blog: AI patterns and anti-patterns, AI and coding agents, and engineering leadership.
- [osherove.com](https://osherove.com): Roy's personal site. Author of The Art of Unit Testing and Elastic Leadership; consulting and training on unit testing, TDD, team leadership and continuous delivery, plus about 20 years of blog posts.
- [The Art of Unit Testing](https://www.artofunittesting.com): The Art of Unit Testing, 3rd Edition (Manning): readable, maintainable, trustworthy unit tests, plus TDD workshops in C#, JavaScript, Java and Kotlin.
- [5whys.com](https://www.5whys.com): Roy's writing on software team leadership: self-organizing teams, surviving chaos, learning mode and the Elastic Leadership model.
- [Pipeline Driven](https://pipelinedriven.org): Continuous delivery and DevOps practices: build patterns, pipeline-friendly testing, DevOps metrics and enterprise DevOps.
- [Elastic Leadership](https://www.elasticleadership.com): Elastic Leadership, Roy's framework for software team leaders (survival, learning and self-organizing modes).

### Social

- X: https://x.com/royosherove
- LinkedIn: https://www.linkedin.com/in/osherove/

### Contact

- Contact form: https://forms.gle/jQSx5KoXvrVA1JCK7

### License

Licensed under CC BY 4.0 — reuse, quote and adapt with attribution (Roy Osherove, link to the original page). License: https://creativecommons.org/licenses/by/4.0/
