<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Matheus Rodrigues</title>
    <description>The latest articles on DEV Community by Matheus Rodrigues (@matheusrodrigues).</description>
    <link>https://dev.to/matheusrodrigues</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F48088%2Fe7835b2b-60e6-49bc-84e8-3551b9f6915f.png</url>
      <title>DEV Community: Matheus Rodrigues</title>
      <link>https://dev.to/matheusrodrigues</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/matheusrodrigues"/>
    <language>en</language>
    <item>
      <title>Integration Tests In ASP.NET Core Controllers</title>
      <dc:creator>Matheus Rodrigues</dc:creator>
      <pubDate>Mon, 03 Sep 2018 11:30:48 +0000</pubDate>
      <link>https://dev.to/matheusrodrigues/integration-tests-in-aspnet-core-controllers-3lm8</link>
      <guid>https://dev.to/matheusrodrigues/integration-tests-in-aspnet-core-controllers-3lm8</guid>
      <description>&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.matheus.ro%2Fwp-content%2Fuploads%2F2018%2F09%2FIntegration-Tests-In-ASP.NET-Core-Controllers-768x384.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.matheus.ro%2Fwp-content%2Fuploads%2F2018%2F09%2FIntegration-Tests-In-ASP.NET-Core-Controllers-768x384.png"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Continuing the series of posts to explore different testing aspects in asp .net core. We’re going experiment with Test Server to create integration tests.&lt;/p&gt;

&lt;p&gt;Test Server is a new features that asp .net core is bringing. It creates a browser abstraction, allowing us to simulate application’s behavior without opening a browser.&lt;/p&gt;

&lt;p&gt;There are many ways to deal with the database in integration tests. The idea is to not affect the data that’s already in the database. We could use an entirely different database, creating and deleting it every time we run our tests.&lt;/p&gt;

&lt;p&gt;Other approach is to use the same database as the application, but clean up data after each test. For example, if we add a register into the database and in the end of the test we remove it.&lt;/p&gt;

&lt;p&gt;We can follow a transactional approach. Which means that we create a transaction scope within each unit test to not commit changes when we call &lt;code&gt;.SaveChanges()&lt;/code&gt;. Then, when the test is over, this transaction is disposed and no changes are committed to the database.&lt;/p&gt;

&lt;p&gt;Entity Framework Core In-Memory database could also be used. Following this approach we won’t have to worry about storing data. But, how can we call this an integration test if we aren’t using a real database? Yeah, that’s right, this approach will fall into the unit test category.&lt;/p&gt;

&lt;p&gt;For this post, we’re going to use the transactional approach.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.matheus.ro/2018/09/03/integration-tests-in-asp-net-core-controllers/" rel="noopener noreferrer"&gt;Continue Reading...&lt;/a&gt;&lt;/p&gt;

</description>
      <category>csharp</category>
      <category>dotnet</category>
      <category>unittest</category>
      <category>dotnetcore</category>
    </item>
    <item>
      <title>Unit Tests In ASP.NET Core Controllers</title>
      <dc:creator>Matheus Rodrigues</dc:creator>
      <pubDate>Mon, 28 May 2018 12:00:15 +0000</pubDate>
      <link>https://dev.to/matheusrodrigues/unit-tests-in-aspnet-core-controllers-2adk</link>
      <guid>https://dev.to/matheusrodrigues/unit-tests-in-aspnet-core-controllers-2adk</guid>
      <description>&lt;p&gt;In this new series of posts, I want to show different aspects of testing an ASP.NET core application.&lt;/p&gt;

&lt;p&gt;I will start in this post showing how to create unit tests for controllers. Then I am going to talk integration test in the controller and explore ASP.NET core new feature, Test Host. After that, let’s see how we can test our application business logic.&lt;/p&gt;

&lt;p&gt;Moving to the database realm. I’m going to show how we can create unit tests for code that deals with &lt;code&gt;DbContext&lt;/code&gt;, using Entity Framework Core In-memory database feature. And them, how to do integration tests with a real database, with strategies to maintain a database for integration tests.&lt;/p&gt;

&lt;p&gt;Let’s jump right into it!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.matheus.ro/2018/05/28/unit-tests-in-asp-net-core-controllers/"&gt;Continue Reading...&lt;/a&gt;&lt;/p&gt;

