<?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 Matheus Henrique on Medium]]></title>
        <description><![CDATA[Stories by Matheus Henrique on Medium]]></description>
        <link>https://medium.com/@bromatheuscruz?source=rss-d4d6de76f18e------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*mLHaxgAxnwkurnrserTf_A.jpeg</url>
            <title>Stories by Matheus Henrique on Medium</title>
            <link>https://medium.com/@bromatheuscruz?source=rss-d4d6de76f18e------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 05 Dec 2026 01:50:54 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@bromatheuscruz/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[Testes de integração: MongoUnit + Mocha + Express]]></title>
            <link>https://medium.com/@bromatheuscruz/testes-de-integra%C3%A7%C3%A3o-mongounit-mocha-express-3f9b099c468b?source=rss-d4d6de76f18e------2</link>
            <guid isPermaLink="false">https://medium.com/p/3f9b099c468b</guid>
            <category><![CDATA[nodejs]]></category>
            <category><![CDATA[mongodb]]></category>
            <category><![CDATA[express]]></category>
            <category><![CDATA[test-automation]]></category>
            <category><![CDATA[test-driven-development]]></category>
            <dc:creator><![CDATA[Matheus Henrique]]></dc:creator>
            <pubDate>Tue, 08 Oct 2019 02:07:22 GMT</pubDate>
            <atom:updated>2019-10-30T01:39:29.168Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="MongoDB + Mongoose + Express + NodeJS" src="https://cdn-images-1.medium.com/max/750/1*8UBsjNZr_0N6PJbvBRLn2w.jpeg" /></figure><p>Meu primeiro contato <strong>profissional com testes</strong> foi incrível.</p><p>Eu sempre soube da <strong>importância</strong> de uma aplicação ser <strong>testável</strong> e <strong>testada</strong> mas nunca tive a oportunidade de trabalhar com testes, mas até que o grande momento chegou.</p><p>Na época eu trabalhava com <strong>Java </strong>(<em>A plataforma/linguagem que mais amo ❤</em>) e para fazer os testes de integração utilizávamos o <strong>DBUnit </strong>.</p><blockquote>Extensão do JUnit para os testes de integração que envolvem banco de dados</blockquote><p>A gente basicamente configurava o estado do nosso banco antes da execução de cada teste, através de <strong>.xml </strong>e em seguida simulávamos as requisições HTTP e fazíamos asserts no response da requisição.</p><h4>Nós vamos fazer a mesma coisa aqui, mas com NodeJS</h4><h3>Vamos ao que interessa …</h3><p>Precisaremos do <strong><em>NodeJS</em></strong> instalado<em> (obviamente)</em></p><p>Para adiantar deixarei o <em>package.json</em> do projeto, daí é só rodar <em>npm install</em> ou <em>yarn.</em></p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a6b1d876a9ff9639a14c6d72d5a6b68b/href">https://medium.com/media/a6b1d876a9ff9639a14c6d72d5a6b68b/href</a></iframe><p>Se você reparou utilizamos o seguinte script no <em>npm test</em>:</p><p><em>mocha — require ./it_test_helper.js — recursive ./test/**.test.js</em></p><p>O que acontece é o seguinte… Antes do <strong><em>mocha</em></strong> executar a suíte de testes, ele vai executar o arquivo<strong> <em>it_test_helper.js </em></strong>que está na raiz do projeto. E é nesse arquivo que nós startamos o nosso banco de dados em memória utilizando o <strong><em>mongo-unit</em></strong> e inserimos a <strong>url</strong> gerada em <strong>process.env.MONGO_URL</strong>.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e059d8e37a1e75de209793918a8e3ed4/href">https://medium.com/media/e059d8e37a1e75de209793918a8e3ed4/href</a></iframe><h4>Citando algumas bibliotecas/framework que utilizei:</h4><h3>Chai</h3><blockquote>Chai is a BDD / TDD assertion library for <a href="http://nodejs.org/">node</a> and the browser that can be delightfully paired with any javascript testing framework.</blockquote><h3>Mocha</h3><blockquote>Mocha is a feature-rich JavaScript test framework running on <a href="https://nodejs.org/">Node.js</a> and in the browser, making asynchronous testing <em>simple</em> and <em>fun</em>. Mocha tests run serially, allowing for flexible and accurate reporting, while mapping uncaught exceptions to the correct test cases. Hosted on <a href="https://github.com/mochajs/mocha">GitHub</a>.</blockquote><h3>Supertest</h3><blockquote>The motivation with this module is to provide a high-level abstraction for testing HTTP, while still allowing you to drop down to the <a href="https://visionmedia.github.io/superagent/">lower-level API</a> provided by superagent.</blockquote><h4>Vai uma dica:</h4><p><strong>Modularizar a nossa aplicação ExpressJS é muito importante!!!</strong></p><p>É extremamente importante definir nossa aplicação express em um arquivo separado, pois podemos reutilizar a instância do express, sendo usada pela biblioteca <strong>Supertest</strong> nos testes automatizados e também em <strong>produção/desenvolvimento</strong>.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6d5111f24ff89725ec9bcde6bbfbfaca/href">https://medium.com/media/6d5111f24ff89725ec9bcde6bbfbfaca/href</a></iframe><h4>Vamos definir agora o estado do nosso banco de dados:</h4><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/326fd020a7071566d3c93fa91ae1ef71/href">https://medium.com/media/326fd020a7071566d3c93fa91ae1ef71/href</a></iframe><h4><strong>Implementando nosso pequeno arquivo nosso Schema com mongoose:</strong></h4><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/bbb99eb6b1ee96ac6a620000e8c2d061/href">https://medium.com/media/bbb99eb6b1ee96ac6a620000e8c2d061/href</a></iframe><h4>Implementando nosso teste:</h4><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/337e3152f1b1499bd47cb3958b560a6e/href">https://medium.com/media/337e3152f1b1499bd47cb3958b560a6e/href</a></iframe><h4>Agora basta rodar npm test e ver o resultado do teste no terminal :)</h4><p>Vou deixar o repositório aqui embaixo, espero ter ajudado ;)</p><p><a href="https://github.com/bromatheuscruz/NodeJSMongoUnit">bromatheuscruz/NodeJSMongoUnit</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=3f9b099c468b" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>