<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Mathieu Larouche Dubé on Medium]]></title>
        <description><![CDATA[Stories by Mathieu Larouche Dubé on Medium]]></description>
        <link>https://medium.com/@matdube?source=rss-365edb235e02------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*DFmmnSDBBiuf9kxTxbahoQ.jpeg</url>
            <title>Stories by Mathieu Larouche Dubé on Medium</title>
            <link>https://medium.com/@matdube?source=rss-365edb235e02------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 04 Dec 2026 06:23:29 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@matdube/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Multiple environments with Angular and Docker]]></title>
            <link>https://medium.com/voobans-tech-stories/multiple-environments-with-angular-and-docker-2512e342ab5a?source=rss-365edb235e02------2</link>
            <guid isPermaLink="false">https://medium.com/p/2512e342ab5a</guid>
            <category><![CDATA[docker]]></category>
            <category><![CDATA[angularjs]]></category>
            <dc:creator><![CDATA[Mathieu Larouche Dubé]]></dc:creator>
            <pubDate>Tue, 05 Dec 2017 19:00:51 GMT</pubDate>
            <atom:updated>2018-06-05T19:47:45.518Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*Bi3SiXWalc_rI-g7" /><figcaption>“Rows of storage bins, the three on the top being red and the bottom two blue” by <a href="https://unsplash.com/@luke92?utm_source=medium&amp;utm_medium=referral">Luke Palmer</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>In a recent project, we faced a challenge when trying to manage multiple environment settings in an Angular application running in a Docker container. This blog post is about how we tackled this problem.</p><h4><strong>The problem</strong></h4><p>Our application is a single page application with a .Net Core back-end API. One setting that has to change from environment to environment is the back-end API url.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2e6c502123505f05cec944894feedc41/href">https://medium.com/media/2e6c502123505f05cec944894feedc41/href</a></iframe><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/77f91a045fb83393d14dcc0c696b08b6/href">https://medium.com/media/77f91a045fb83393d14dcc0c696b08b6/href</a></iframe><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/9fc09f89b0b42826223614729c322bf8/href">https://medium.com/media/9fc09f89b0b42826223614729c322bf8/href</a></iframe><p>Angular apps are typically built using angular-cli, passing the environment like so:</p><pre>ng build --environment=prod</pre><p>The corresponding environment settings file (ie: src/app/environments/environment.prod.ts) is then bundled with the application source. In many cases, this is fine. Except it doesn’t quite fit the <em>Build Once, Deploy Anywhere</em> paradigm where a build server packages an artifact (ie: a Docker image) which can then be deployed anywhere. We needed a way to replace the environment settings at runtime.</p><h4><strong>Our solution</strong></h4><p>We tried to stay as close as possible to the built-in way of doing things, while achieving the runtime flexibility that we needed.</p><h4><strong>What stayed the same</strong></h4><p>The default configuration, corresponding to the development environment, is still located in src/app/environments/environment.ts but we got rid of all the environment.{whatever}.ts files.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7a9e47e6cd57dfccc9ae3198006e79d0/href">https://medium.com/media/7a9e47e6cd57dfccc9ae3198006e79d0/href</a></iframe><p>Services and components still load the environment settings as usual:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e442f3a5ab53115c4a9b34284acc2561/href">https://medium.com/media/e442f3a5ab53115c4a9b34284acc2561/href</a></iframe><p><strong>What changed</strong></p><p>The trick is to replace the configuration when the application loads. To do that, we changed main.ts a bit:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b9d5355018398e35eb76d6029e91260b/href">https://medium.com/media/b9d5355018398e35eb76d6029e91260b/href</a></iframe><p>Notice at line 11 we replace the environment settings from what’s returned from a module called EnvironmentLoader. EnvironmentLoader is pretty straightfoward. It tries to load an environment.json file from the assets, which are served staticly, and resolve the promise with it’s content. If the file wasn’t found, as it would be the case during development, it simply returns the default configuration from src/app/environments/environment.ts.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8fe21903aed04452c4ae216f3e871235/href">https://medium.com/media/8fe21903aed04452c4ae216f3e871235/href</a></iframe><p>Here’s what a production environment.json file would look like:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a11e05912319c2ea3a44d20a0ccdddbf/href">https://medium.com/media/a11e05912319c2ea3a44d20a0ccdddbf/href</a></iframe><h4><strong>Putting it all together</strong></h4><p>When we build our app, we don’t give it any additional argument:</p><blockquote>ng build</blockquote><p>At runtime, the default configuration is replaced if an environment.json file is present in dist/assets/environments/ so changing the configuration when running in Docker is a simple matter of copying a file:</p><blockquote>docker run -p 4200:4200 — name my_container app:1.0.0</blockquote><blockquote>docker cp prod.json my_container:/app/dist/assets/environments/environment.json</blockquote><p>Or mounting a volume:</p><blockquote>docker run -p 4200:4200 — volume=”~/config:/app/dist/assets/environments” app:1.0.0</blockquote><h4><strong>What next?</strong></h4><p>So far, this looks like a promising solution but it feels a little hack-ish so we’re still experimenting. Stay tuned.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=2512e342ab5a" width="1" height="1" alt=""><hr><p><a href="https://medium.com/voobans-tech-stories/multiple-environments-with-angular-and-docker-2512e342ab5a">Multiple environments with Angular and Docker</a> was originally published in <a href="https://medium.com/voobans-tech-stories">Vooban&#39;s tech stories</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Gestion de plusieurs environnements avec Angular et Docker]]></title>
            <link>https://medium.com/vooban-histoires-de-d%C3%A9veloppeurs/gestion-de-plusieurs-environnements-avec-angular-et-docker-96874cededa4?source=rss-365edb235e02------2</link>
            <guid isPermaLink="false">https://medium.com/p/96874cededa4</guid>
            <category><![CDATA[docker]]></category>
            <category><![CDATA[angularjs]]></category>
            <dc:creator><![CDATA[Mathieu Larouche Dubé]]></dc:creator>
            <pubDate>Tue, 05 Dec 2017 19:00:33 GMT</pubDate>
            <atom:updated>2018-06-27T13:00:30.241Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*W224MF6d1aqPTTHI" /><figcaption>“Rows of storage bins, the three on the top being red and the bottom two blue” by <a href="https://unsplash.com/@luke92?utm_source=medium&amp;utm_medium=referral">Luke Palmer</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>Dernièrement, nous avons été confrontés à un problème en essayant de gérer plusieurs configurations d’environnement dans une application Angular tournant sur Docker. Voici comment nous avons surmonté ce problème.</p><h4><strong>Le problème</strong></h4><p>Notre application est une <em>single page application</em> soutenue par un <em>API</em> .Net Core. Un exemple de configuration qui change d’un environnement à l’autre est l’adresse de l’<em>API</em>. Voici les différents fichiers de configuration, selon l’environnement:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e95ec36f1da2f6944291b9df69e4a664/href">https://medium.com/media/e95ec36f1da2f6944291b9df69e4a664/href</a></iframe><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/79517b3faac4bb96fe092c5e8ed1cb72/href">https://medium.com/media/79517b3faac4bb96fe092c5e8ed1cb72/href</a></iframe><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ee4f90b110f640f62c9fefd136e5df86/href">https://medium.com/media/ee4f90b110f640f62c9fefd136e5df86/href</a></iframe><p>Habituellement, les applications <em>Angular</em> sont compilées en ligne de commande en passant l’environnement désiré:</p><blockquote>ng build — environment=prod</blockquote><p>Le fichier Typescript correspondant à l’environnement (ex: src/app/environments/environment.prod.ts) est ainsi <em>packagé </em>directement dans le code de l’application. Pour bien des cas d’utilisation, ça fonctionne parfaitement. Toutefois, ça ne cadre pas exactement avec le paradigme <em>Build Once, Deploy Anywhere</em> où le serveur de build fabrique un artefact (ex: une image Docker) qui peut ainsi être déployé sur n’importe quel hôte. Nous avions donc besoin d’une façon de remplacer les configuration d’environnement au moment de l’exécution.</p><h4><strong>Notre solution</strong></h4><p>L’objectif était de rester le plus près possible de la façon de faire traditionnelle, tout en allant chercher la flexibilité dont on avait besoin.</p><h4><strong>Ce qui n’a pas changé</strong></h4><p>La configuration par défaut, correspondant à l’environnement de développement, est toujours présente à l’endroit habituel (src/app/environments/environment.ts) mais nous nous sommes débarrassés de tous les autres fichiers environment.{???}.ts.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/aca448a0099cbf7e7e2bfef011e3edcd/href">https://medium.com/media/aca448a0099cbf7e7e2bfef011e3edcd/href</a></iframe><p>Les services et composants Angular continuent de charger les configurations avec l’instruction import:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a660f19f5627bbbcf5d5d3c3dcf6fa7c/href">https://medium.com/media/a660f19f5627bbbcf5d5d3c3dcf6fa7c/href</a></iframe><p><strong>Ce qui a changé</strong></p><p>L’idée étant de remplacer la configuration par défaut au lancement de l’application. C’est pourquoi nous avons changé un peu le comportement par défaut de main.ts:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/259432def8ab9ee758f75e05e1a7d586/href">https://medium.com/media/259432def8ab9ee758f75e05e1a7d586/href</a></iframe><p>Remarquez à la ligne 12, nous remplaçons les configurations par ce qui nous a été retourné par le module EnvironmentLoader.</p><p>EnvironmentLoader est assez simple. Il essaye de charger un fichier environnement.json à partir des assets, qui sont servis de façon statique. Si le fichier a été trouvé, son contenu est retourné à l’appelant. Dans le cas contraire, c’est la configuration par défaut (src/app/environments/environment.ts) qui est retournée.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8fe21903aed04452c4ae216f3e871235/href">https://medium.com/media/8fe21903aed04452c4ae216f3e871235/href</a></iframe><p>Voici à quoi pourrait ressembler un fichier environment.json correspondant à l’environnement de production:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/97a43085c0922a9a1ad743b217317240/href">https://medium.com/media/97a43085c0922a9a1ad743b217317240/href</a></iframe><h4><strong>Le résultat</strong></h4><p>Lorsqu’on compile notre application, il n’est plus nécessaire de passer l’environnement:</p><blockquote>ng build</blockquote><p>Lors de l’exécution, la configuration par défaut sera remplacée si un fichier environment.json est présent à l’endroit prévu (dist/assets/environments/). Cela signifie que pour changer la configuration lorsqu’on roule avec Docker, il suffit de copier un fichier:</p><blockquote>docker run -p 4200:4200 — name my_container app:1.0.0<br>docker cp prod.json my_container:/app/dist/assets/environments/environment.json</blockquote><p>Ou encore attacher un volume:</p><blockquote>docker run -p 4200:4200 — volume=”~/config:/app/dist/assets/environments” app:1.0.0</blockquote><h4><strong>La suite…</strong></h4><p>Jusqu’à maintenant, cette solution fonctionne très bien mais ne sommes pas sûr qu’elle soit la réponse parfaite à ce problème. Nous continuons d’expérimenter. Restez à l’écoute!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=96874cededa4" width="1" height="1" alt=""><hr><p><a href="https://medium.com/vooban-histoires-de-d%C3%A9veloppeurs/gestion-de-plusieurs-environnements-avec-angular-et-docker-96874cededa4">Gestion de plusieurs environnements avec Angular et Docker</a> was originally published in <a href="https://medium.com/vooban-histoires-de-d%C3%A9veloppeurs">Vooban - Histoires de développeurs</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How to mock DateTime in .net]]></title>
            <link>https://medium.com/voobans-tech-stories/datetime-now-73306410d3f5?source=rss-365edb235e02------2</link>
            <guid isPermaLink="false">https://medium.com/p/73306410d3f5</guid>
            <category><![CDATA[testing]]></category>
            <category><![CDATA[dotnet-core]]></category>
            <category><![CDATA[dotnet]]></category>
            <dc:creator><![CDATA[Mathieu Larouche Dubé]]></dc:creator>
            <pubDate>Sun, 29 May 2016 18:07:09 GMT</pubDate>
            <atom:updated>2018-06-07T13:48:15.324Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*lPJGG3AGLqWGSWuw" /><figcaption>“An analog timepiece with black number boards” by <a href="https://unsplash.com/@loic?utm_source=medium&amp;utm_medium=referral">Loic Djim</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><h3>Datetime.now</h3><p>A few weeks ago, a colleague asked me how I would single test a class using <a href="https://msdn.microsoft.com/en-us/library/system.datetime.now%28v=vs.110%29.aspx"><strong>DateTime.Now</strong></a> to have the date. Good question! What’s the problem with this property? As it’s a static property, we can’t simply do a mock. Since, by definition, the value of <strong>DateTime.Now</strong> is constantly changing, we can’t download it to IoC Container.</p><p>I once had this problem and I had a simple solution, but I was wondering if I could do better. Here are the results of my research.</p><p>Before we start, here is a code that I would like to test.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0f06ebbac8be5084f5de6e77155e9e8d/href">https://medium.com/media/0f06ebbac8be5084f5de6e77155e9e8d/href</a></iframe><p>To be sure that my method is working properly, I would like to build two single tests, one for a weekday and another one for a weekend day.</p><h4>First option</h4><p>Since the usual mock tools can’t help us, we can use other tools. Stronger mock frameworks working on IL that let us mock anything are available — even those that are static or sealed. One of these tools is <a href="http://www.typemock.com/isolator-product-page">Typemock Isolator</a>. Here is an example of its use:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8b495f699f2cbf81c2cb4b29514f509c/href">https://medium.com/media/8b495f699f2cbf81c2cb4b29514f509c/href</a></iframe><p>A characteristic to add, a line code to mock <strong>DateTime.Now</strong> and there you go! We don’t even need to touch the existing code. It’s an elegant and quite simple solution to set up. However, it’s not perfect. On the one hand, tests become extremely long to run. This is normal since Typemock is working with IL Rewriting, which is adding some overhead. On the other hand, Typemock is quite expensive ($400 annually for a developer licence + $1,000 annually for a build server licence).</p><p>Another test framework that works the same way is also available — <a href="https://msdn.microsoft.com/en-us/library/hh549175.aspx"><strong>Microsoft Fakes</strong></a>. It comes with Visual Studio Premium, so I was not able to test it.</p><h4>Second option</h4><p>An option would be to add an abstraction. To do so, you need to create, for example, an ITimeProvider interface and use it instead of <strong>DateTime</strong>:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/788d882b6779cfcf11bc5426bb612b2d/href">https://medium.com/media/788d882b6779cfcf11bc5426bb612b2d/href</a></iframe><p>We can now write our single tests as usual, using regular mocks:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e06ed134adab8eb6c1d2b6a63bdba2c5/href">https://medium.com/media/e06ed134adab8eb6c1d2b6a63bdba2c5/href</a></iframe><p>For code running outside the tests, simply add a class that implements <strong>ITimeProvider </strong>and searches the date in <strong>DateTime.Now</strong>.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ccb32e7a1b626f785d8e5fe7f459cf03/href">https://medium.com/media/ccb32e7a1b626f785d8e5fe7f459cf03/href</a></iframe><p>Besides not adding additional costs, the biggest advantage of this technique is that it does not negatively affect test-running speed.</p><p>There’s also another advantage. Sometimes, we want to mock date outside of the single trial context, for user tests in a trial environment, for example. Usually, we would have to change the date in the system, which is not the ideal method. Using a different <strong>ITimeProvider</strong>, we can change the date of BD or in the application configuration:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/da9dd4e365e0204cff469991f8d54128/href">https://medium.com/media/da9dd4e365e0204cff469991f8d54128/href</a></iframe><p>Obviously, we need to make sure not to use DateTime.Now in the code. It could be interesting to add a check-up using a tool like FxCop.</p><p>To conclude, which approach is the best? Personally, I favour using <strong>ITimeProvider</strong>, but the two options are certainly valid. Don’t hesitate to try them and make up your own mind.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=73306410d3f5" width="1" height="1" alt=""><hr><p><a href="https://medium.com/voobans-tech-stories/datetime-now-73306410d3f5">How to mock DateTime in .net</a> was originally published in <a href="https://medium.com/voobans-tech-stories">Vooban&#39;s tech stories</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Comment "mocker" DateTime in .net]]></title>
            <link>https://medium.com/vooban-histoires-de-d%C3%A9veloppeurs/mock-de-datetime-now-185ef271b19?source=rss-365edb235e02------2</link>
            <guid isPermaLink="false">https://medium.com/p/185ef271b19</guid>
            <category><![CDATA[dotnet]]></category>
            <category><![CDATA[testing]]></category>
            <category><![CDATA[dotnet-core]]></category>
            <dc:creator><![CDATA[Mathieu Larouche Dubé]]></dc:creator>
            <pubDate>Sun, 29 May 2016 18:07:09 GMT</pubDate>
            <atom:updated>2018-06-27T12:59:35.181Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*YmaVJIIsYQgOsJCS" /><figcaption>“An analog timepiece with black number boards” by <a href="https://unsplash.com/@loic?utm_source=medium&amp;utm_medium=referral">Loic Djim</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><h3>Mock de datetime.now</h3><p>L’autre jour, un collègue m’a demandé comment je m’y prendrais pour tester unitairement une classe qui utilise <a href="https://msdn.microsoft.com/en-us/library/system.datetime.now%28v=vs.110%29.aspx">DateTime.Now</a> pour obtenir la date du jour. Bonne question! Mais quel est donc le problème avec cette propriété ? Comme il s’agit d’une<em>static property</em>, on ne peut pas simplement faire un mock. Et puisque, par définition, la valeur de DateTime.Now change constamment, on ne peut pas non plus l’enregistrer dans l’<em>IoC Container</em>.</p><p>J’avais déjà rencontré ce problème et j’avais une solution toute prête mais je me suis demandé s’il était possible de faire mieux. Voici le résultat de mes recherches.</p><p>Avant de commencer, voici un exemple de code que j’aimerais tester.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0f06ebbac8be5084f5de6e77155e9e8d/href">https://medium.com/media/0f06ebbac8be5084f5de6e77155e9e8d/href</a></iframe><p>Pour m’assurer du bon fonctionnement de ma méthode, j’aimerais composer deux tests unitaires, un pour un jour de semaine et un pour un jour de fin de semaine.</p><h4>Première option</h4><p>Puisque les outils de mocks habituels ne peuvent pas nous aider, on peut changer d’outil. Il existe en effet des frameworks de mocks plus puissants, qui travaillent au niveau de l’IL et qui permettent de mocker n’importe quoi. Même ce qui est <em>static</em> ou<em>sealed</em>. Un de ces outils est <a href="http://www.typemock.com/isolator-product-page">Typemock Isolator</a>. Voici un exemple de son utilisation :</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8b495f699f2cbf81c2cb4b29514f509c/href">https://medium.com/media/8b495f699f2cbf81c2cb4b29514f509c/href</a></iframe><p>Un attribut à ajouter, une ligne de code pour mocker DateTime.Now et bingo ! On n’a même pas besoin de toucher au code existant. C’est une solution élégante et relativement simple à mettre en place. Toutefois, ce n’est pas parfait. D’une part, les tests deviennent extrèmement longs à rouler. C’est normal parce que Typemock fonctionne avec du IL Rewriting, ce qui ajoute pas mal d’overhead. D’autre part, Typemock est loin d’être gratuit (400$/an pour une licence développeur + 1000$/an pour une licence build server).</p><p>Il existe un autre framework de test qui utilise le même principe, <a href="https://msdn.microsoft.com/en-us/library/hh549175.aspx">Microsoft Fakes</a>. Il vient avec Visual Studio Premium donc je n’ai pas pu l’essayer.</p><h4>Deuxième option</h4><p>Une autre option serait d’ajouter une abstraction. Il s’agit de créer, par exemple, une interface ITimeProvider et de l’utiliser à la place de DateTime :</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b4c91d6150bc03cb682292d3f0fb5e52/href">https://medium.com/media/b4c91d6150bc03cb682292d3f0fb5e52/href</a></iframe><p>On peut maintenant écrire nos tests unitaires comme à l’habitude, avec des mocks réguliers :</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ce2677f3c6e6a0d9b43448c85b2696cd/href">https://medium.com/media/ce2677f3c6e6a0d9b43448c85b2696cd/href</a></iframe><p>Pour ce qui est du code qui roule hors du contexte des tests, il suffit d’ajouter une classe qui implémente ITimeProvider et qui va chercher la date dans DateTime.Now.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/bb4dfee28458104a5323a1a8b04e0dc7/href">https://medium.com/media/bb4dfee28458104a5323a1a8b04e0dc7/href</a></iframe><p>En plus de n’entrainer aucun coût supplémentaire, le gros avantage de cette technique est qu’elle n’affecte pas négativement la vitesse d’exécution des tests.</p><p>Mais elle apporte aussi un autre avantage. Il arrive parfois que l’on veuille mocker la date du jour en dehors du contexte des essais unitaires. Pour les tests utilisateur dans un environnement d’essais, par exemple. En temps normal, il faudrait changer la date du système, ce qui est loin d’être idéal. Grâce à un différent ITimeProvider, on peut aller chercher la date dans une BD ou dans la configuration de l’application :</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b53428c3ef2d839d56900e8e1bf2442d/href">https://medium.com/media/b53428c3ef2d839d56900e8e1bf2442d/href</a></iframe><p>Évidemment, il faut s’assurer de ne plus utiliser DateTime.Now dans le code. Il pourrait être intéressant d’ajouter une validation avec un outil comme FxCop.</p><p>En conclusion, quelle est la meilleure approche ? Personnellement, je privilégie l’utilisation du ITimeProvider mais les deux options sont certainement valables. N’hésitez-pas à les essayer pour vous faire votre propre idée.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=185ef271b19" width="1" height="1" alt=""><hr><p><a href="https://medium.com/vooban-histoires-de-d%C3%A9veloppeurs/mock-de-datetime-now-185ef271b19">Comment &quot;mocker&quot; DateTime in .net</a> was originally published in <a href="https://medium.com/vooban-histoires-de-d%C3%A9veloppeurs">Vooban - Histoires de développeurs</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>