</description>
      <category>dotnet</category>
      <category>testing</category>
      <category>tutorial</category>
      <category>unittest</category>
    </item>
    <item>
      <title>Improving Unit Test Readability In .Net With Fluent Assertions</title>
      <dc:creator>Matheus Rodrigues</dc:creator>
      <pubDate>Mon, 16 Apr 2018 12:00:45 +0000</pubDate>
      <link>https://dev.to/matheusrodrigues/improving-unit-test-readability-in-net-with-fluent-assertions-4336</link>
      <guid>https://dev.to/matheusrodrigues/improving-unit-test-readability-in-net-with-fluent-assertions-4336</guid>
      <description>&lt;p&gt;The assertion in unit tests is the phase where we verify if the test result is what we expect. This phase is straightforward, usually it is just one line. But it is not very readable, because it looks something like this: &lt;code&gt;Assert.Equal(“ExpectedResult”, “ActualResult”)&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;There is no logical order in expected result and actual result parameter, this always confuses me. Other aspect about readability is that it is not human readable. If you try to read a code like this out loud it is not sound that good.&lt;/p&gt;

&lt;p&gt;To solve this problem, frameworks to improve readability in assertions were created, like, &lt;a href="https://fluentassertions.com/"&gt;Fluent Assertions&lt;/a&gt; framework for .net. As the name say, this framework use a fluent interface to create readable assertions.&lt;/p&gt;

&lt;p&gt;In this post, I’m going to show how Fluent Assertions can improve the unit test readability.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.matheus.ro"&gt;Continue Reading...&lt;/a&gt; &lt;a href="https://www.matheus.ro/2018/04/16/improving-unit-test-readability-in-net-with-fluent-assertions/"&gt;Improving Unit Test Readability In .Net With Fluent Assertions&lt;/a&gt;&lt;/p&gt;

</description>
      <category>dotnet</category>
      <category>testing</category>
      <category>unittest</category>
      <category>fluentassertions</category>
    </item>
    <item>
      <title>Mock Multiple Calls To The Same Method With FakeItEasy, Moq and NSubstitute</title>
      <dc:creator>Matheus Rodrigues</dc:creator>
      <pubDate>Mon, 26 Mar 2018 12:00:32 +0000</pubDate>
      <link>https://dev.to/matheusrodrigues/mock-multiple-calls-to-the-same-method-with-fakeiteasy-moq-and-nsubstitute-45lp</link>
      <guid>https://dev.to/matheusrodrigues/mock-multiple-calls-to-the-same-method-with-fakeiteasy-moq-and-nsubstitute-45lp</guid>
      <description>&lt;p&gt;You can download this article project on &lt;a href="https://github.com/mrodrigues1/MockMultipleCalls"&gt;GitHub&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Sometimes when doing unit test, we encounter subsequent calls to the same method. When mocking a method the default behavior is to always return the same result.&lt;/p&gt;

&lt;p&gt;In this post, I will show how you can mock multiple calls to the same method with the mocking frameworks &lt;a href="https://fakeiteasy.github.io/"&gt;FakeItEasy&lt;/a&gt;, &lt;a href="https://github.com/moq/moq4"&gt;Moq&lt;/a&gt; and &lt;a href="http://nsubstitute.github.io/"&gt;NSubstitute&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.matheus.ro/2018/03/26/mock-multiple-calls-to-the-same-method-with-fakeiteasy-moq-and-nsubstitute/"&gt;Continue Reading...&lt;/a&gt;&lt;/p&gt;

</description>
      <category>testing</category>
      <category>dotnet</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>How To: Use EntityFramework Core In-Memory Database For Unit Testing</title>
      <dc:creator>Matheus Rodrigues</dc:creator>
      <pubDate>Mon, 19 Mar 2018 13:00:37 +0000</pubDate>
      <link>https://dev.to/matheusrodrigues/how-to-use-entityframework-core-in-memory-database-for-unit-testing-1kl9</link>
      <guid>https://dev.to/matheusrodrigues/how-to-use-entityframework-core-in-memory-database-for-unit-testing-1kl9</guid>
      <description>&lt;p&gt;You can download this article project on &lt;a href="https://github.com/mrodrigues1/SimpleToDo"&gt;GitHub&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;In this post, I’ll show how to use the in-memory feature of Entity Framework core to create unit tests involving the database. Let’s jump right in!&lt;/p&gt;

&lt;p&gt;Create unit tests using a real &lt;em&gt;DbContext&lt;/em&gt; is a pain because we have to mock the entire context to achieve this. Entity Framework core makes unit tests easier to write by providing us with an in-memory database provider. It creates an in-memory representation of our &lt;em&gt;DbContext&lt;/em&gt;, for this reason, we don’t have to worry about mocking the database.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.matheus.ro/2018/03/19/how-to-use-entityframework-core-in-memory-database-for-unit-testing/"&gt;Continue Reading...&lt;/a&gt;&lt;/p&gt;

</description>
      <category>dotnet</category>
      <category>testing</category>
      <category>tutorial</category>
      <category>entityframework</category>
    </item>
    <item>
      <title>Getting Started With MongoDB Using ASP.NET Core Web API</title>
      <dc:creator>Matheus Rodrigues</dc:creator>
      <pubDate>Mon, 05 Mar 2018 13:00:47 +0000</pubDate>
      <link>https://dev.to/matheusrodrigues/getting-started-with-mongodb-using-aspnet-core-web-api-1k4b</link>
      <guid>https://dev.to/matheusrodrigues/getting-started-with-mongodb-using-aspnet-core-web-api-1k4b</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;&lt;a href="https://github.com/mrodrigues1/MongoDB-WebApi" rel="noopener noreferrer"&gt;You can download the code here&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Recently, I’m trying to get into NoSQL databases, more specific into document databases.&lt;/p&gt;

&lt;p&gt;After some research, MongoDB was my choice to start with. MongoDB is a well know and popular document database, which store JSON-like documents. It’s quite accessible, having drivers supporting major languages and frameworks. Have a great community support with a large number of  unofficial drivers. &lt;/p&gt;

&lt;p&gt;The goal is to show how to set up a MongoDB instance and how to consume it with a web API in asp.net core.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Softwares needed:&lt;/strong&gt;
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://www.mongodb.com/download-center#community" rel="noopener noreferrer"&gt;MongoDB&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.visualstudio.com/downloads/" rel="noopener noreferrer"&gt;Visual Studio 2017 Community Edition&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.getpostman.com/" rel="noopener noreferrer"&gt;Postman&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;*I used windows 10 to create this tutorial.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.matheus.ro/2018/03/05/getting-started-mongodb-using-asp-net-core-web-api/" rel="noopener noreferrer"&gt;Continue Reading...&lt;/a&gt;&lt;/p&gt;

</description>
      <category>dotnet</category>
      <category>tutorial</category>
      <category>mongodb</category>
    </item>
    <item>
      <title>Design Patterns and Practices in .Net: Fluent Builder Test Pattern</title>
      <dc:creator>Matheus Rodrigues</dc:creator>
      <pubDate>Mon, 08 Jan 2018 11:00:24 +0000</pubDate>
      <link>https://dev.to/matheusrodrigues/design-patterns-and-practices-in-net-fluent-builder-test-pattern-105g</link>
      <guid>https://dev.to/matheusrodrigues/design-patterns-and-practices-in-net-fluent-builder-test-pattern-105g</guid>
      <description>&lt;p&gt;The unit test’s arrange phase is where we write more code. Coding data creation can be cumbersome. The worst scenario happen when we have to fill all class’ properties, even though, we’re only using one property in the test. Besides being tedious, the data creation process can have a lot of code, resulting in less readable and maintainable tests.&lt;/p&gt;

&lt;p&gt;To address this problem, we can use the Builder Pattern. The pattern is described in the book design patterns, and solve problems like:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;How can a class create different representation of a complex object?&lt;/li&gt;
&lt;li&gt;How to simplify a class which creates a complex object?&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The Builder Pattern brings more flexibility in a complex object’s creation, by delegating the construction of the complex object to a builder object. The builder object can create different representations of the same complex object.&lt;/p&gt;

&lt;p&gt;In this post I’m focus on the unit test side, and I’m gonna show you a different version of the Builder Pattern, the Fluent Builder Test Pattern.&lt;/p&gt;

&lt;p&gt;The main goal of the Fluent Builder Test Pattern is to facilitate the test data creation. Joining the Builder Pattern and the Fluent Interface Pattern makes the complex object’s creation straight forward.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.matheus.ro/2018/01/08/design-patterns-practices-net-fluent-builder-test-pattern/"&gt;Continue Reading...&lt;/a&gt;&lt;/p&gt;

</description>
      <category>dotnet</category>
      <category>designpattern</category>
      <category>unittest</category>
    </item>
    <item>
      <title>How To: Create a Simple Alert System Using Tag Helpers In Asp.Net Core Mvc</title>
      <dc:creator>Matheus Rodrigues</dc:creator>
      <pubDate>Mon, 18 Dec 2017 11:00:45 +0000</pubDate>
      <link>https://dev.to/matheusrodrigues/how-to-create-a-simple-alert-system-using-tag-helpers-in-aspnet-core-mvc-3pn8</link>
      <guid>https://dev.to/matheusrodrigues/how-to-create-a-simple-alert-system-using-tag-helpers-in-aspnet-core-mvc-3pn8</guid>
      <description>&lt;p&gt;In this post I’m gonna show you how to create an alert system using the tag helpers feature from the asp.net core mvc.&lt;/p&gt;

&lt;p&gt;During this tutorial, I’ll be using a simple to do list project that I build, in order to experiment new stuff, &lt;a href="https://github.com/mrodrigues1/SimpleToDo"&gt;you can download or clone this git repository&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;The Problem&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Feedback is important for the user, usually, is in form of messages on the screen. We could follow the client-side approach, by, creating an ajax request and showing a message after the response, or instead, follow the server-side approach, storing a message on a view bag, or even return a model with the message. &lt;/p&gt;

&lt;p&gt;The asp.net core mvc has a new feature called tag helper, enabling us to create an alert system on the server-side.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.matheus.ro/2017/12/18/how-to-create-a-simple-alert-system-using-tag-helpers-in-asp-net-core-mvc/"&gt;Continue Reading...&lt;/a&gt;&lt;/p&gt;

</description>
      <category>dotnet</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Design Patterns and Practices in .Net: Option Functional Type in C#</title>
      <dc:creator>Matheus Rodrigues</dc:creator>
      <pubDate>Wed, 27 Sep 2017 00:29:37 +0000</pubDate>
      <link>https://dev.to/matheusrodrigues/design-patterns-and-practices-in-net-option-functional-type-in-c-5g3l</link>
      <guid>https://dev.to/matheusrodrigues/design-patterns-and-practices-in-net-option-functional-type-in-c-5g3l</guid>
      <description>

&lt;p&gt;In the functional programming the option type is very used, in languages such as Haskell, F#, Scala and a lot more. In this language it’s a convention to return a value when the function fails. The following example is a function written in F#, to show how the option type works:&lt;/p&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;(* This function uses pattern matching to deconstruct Options *)
let compute = function
  | None -&amp;gt; "No value"
  | Some x -&amp;gt; sprintf "The value is: %d" x

printfn "%s" (compute &amp;lt;| Some 42)(* The value is: 42 *)
printfn "%s" (compute None) (* No value *)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;We have no equivalent of the option type in the C# language , but we can try to emulate it.&lt;/p&gt;

&lt;p&gt;In my last &lt;a href="https://wp.me/p9d6L8-1h"&gt;post&lt;/a&gt;, I discussed two design patterns, Null Object and the Special Case Object, the goal of these patterns are to return a special object instead of return null. If you didn’t read that post, &lt;a href="https://wp.me/p9d6L8-1h"&gt;go check it&lt;/a&gt; and come back to this post.&lt;/p&gt;

&lt;p&gt;The idea in this post is to show the implementation of the option type in C#.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Problem&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;The problem is very similar to my last post, in fact it’s the continuation of that same problem.&lt;/p&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public IProduct GetProductById(int productId)
{
    Product product = _productRepository.GetProductById(productId);

    if(product == null)
        return new ProductNotFound();

    return product;
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;a href="https://www.matheus.ro/2017/09/26/design-patterns-and-practices-in-net-option-functional-type-in-csharp/"&gt;Continue Reading...&lt;/a&gt;&lt;/p&gt;


</description>
      <category>csharp</category>
      <category>cleancode</category>
      <category>designpattern</category>
      <category>dotnet</category>
    </item>
    <item>
      <title>Design Patterns and Practices in .Net: Null Object and Special Case Object</title>
      <dc:creator>Matheus Rodrigues</dc:creator>
      <pubDate>Mon, 25 Sep 2017 23:41:02 +0000</pubDate>
      <link>https://dev.to/matheusrodrigues/design-patterns-and-practices-in-net-null-object-and-special-case-object-41i7</link>
      <guid>https://dev.to/matheusrodrigues/design-patterns-and-practices-in-net-null-object-and-special-case-object-41i7</guid>
      <description>

&lt;p&gt;In programming null is an awkward thing to deal with, when you return a variable and it’s possible to be a null, you have to remember to make null checks all around your code.&lt;/p&gt;

&lt;p&gt;The null object and the special case object are patterns that try to reduce this type of boiler plate code.&lt;/p&gt;

&lt;p&gt;A null object is an object that doesn’t have a referenced value, is an object with a default behavior. A behavior which represents a missing object.&lt;/p&gt;

&lt;p&gt;Special case object is very similar to the null object, but it’s an improvement over the null object, because instead of creating a generic “missing object”, you are creating a more specialized object that can suit better your needs.&lt;/p&gt;

&lt;p&gt;The goal of these two pattern is to return a null or a special case version of an object, instead of return a null reference.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Problem&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Many times we caught ourselves making this type of check:&lt;/p&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public Product GetProductById(int productId)
{
    Product product = _productRepository.GetProductById(productId);
    if(product == null)
        throw new Exception("Product Not Found");
    return product;
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;a href="https://www.matheus.ro/2017/09/25/design-patterns-practices-net-null-object-special-case-object/"&gt;Continue Reading...&lt;/a&gt;&lt;/p&gt;


</description>
      <category>csharp</category>
      <category>cleancode</category>
      <category>designpattern</category>
      <category>dotnet</category>
    </item>
    <item>
      <title>How To Get appsettings.json Data In Asp.Net Core MVC</title>
      <dc:creator>Matheus Rodrigues</dc:creator>
      <pubDate>Mon, 25 Sep 2017 23:17:48 +0000</pubDate>
      <link>https://dev.to/matheusrodrigues/how-to-get-appsettingsjson-data-in-aspnet-core-mvc-5337</link>
      <guid>https://dev.to/matheusrodrigues/how-to-get-appsettingsjson-data-in-aspnet-core-mvc-5337</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;&lt;a href="https://github.com/mrodrigues1/AppSettingsDI" rel="noopener noreferrer"&gt;You can download this sample from this git repository.&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;In this tutorial I’ll show you how to get the data from &lt;em&gt;appsettings.json&lt;/em&gt; using the built-in dependency injection in Asp.Net Core MVC.&lt;/p&gt;

&lt;p&gt;Let’s start right away \o/&lt;/p&gt;

&lt;p&gt;First, you need to create a project in visual studio. I’m using visual studio 2017 community with .net core version 1.1.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fi1.wp.com%2Fwww.matheus.ro%2Fwp-content%2Fuploads%2F2017%2F09%2F1-4Rx8EdKFP6Q3Ut5SK1itNg.png%3Fresize%3D800%252C556%26ssl%3D1" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fi1.wp.com%2Fwww.matheus.ro%2Fwp-content%2Fuploads%2F2017%2F09%2F1-4Rx8EdKFP6Q3Ut5SK1itNg.png%3Fresize%3D800%252C556%26ssl%3D1"&gt;&lt;/a&gt;New ASP.NET Core Web App Project&lt;/p&gt;

&lt;p&gt;I’m creating an ASP.NET Core Web Application, that’s the new version of the mvc framework.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.matheus.ro/2017/09/25/get-appsettings-json-data-asp-net-core-mvc/" rel="noopener noreferrer"&gt;Continue Reading...&lt;/a&gt;&lt;/p&gt;

</description>
      <category>dotnet</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Unit Test And Time Dependency</title>
      <dc:creator>Matheus Rodrigues</dc:creator>
      <pubDate>Mon, 25 Sep 2017 01:27:50 +0000</pubDate>
      <link>https://dev.to/matheusrodrigues/unit-test-and-time-dependency-jc9</link>
      <guid>https://dev.to/matheusrodrigues/unit-test-and-time-dependency-jc9</guid>
      <description>

&lt;p&gt;When you start to write unit tests, inevitably you’ll encounter a hard time when a functionality depends on time. Depend on DateTime.Now doesn’t work well for unit testing, your tests will pass on certain time of day and fail in others.&lt;/p&gt;

&lt;p&gt;To resolve this problem we need to isolate the time dependency of system, to be able to make our unit test reliable.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Example&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;So, we have a shop that gives some discount based on the hour of the day, and we have a method that return how much the discount will be:&lt;/p&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public class Shop
{
    //Constructor
    //public Shop(.....

    //Other Implementations
    //.....

    public int GetCurrentDiscount()
    {
        switch (DateTime.Now.Hour)
        {
            case 9:
                return 30;
            case 12:
                return 20;
            case 18:
                return 40;
            default:
                return 0;
        }
    }
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;a href="https://www.matheus.ro/2017/09/25/unit-test-time-dependency/"&gt;Continue Reading...&lt;/a&gt;&lt;/p&gt;


</description>
      <category>designpattern</category>
      <category>testing</category>
      <category>dotnet</category>
      <category>tutorial</category>
    </item>
  </channel>
</rss>
