<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Herminio Torres &#187; Gem</title>
	<atom:link href="http://www.herminiotorres.com/blog/category/gem/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.herminiotorres.com/blog</link>
	<description>Ruby helps programmers have more fun!</description>
	<lastBuildDate>Wed, 21 Jul 2010 22:32:32 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>RVM &#8211; The Ruby Version Manager</title>
		<link>http://www.herminiotorres.com/blog/2010/07/08/rvm-the-ruby-version-manager/</link>
		<comments>http://www.herminiotorres.com/blog/2010/07/08/rvm-the-ruby-version-manager/#comments</comments>
		<pubDate>Thu, 08 Jul 2010 12:27:22 +0000</pubDate>
		<dc:creator>Herminio</dc:creator>
				<category><![CDATA[Gem]]></category>
		<category><![CDATA[Geral]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[rvm]]></category>
		<category><![CDATA[gem]]></category>
		<category><![CDATA[install]]></category>

		<guid isPermaLink="false">http://www.herminiotorres.com/blog/?p=174</guid>
		<description><![CDATA[Instalando RVM Agora vamos ver como usar o RVM e o que podemos fazer com ele. RVM funciona em todos os sistemas *nix, se você tem um Linux/MacOSX ou FreeBSD, abra seu console e vamos começar com a instalação do RVM. O caminho recomendado do próprio desenvolvedor da gem é instalá-lo a partir do repositório [...]]]></description>
			<content:encoded><![CDATA[<p><strong>Instalando RVM</strong><br />
Agora vamos ver como usar o RVM e o que podemos fazer com ele. RVM funciona em todos os sistemas *nix, se você tem um Linux/MacOSX ou FreeBSD, abra seu console e vamos começar com a instalação do RVM. O caminho recomendado do próprio desenvolvedor da gem é instalá-lo a partir do repositório GitHub com o seguinte comando:</p>
<div class="codecolorer-container bash vibrant" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">$ <span style="color: #c20cb9; font-weight: bold;">mkdir</span> <span style="color: #660033;">-p</span> ~<span style="color: #000000; font-weight: bold;">/</span>.rvm<span style="color: #000000; font-weight: bold;">/</span>src<span style="color: #000000; font-weight: bold;">/</span><br />
$ <span style="color: #7a0874; font-weight: bold;">cd</span> ~<span style="color: #000000; font-weight: bold;">/</span>.rvm<span style="color: #000000; font-weight: bold;">/</span>src <span style="color: #000000; font-weight: bold;">&amp;&amp;</span> <span style="color: #c20cb9; font-weight: bold;">rm</span> <span style="color: #660033;">-rf</span> .<span style="color: #000000; font-weight: bold;">/</span>rvm<span style="color: #000000; font-weight: bold;">/</span><br />
$ <span style="color: #c20cb9; font-weight: bold;">git</span> clone <span style="color: #c20cb9; font-weight: bold;">git</span>:<span style="color: #000000; font-weight: bold;">//</span>github.com<span style="color: #000000; font-weight: bold;">/</span>wayneeseguin<span style="color: #000000; font-weight: bold;">/</span>rvm.git<br />
$ <span style="color: #7a0874; font-weight: bold;">cd</span> rvm<br />
$ .<span style="color: #000000; font-weight: bold;">/</span><span style="color: #c20cb9; font-weight: bold;">install</span></div></div>
<p>(<strong>Nota:</strong> é preciso que você tenha o git instalado, se você não tiver o git instalado então faça download em <a href="http://git-scm.com">http://git-scm.com</a> e instale o git de acordo com o seu sistema operacional). Então vamos para a próxima etapa.</p>
<p>Antes de iniciar a instalação do nosso rubies e gems, é garantindo que o nosso RVM está na ultima versão então digite o seguinte comando:</p>
<div class="codecolorer-container bash vibrant" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">$ rvm update –<span style="color: #c20cb9; font-weight: bold;">head</span></div></div>
<p>Certifique-se de ler e seguir todas as instruções emitidas pela instalação da linha acima. Certifique-se de novo para ativar o RVM shells, colocando a linha:</p>
<div class="codecolorer-container bash vibrant" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #ff0000;">'if [[ -s $HOME/.rvm/scripts/rvm ]] ; then source<br />
$HOME/.rvm/scripts/rvm ; fi'</span></div></div>
<p>No final do arquivo em <strong>~/.bash_profile</strong> e <strong>~/.bashrc</strong> assegurar que não há uma linha que termina com &#8216;<strong>&#038;&#038; return</strong>&#8216; no seu <strong>~/.bashrc</strong>.</p>
<p><strong>Instalando rubies</strong><br />
Perfeito, agora temos a versão mais recente do RVM instalado. Agora, a parte que realmente precisamos para desenvolver nossas próprias aplicações com Ruby. Nós podemos escolher agora várias possibilidades dos interpretadores de ruby que desejar instalar. A mais comumente utilizada é a instalação de um nível de patch específico, que é o padrão. Vamos instalar três intérpretes ruby, especificando suas versões (MRI ruby é interpretador padrão):</p>
<div class="codecolorer-container bash vibrant" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">$ rvm <span style="color: #c20cb9; font-weight: bold;">install</span> 1.8.6,1.8.7,1.9.1</div></div>
<p>Depois de executar este comando (e esperar por um tempo, dependendo da velocidade da CPU e da largura de banda da rede) devemos ter três interpretadores ruby instalado para cada um deles a última versão do patch. RVM obtém os níveis de patch padrão que são especificado no RVM &#8216;key=value&#8217; no arquivo <strong>~/.rvm/config/db</strong>, estas definições podem ser substituídas pelo usuário em  <strong>~/.rvm/config/user</strong>.<br />
Para ver os rubies instalados basta simplesmente digitar: <strong>rvm list</strong> ao qual devemos ver algo semelhante a:</p>
<div class="codecolorer-container bash vibrant" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">rvm Rubies<br />
ruby-1.8.6-p398 <span style="color: #7a0874; font-weight: bold;">&#91;</span> x86_64 <span style="color: #7a0874; font-weight: bold;">&#93;</span><br />
ruby-1.8.7-p249 <span style="color: #7a0874; font-weight: bold;">&#91;</span> x86_64 <span style="color: #7a0874; font-weight: bold;">&#93;</span><br />
ruby-1.9.1-p378 <span style="color: #7a0874; font-weight: bold;">&#91;</span> x86_64 <span style="color: #7a0874; font-weight: bold;">&#93;</span><br />
System Ruby<br />
system <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span></div></div>
<p><strong>Selecionando Rubies</strong><br />
Se quisermos usar o Ruby 1.8.6, nós simplesmente podemos selecionar em nosso shell atual, digitando <strong>rvm 1.8.6</strong>. Podemos então verificar que versão od ruby está sendo utilizada, digitando <strong>ruby -v</strong> e também podemos verificar em qual ambiente está, digitando <strong>rvm info</strong>. RVM opera em uma base per-shell para este ambiente só está ativo para o shell atual, se abrir um novo shell, em seguida, estaremos de volta ao ambiente do sistema, o que nos traz para &#8230;</p>
<p><strong>Definir um Ruby padrão</strong><br />
Se quisermos usar uma versão específica do ruby como padrão para todas as outras versões instaladas, digamos, por exemplo, 1.9.1, vamos definir o padrão digitando: <strong>rvm 1.9.1 &#8211;default</strong>. Então quando nos digitamos <strong>rvm list</strong> vemos agora:</p>
<div class="codecolorer-container bash vibrant" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">rvm Rubies<br />
ruby-1.8.6-p398 <span style="color: #7a0874; font-weight: bold;">&#91;</span> x86_64 <span style="color: #7a0874; font-weight: bold;">&#93;</span><br />
ruby-1.8.7-p249 <span style="color: #7a0874; font-weight: bold;">&#91;</span> x86_64 <span style="color: #7a0874; font-weight: bold;">&#93;</span><br />
=<span style="color: #000000; font-weight: bold;">&gt;</span> ruby-1.9.1-p378 <span style="color: #7a0874; font-weight: bold;">&#91;</span> x86_64 <span style="color: #7a0874; font-weight: bold;">&#93;</span><br />
Default Ruby <span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #000000; font-weight: bold;">for</span> new shells<span style="color: #7a0874; font-weight: bold;">&#41;</span><br />
ruby-1.9.1-p378 <span style="color: #7a0874; font-weight: bold;">&#91;</span> x86_64 <span style="color: #7a0874; font-weight: bold;">&#93;</span><br />
System Ruby<br />
system <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span></div></div>
<p>Agora toda vez que abrir um novo shell vamos encontrar <strong>ruby -v</strong> o RVM 1.9.1 instalado e a <strong>gem list</strong> e as gems que podem ser instaladas no interpretador do RVM 1.9.1.</p>
<p><strong>Ruby Gems</strong><br />
Com isso agora podemos instalar gems, e os pequenos pacotes que tanto precisamos, a fim de produzir o nosso código magnífico! Depois de selecionar uma versão do ruby com rvm 1.9.1, podemos instalar gems usando <strong>gem install <nome da gem></nome></strong> <em>(aviso prévio, sem sudo!)</em>. RVM configura seu ambiente de tal forma que as gems instaladas em um diretório separado para cada interpretador Ruby distintas. Isto significa que devemos instalar as gems para cara interpretador Ruby instalado que queremos com as gems. RVM fornece uma maneira fácil de instalar uma única gem para vários interpretadores: <strong>rvm 1.8.6,1.8.7 gem install ruby-debug</strong> irá instalar ruby-debug para ambas 1.8.6 e 1.8.7, enquanto <strong>rvm 1.9.1 gem install ruby-debug19</strong> irá instalar o ruby-debug19 só para o ruby RVM&#8217;s 1.9.1. Para instalar uma gem a todos os interpretadores simplesmente omitir os seletores: <strong>rvm gem install shoulda</strong>. </p>
<p><strong>(não)sudo</strong><br />
É muito importante para deixar o hábito de usar &#8216;<strong>sudo</strong>&#8216; para instalar gems. Quando <strong>sudo gem install x</strong> é usado <strong>gem install X</strong> é executado como o usuário root com a instalação raiz do ambiente e não no ambiente RVM cuidadosamente construída.</p>
<p><strong>Resumo</strong><br />
Este é um breve tutorial. Para maiores informações e documentação mais detalhada, visite o site do RVM (<a href="http://rvm.beginrescueend.com">http://rvm.beginrescueend.com</a>). </p>
<p><strong>Créditos</strong>: <a href="http://railsmagazine.com/issues/6">http://railsmagazine.com/issues/6</a> page 31. RVM – The Ruby Version Manager by Markus Dreier.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.herminiotorres.com/blog/2010/07/08/rvm-the-ruby-version-manager/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>RSpec &#8211; Objetos Mock com comportamento de Objetos Reais</title>
		<link>http://www.herminiotorres.com/blog/2009/08/25/rspec-objetos-mock-com-comportamento-de-objetos-reais/</link>
		<comments>http://www.herminiotorres.com/blog/2009/08/25/rspec-objetos-mock-com-comportamento-de-objetos-reais/#comments</comments>
		<pubDate>Tue, 25 Aug 2009 19:08:39 +0000</pubDate>
		<dc:creator>Herminio</dc:creator>
				<category><![CDATA[Gem]]></category>
		<category><![CDATA[Geral]]></category>
		<category><![CDATA[RSpec]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[Teste]]></category>
		<category><![CDATA[rspec spec teste ruby mock]]></category>

		<guid isPermaLink="false">http://www.herminiotorres.com/blog/?p=141</guid>
		<description><![CDATA[Objetos Mock com comportamento de Objetos Reais RSpec permite que você adicione o comportamento de objetos reais para objetos fictícios, para que possa definir as expectativas de mensagens ocultas e método em qualquer objeto em seu sistema. Uma utilização comum deste apoio é isolar exemplos do ActiveRecord e, portanto, o banco de dados em exemplos [...]]]></description>
			<content:encoded><![CDATA[<p><strong><a href="http://rspec.info/documentation/mocks/partial_mocks.html" target="_blank">Objetos Mock com comportamento de Objetos Reais</a></strong></p>
<p>	RSpec permite que você adicione o comportamento de objetos reais para objetos fictícios, para que possa definir as expectativas de mensagens ocultas e método em qualquer objeto em seu sistema.</p>
<p>	Uma utilização comum deste apoio é isolar exemplos do ActiveRecord e, portanto, o banco de dados em exemplos no Ruby on Rails.</p>
<div class="codecolorer-container ruby vibrant" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="ruby codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&nbsp; &nbsp; MyModel.<span style="color:#9900CC;">should_receive</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:find</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">with</span><span style="color:#006600; font-weight:bold;">&#40;</span>id<span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">and_return</span><span style="color:#006600; font-weight:bold;">&#40;</span>@mock_model_instance<span style="color:#006600; font-weight:bold;">&#41;</span></div></div>
<p>Controlar o comportamento dos métodos da classe de nível (por exemplo, tendo-lhes que devolver um objeto fictício, em vez de uma instância real da classe modelo) permite-lhe descrever seus controllers e views de forma isolada a partir da lógica de ocorrência de classes do seu model. Isso significa que você pode mudar as regras de validação de um modelo, por exemplo, e de unidade que nos exemplos modelos sem afetar o tratamento e exemplos de exibição.</p>
<p>	Isso também ajuda a manter o contexto do seu exemplo completamente em exibição (sem ter de olhar para fixtures/xyz.yml e ter que entender o que está acontecendo).</p>
]]></content:encoded>
			<wfw:commentRss>http://www.herminiotorres.com/blog/2009/08/25/rspec-objetos-mock-com-comportamento-de-objetos-reais/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>RSpec &#8211; Mocks and Stubs</title>
		<link>http://www.herminiotorres.com/blog/2009/08/25/rspec-mocks-and-stubs/</link>
		<comments>http://www.herminiotorres.com/blog/2009/08/25/rspec-mocks-and-stubs/#comments</comments>
		<pubDate>Tue, 25 Aug 2009 15:52:19 +0000</pubDate>
		<dc:creator>Herminio</dc:creator>
				<category><![CDATA[Gem]]></category>
		<category><![CDATA[Geral]]></category>
		<category><![CDATA[RSpec]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[Teste]]></category>
		<category><![CDATA[rspec spec teste ruby mock stub]]></category>

		<guid isPermaLink="false">http://www.herminiotorres.com/blog/?p=137</guid>
		<description><![CDATA[Mocks and Stubs Objetos Mock são objetos de dublê que dão maior controle declarativa sobre o seu comportamento no cursor da execução de um exemplo. Definindo mensagens de expectativas e métodos stubs de objetos permite que você especifique uma simulação como um objeto colaborar/corresponder com os outros antes desses objetos existirem. Você também pode usar [...]]]></description>
			<content:encoded><![CDATA[<p><strong><a href="http://rspec.info/documentation/mocks/" target="_blank">Mocks and Stubs</a></strong></p>
<p>	Objetos Mock são objetos de dublê que dão maior controle declarativa sobre o seu comportamento no cursor da execução de um exemplo. Definindo mensagens de expectativas e métodos stubs de objetos permite que você especifique uma simulação como um objeto colaborar/corresponder com os outros antes desses objetos existirem.</p>
<p>	Você também pode usar objetos dublê para isolar os seus exemplos de serviços que são complexos para configurar ou caros demais para serem executados e assim pode manter seu conjunto de exemplos funcionando rapidamente.</p>
<p>	RSpec possibilita ter objetos falsos, assim lhe permite criar objetos fictícios em seus exemplos de objetos mocks, tendo o mesmo comportamento de seus objetos existentes.</p>
<p>	<strong>Criando um objeto fictício</strong></p>
<p>	Você cria um objeto mock com o método de simulação:</p>
<div class="codecolorer-container ruby vibrant" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="ruby codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&nbsp; &nbsp; my_mock = mock<span style="color:#006600; font-weight:bold;">&#40;</span>name<span style="color:#006600; font-weight:bold;">&#41;</span></div></div>
<p>Isso cria uma nova simulação com esse name (uma string) e registra-lo. Quando terminar o exemplo, todas as simulações registradas são verificadas.</p>
<div class="codecolorer-container ruby vibrant" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="ruby codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&nbsp; &nbsp; my_mock = mock<span style="color:#006600; font-weight:bold;">&#40;</span>name, stubs_and_options<span style="color:#006600; font-weight:bold;">&#41;</span></div></div>
<p>Como acima, mas permite que você defina valores de retorno mínimo para determinadas mensagens e opções à ajustar a simulação do comportamento. Atualmente a única opção suportada é a seguinte :null_object. Definir isso como verdade instrui a ironizar a ignorar (baixo consumo) qualquer mensagem que não foi dita para esperar e retornar-se.</p>
<div class="codecolorer-container ruby vibrant" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="ruby codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&nbsp; &nbsp; my_mock = mock<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;blah&quot;</span>, <span style="color:#ff3333; font-weight:bold;">:null_object</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#0000FF; font-weight:bold;">true</span><span style="color:#006600; font-weight:bold;">&#41;</span></div></div>
<p><strong>Notas</strong><br />
	Há muitos pontos de vista diferentes sobre o significado de mocks e stubs. Se você estiver interessado em aprender mais, aqui estão algumas leituras recomendadas:</p>
<p>	• Mock Objects: <a href="http://www.mockobjects.com/" target="_blank">http://www.mockobjects.com/</a><br />
  • Endo-Testing: <a href="http://www.mockobjects.com/files/endotesting.pdf" target="_blank">http://www.mockobjects.com/files/endotesting.pdf</a><br />
  • Mock Roles, Not Objects: <a href="http://www.mockobjects.com/files/mockrolesnotobjects.pdf" target="_blank">http://www.mockobjects.com/files/mockrolesnotobjects.pdf</a><br />
  • Test Double Patterns: <a href="http://xunitpatterns.com/Test%20Double%20Patterns.html" target="_blank">http://xunitpatterns.com/Test%20Double%20Patterns.html</a><br />
  • Mocks aren’t stubs: <a href="http://www.martinfowler.com/articles/mocksArentStubs.html" target="_blank">http://www.martinfowler.com/articles/mocksArentStubs.html</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.herminiotorres.com/blog/2009/08/25/rspec-mocks-and-stubs/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>RSpec &#8211; Expectativas</title>
		<link>http://www.herminiotorres.com/blog/2009/08/25/rspec-expectativas/</link>
		<comments>http://www.herminiotorres.com/blog/2009/08/25/rspec-expectativas/#comments</comments>
		<pubDate>Tue, 25 Aug 2009 03:21:31 +0000</pubDate>
		<dc:creator>Herminio</dc:creator>
				<category><![CDATA[Gem]]></category>
		<category><![CDATA[Geral]]></category>
		<category><![CDATA[RSpec]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[Teste]]></category>
		<category><![CDATA[expectativas rspec spec ruby teste matchers expectations]]></category>

		<guid isPermaLink="false">http://www.herminiotorres.com/blog/?p=135</guid>
		<description><![CDATA[Spec::Expectations e Spec::Matchers Quando executa especificações do RSpec, que define #should e #should_not a cada objeto do sistema. Esses métodos são a sua entrada para a magia do RSpec. Veja Spec::Expectations e Spec::Matchers para mais informações.]]></description>
			<content:encoded><![CDATA[<p><a href="http://rspec.info/documentation/expectations.html" target="_blank">Spec::Expectations e Spec::Matchers</a></p>
<p>Quando executa especificações do RSpec, que define <strong>#should</strong> e <strong>#should_not</strong> a<br />
cada objeto do sistema. Esses métodos são a sua entrada para a magia do RSpec.</p>
<p>Veja <a href="http://rspec.rubyforge.org/rspec/1.2.8/classes/Spec/Expectations.html" target="_blank">Spec::Expectations</a> e <a href="http://rspec.rubyforge.org/rspec/1.2.8/classes/Spec/Matchers.html" target="_blank">Spec::Matchers</a> para mais informações.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.herminiotorres.com/blog/2009/08/25/rspec-expectativas/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>RSpec &#8211; Exemplos de código executável</title>
		<link>http://www.herminiotorres.com/blog/2009/08/24/rspec-exemplos-de-codigo-executavel/</link>
		<comments>http://www.herminiotorres.com/blog/2009/08/24/rspec-exemplos-de-codigo-executavel/#comments</comments>
		<pubDate>Mon, 24 Aug 2009 18:29:53 +0000</pubDate>
		<dc:creator>Herminio</dc:creator>
				<category><![CDATA[Gem]]></category>
		<category><![CDATA[Geral]]></category>
		<category><![CDATA[RSpec]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[Teste]]></category>
		<category><![CDATA[rspec spec teste ruby]]></category>

		<guid isPermaLink="false">http://www.herminiotorres.com/blog/?p=132</guid>
		<description><![CDATA[Exemplos de código executável RSpec fornece uma Domain Specific Language(DSL) com o qual você pode expressar exemplos executável do comportamento esperado de seu código. Imagine que você estivesse falando com um cliente solicitando o software para o seu banco. Parte da conversa que bem poderia ser assim: &#160; &#160; Você: Descrever uma conta quando ela [...]]]></description>
			<content:encoded><![CDATA[<p><strong>Exemplos de código executável</strong></p>
<p>	<a href="http://rspec.info/documentation/" target="_blank">RSpec</a> fornece uma <a href="http://blog.caelum.com.br/2008/12/30/dsls-nao-sao-para-gerentes/" target="_blank">Domain Specific Language</a>(DSL) com o qual você pode expressar exemplos executável do comportamento esperado de seu código.</p>
<p>	Imagine que você estivesse falando com um cliente solicitando o software para o seu banco. Parte da conversa que bem poderia ser assim:</p>
<div class="codecolorer-container ruby vibrant" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="ruby codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&nbsp; &nbsp; Você: Descrever uma conta quando ela é criada pela primeira vez.<br />
&nbsp; &nbsp; <span style="color:#9900CC;">Cliente</span>: Ele deve ter um saldo de $<span style="color:#006666;">0</span>.</div></div>
<p>Veja como podemos expressar essa conversa de RSpec:</p>
<div class="codecolorer-container ruby vibrant" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="ruby codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&nbsp; &nbsp; describe Account, <span style="color:#996600;">&quot;when first created&quot;</span> <span style="color:#9966CC; font-weight:bold;">do</span><br />
&nbsp; &nbsp; it <span style="color:#996600;">&quot;should have a balance of $0&quot;</span> <span style="color:#9966CC; font-weight:bold;">do</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; …<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">end</span><br />
&nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">end</span></div></div>
<p>O <strong>describe()</strong> é um método que retorna uma classe ExampleGroup, que contém um conjunto de exemplos de um comportamento específico do sistema que pretende descrever.</p>
<p>	O <strong>it()</strong> é um método que retorna uma instância da ExampleGroup em que esse exemplo é executado.</p>
<p>	Quando você executar esse exemplo, RSpec pode fornecer um relatório como este:</p>
<div class="codecolorer-container ruby vibrant" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="ruby codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&nbsp; &nbsp; Conta, quando criada pela primeira vez<br />
&nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">-</span> Deve ter um saldo de $<span style="color:#006666;">0</span></div></div>
<p><strong>before() e after()</strong></p>
<p>	Você pode usar <strong>before()</strong> e/ou <strong>after()</strong> para definir um código que é executado antes e depois de cada exemplo, ou apenas uma vez por exampleGroup:</p>
<div class="codecolorer-container ruby vibrant" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="ruby codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&nbsp; &nbsp; describe Thing <span style="color:#9966CC; font-weight:bold;">do</span><br />
&nbsp; &nbsp; &nbsp; before<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:all</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#008000; font-style:italic;"># Este é executado apenas uma única vez, antes de todos os exemplos</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#008000; font-style:italic;"># e antes de qualquer blocks before(:each)</span><br />
&nbsp; &nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">end</span><br />
<br />
&nbsp; &nbsp; &nbsp; before<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:each</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#008000; font-style:italic;"># Este é executado antes de cada exemplo</span><br />
&nbsp; &nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">end</span><br />
<br />
&nbsp; &nbsp; &nbsp; before <span style="color:#9966CC; font-weight:bold;">do</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#008000; font-style:italic;"># :each é o padrão, então este é o mesmo que before(:each)</span><br />
&nbsp; &nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">end</span><br />
<br />
&nbsp; &nbsp; &nbsp; it <span style="color:#996600;">&quot;should do stuff&quot;</span> <span style="color:#9966CC; font-weight:bold;">do</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#008000; font-style:italic;"># deve fazer coisas</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; …<br />
&nbsp; &nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">end</span><br />
<br />
&nbsp; &nbsp; &nbsp; it <span style="color:#996600;">&quot;should do more stuff&quot;</span> <span style="color:#9966CC; font-weight:bold;">do</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#008000; font-style:italic;"># deve fazer mais coisas</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; …<br />
&nbsp; &nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">end</span><br />
<br />
&nbsp; &nbsp; &nbsp; after<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:each</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#008000; font-style:italic;"># isto é, após cada exemplo</span><br />
&nbsp; &nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">end</span><br />
<br />
&nbsp; &nbsp; &nbsp; after <span style="color:#9966CC; font-weight:bold;">do</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#008000; font-style:italic;"># :each é o padrão, então este é o mesmo que after(:each)</span><br />
&nbsp; &nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">end</span><br />
<br />
&nbsp; &nbsp; &nbsp; after<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:all</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#008000; font-style:italic;"># Este é executado apenas uma única vez depois de todos os exemplos</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#008000; font-style:italic;"># E depois de qualquer blocks after(:each)</span><br />
&nbsp; &nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">end</span><br />
&nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">end</span></div></div>
<p><strong>Atenção:</strong> A utilização do before(:all) e after(:all) é desencorajada porque ela introduz dependências entre os exemplos. Mesmo assim, pode ser útil para operações se você sabe o que está fazendo.</p>
<p><strong>Métodos Helper</strong></p>
<p>	Você pode escrever métodos helper(auxiliares) diretamente dentro de um Exemple Group:</p>
<div class="codecolorer-container ruby vibrant" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="ruby codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&nbsp; &nbsp; describe <span style="color:#996600;">&quot;…&quot;</span> <span style="color:#9966CC; font-weight:bold;">do</span><br />
&nbsp; &nbsp; &nbsp; it <span style="color:#996600;">&quot;…&quot;</span> <span style="color:#9966CC; font-weight:bold;">do</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; helper_method<br />
&nbsp; &nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">end</span><br />
<br />
&nbsp; &nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">def</span> helper_method<br />
&nbsp; &nbsp; &nbsp; &nbsp; …<br />
&nbsp; &nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">end</span><br />
&nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">end</span></div></div>
<p><strong>Métodos Helper Reutilizáveis</strong></p>
<p>	Você pode incluir métodos helper em vários ExampleGroups, basta colocar dentro de um módulo e em seguida, incluindo um módulo em seu exampleGroup:</p>
<div class="codecolorer-container ruby vibrant" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="ruby codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">module</span> AccountExampleHelperMethods<br />
&nbsp; &nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">def</span> helper_method<br />
&nbsp; &nbsp; &nbsp; &nbsp; …<br />
&nbsp; &nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">end</span><br />
&nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">end</span><br />
<br />
&nbsp; &nbsp; describe <span style="color:#996600;">&quot;A new account&quot;</span> <span style="color:#9966CC; font-weight:bold;">do</span><br />
&nbsp; &nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">include</span> AccountExampleHelperMethods<br />
&nbsp; &nbsp; &nbsp; before <span style="color:#9966CC; font-weight:bold;">do</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0066ff; font-weight:bold;">@account</span> = Account.<span style="color:#9900CC;">new</span><br />
&nbsp; &nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">end</span><br />
<br />
&nbsp; &nbsp; &nbsp; it <span style="color:#996600;">&quot;should have a balance of $0&quot;</span> <span style="color:#9966CC; font-weight:bold;">do</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; helper_method<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0066ff; font-weight:bold;">@account</span>.<span style="color:#9900CC;">balance</span>.<span style="color:#9900CC;">should</span> eql<span style="color:#006600; font-weight:bold;">&#40;</span>Money.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;">0</span>, <span style="color:#ff3333; font-weight:bold;">:dollars</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">end</span><br />
&nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">end</span></div></div>
<p>Exemplo Grupos Compartilhados</p>
<p>	Você pode criar exemplo de grupos compartilhados e incluir os grupos em outros grupos.</p>
<p>	Suponha que você tenha algum tipo de comportamento que se aplica a todas as edições do seu produto, grandes e pequenos.</p>
<p>	Primeiro comportamento do fator de &#8220;compartilhamento&#8221;:</p>
<div class="codecolorer-container ruby vibrant" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="ruby codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&nbsp; &nbsp; shared_examples_for <span style="color:#996600;">&quot;all editions&quot;</span> <span style="color:#9966CC; font-weight:bold;">do</span><br />
&nbsp; &nbsp; &nbsp; it <span style="color:#996600;">&quot;should behave like all editions&quot;</span> <span style="color:#9966CC; font-weight:bold;">do</span><br />
&nbsp; &nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">end</span><br />
&nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">end</span></div></div>
<p>Então, quando você precisa definir o comportamento para as grandes e pequenas edições,<br />
	referência o comportamento compartilhado usando o método it_should_behave_like ()</p>
<div class="codecolorer-container ruby vibrant" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="ruby codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&nbsp; &nbsp; describe <span style="color:#996600;">&quot;SmallEdition&quot;</span> <span style="color:#9966CC; font-weight:bold;">do</span><br />
&nbsp; &nbsp; &nbsp; it_should_behave_like <span style="color:#996600;">&quot;all editions&quot;</span><br />
<br />
&nbsp; &nbsp; &nbsp; it <span style="color:#996600;">&quot;should also behave like a small edition&quot;</span> <span style="color:#9966CC; font-weight:bold;">do</span><br />
&nbsp; &nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">end</span><br />
&nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">end</span><br />
<br />
&nbsp; &nbsp; describe <span style="color:#996600;">&quot;LargeEdition&quot;</span> <span style="color:#9966CC; font-weight:bold;">do</span><br />
&nbsp; &nbsp; &nbsp; it_should_behave_like <span style="color:#996600;">&quot;all editions&quot;</span><br />
<br />
&nbsp; &nbsp; &nbsp; it <span style="color:#996600;">&quot;should also behave like a large edition&quot;</span> <span style="color:#9966CC; font-weight:bold;">do</span><br />
&nbsp; &nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">end</span><br />
&nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">end</span></div></div>
<p>it_should_behave_like irá procurar um ExampleGroup pela sua descrição, neste caso, &#8220;all editions&#8221;</p>
<p>	Todas abaixo são incluídas em um grupo compartilhado:<br />
		* before(:all)<br />
    * before(:each)<br />
    * after(:each)<br />
    * after(:all)<br />
    * all included modules<br />
    * all methods</p>
<p>	Exemplos de grupos compartilhados não podem extend(estender) as classes.</p>
<p>	Vários exemplos grupos compartilhados pode ser referenciado em um grupo não compartilhado.</p>
<p>	Exemplos de grupos compartilhados podem ser incluídos em outros grupos compartilhados:</p>
<div class="codecolorer-container ruby vibrant" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="ruby codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&nbsp; &nbsp; shared_examples_for <span style="color:#996600;">&quot;All Employees&quot;</span> <span style="color:#9966CC; font-weight:bold;">do</span><br />
&nbsp; &nbsp; &nbsp; it <span style="color:#996600;">&quot;should be payable&quot;</span> <span style="color:#9966CC; font-weight:bold;">do</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0066ff; font-weight:bold;">@employee</span>.<span style="color:#9900CC;">should</span> respond_to<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:calculate_pay</span><span style="color:#006600; font-weight:bold;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">end</span><br />
&nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">end</span><br />
<br />
&nbsp; &nbsp; shared_examples_for <span style="color:#996600;">&quot;All Managers&quot;</span> <span style="color:#9966CC; font-weight:bold;">do</span><br />
&nbsp; &nbsp; &nbsp; it_should_behave_like <span style="color:#996600;">&quot;All Employees&quot;</span><br />
<br />
&nbsp; &nbsp; &nbsp; it <span style="color:#996600;">&quot;should be bonusable&quot;</span> <span style="color:#9966CC; font-weight:bold;">do</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0066ff; font-weight:bold;">@employee</span>.<span style="color:#9900CC;">should</span> respond_to<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:apply_bonus</span><span style="color:#006600; font-weight:bold;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">end</span><br />
&nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">end</span><br />
<br />
&nbsp; &nbsp; describe Officer <span style="color:#9966CC; font-weight:bold;">do</span><br />
&nbsp; &nbsp; &nbsp; before<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:each</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0066ff; font-weight:bold;">@employee</span> = Officer.<span style="color:#9900CC;">new</span><br />
&nbsp; &nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">end</span><br />
<br />
&nbsp; &nbsp; &nbsp; it_should_behave_like <span style="color:#996600;">&quot;All Managers&quot;</span><br />
<br />
&nbsp; &nbsp; &nbsp; it <span style="color:#996600;">&quot;should be optionable&quot;</span> <span style="color:#9966CC; font-weight:bold;">do</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#0066ff; font-weight:bold;">@employee</span>.<span style="color:#9900CC;">should</span> respond_to<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:grant_options</span><span style="color:#006600; font-weight:bold;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">end</span><br />
&nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">end</span><br />
<br />
<br />
&nbsp; &nbsp; $ spec officer_spec.<span style="color:#9900CC;">rb</span><br />
&nbsp; &nbsp; Officer<br />
&nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">-</span> should be payable<br />
&nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">-</span> should be bonusable<br />
&nbsp; &nbsp; <span style="color:#006600; font-weight:bold;">-</span> should be optionable</div></div>
<p><strong>Exemplos Pendentes</strong></p>
<p>	Há três formas de marcar um exemplo como &#8220;pendente&#8221;.<br />
	<strong>Deixar o bloco:</strong></p>
<div class="codecolorer-container ruby vibrant" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="ruby codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&nbsp; &nbsp; it <span style="color:#996600;">&quot;should say foo&quot;</span></div></div>
<p>A saída será dizer pendente (ainda não implementado).<br />
	<strong>Use o método pendente com nenhum bloco:</strong></p>
<div class="codecolorer-container ruby vibrant" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="ruby codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&nbsp; &nbsp; it <span style="color:#996600;">&quot;should say foo&quot;</span> <span style="color:#9966CC; font-weight:bold;">do</span><br />
&nbsp; &nbsp; &nbsp; pending<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;get the vocal chords working&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; subject.<span style="color:#9900CC;">should</span> say<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;foo&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span><br />
&nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">end</span></div></div>
<p>A saída será dizer PENDENTES (obter as cordas vocais de trabalho).<br />
	<strong>Use o método pendente com um bloco:</strong></p>
<div class="codecolorer-container ruby vibrant" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="ruby codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&nbsp; &nbsp; it <span style="color:#996600;">&quot;should say foo&quot;</span> <span style="color:#9966CC; font-weight:bold;">do</span><br />
&nbsp; &nbsp; &nbsp; pending<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;get the vocal chords working&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; subject.<span style="color:#9900CC;">should</span> say<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;foo&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">end</span><br />
&nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">end</span></div></div>
<p>A saída será dizer PENDENTES (obter as cordas vocais de trabalho), e o exemplo falhará dizendo-lhe para que seja corrigido logo o bloco e não gera nenhum erro.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.herminiotorres.com/blog/2009/08/24/rspec-exemplos-de-codigo-executavel/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>RSpec &#8211; O Inicio, como testar com rspec.</title>
		<link>http://www.herminiotorres.com/blog/2009/08/24/rspec-o-inicio-como-testar-com-rspec/</link>
		<comments>http://www.herminiotorres.com/blog/2009/08/24/rspec-o-inicio-como-testar-com-rspec/#comments</comments>
		<pubDate>Mon, 24 Aug 2009 16:39:32 +0000</pubDate>
		<dc:creator>Herminio</dc:creator>
				<category><![CDATA[Gem]]></category>
		<category><![CDATA[Geral]]></category>
		<category><![CDATA[RSpec]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[Teste]]></category>
		<category><![CDATA[rspec spec gem teste ruby rubygems]]></category>

		<guid isPermaLink="false">http://www.herminiotorres.com/blog/?p=105</guid>
		<description><![CDATA[Primeiramente vamos instalar Gem Spec. $ gem install rspec Agora vamos criar a nossa estrutura onde vamos trabalhar um exemplo simples. $ mkdir bowling $ cd bowling $ mkdir spec $ mkdir lib Comece com um exemplo muito simples que expressa algum comportamento básico desejado. # vamos criar um arquivo chamado bowling_spec.rb # dentro pasta [...]]]></description>
			<content:encoded><![CDATA[<p style="text-align: justify;">Primeiramente vamos instalar <a href="http://wiki.github.com/dchelimsky/rspec/install" target="_blank">Gem Spec</a>.</p>
<div class="codecolorer-container bash vibrant" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">$ gem <span style="color: #c20cb9; font-weight: bold;">install</span> rspec</div></div>
</p>
<p style="text-align: justify;">Agora vamos criar a nossa estrutura onde vamos trabalhar um <a href="http://rspec.info/" target="_blank">exemplo simples</a>.</p>
<div class="codecolorer-container bash vibrant" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">$ <span style="color: #c20cb9; font-weight: bold;">mkdir</span> bowling<br />
$ <span style="color: #7a0874; font-weight: bold;">cd</span> bowling<br />
$ <span style="color: #c20cb9; font-weight: bold;">mkdir</span> spec<br />
$ <span style="color: #c20cb9; font-weight: bold;">mkdir</span> lib</div></div>
</p>
<p style="text-align: justify;">Comece com um exemplo muito simples que expressa algum comportamento básico desejado.</p>
<div class="codecolorer-container ruby vibrant" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="ruby codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color:#008000; font-style:italic;"># vamos criar um arquivo chamado bowling_spec.rb</span><br />
<span style="color:#008000; font-style:italic;"># dentro pasta spec que criamos agora a pouco</span><br />
<span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'rubygems'</span><br />
<span style="color:#008000; font-style:italic;"># o require a rubygems é para que eu possa</span><br />
<span style="color:#008000; font-style:italic;"># chamadar a gem spec e testar minha aplicação.</span><br />
<span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'spec'</span><br />
<span style="color:#008000; font-style:italic;"># e este require é do meu arquivo de boliche</span><br />
<span style="color:#008000; font-style:italic;"># onde vou tentar validar seu código, com testes.</span><br />
<span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'lib/bowling'</span><br />
<br />
<span style="color:#008000; font-style:italic;"># aqui é onde vamos descrever um boliche</span><br />
describe Bowling <span style="color:#9966CC; font-weight:bold;">do</span><br />
<br />
&nbsp; &nbsp; <span style="color:#008000; font-style:italic;"># vamos descrever um comportamento</span><br />
&nbsp; &nbsp; <span style="color:#008000; font-style:italic;"># deve ter 0 escore na calha do jogo</span><br />
&nbsp; &nbsp; it <span style="color:#996600;">&quot;should score 0 for gutter game&quot;</span> <span style="color:#9966CC; font-weight:bold;">do</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#008000; font-style:italic;"># cria um novo objeto</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; bowling = Bowling.<span style="color:#9900CC;">new</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#008000; font-style:italic;"># passa o valor 0 pinos para o método hit</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#006666;">20</span>.<span style="color:#9900CC;">times</span> <span style="color:#006600; font-weight:bold;">&#123;</span> bowling.<span style="color:#9900CC;">hit</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#006666;">0</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#008000; font-style:italic;"># aqui ele diz: score deveria ser igual a 0</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; bowling.<span style="color:#9900CC;">score</span>.<span style="color:#9900CC;">should</span> == <span style="color:#006666;">0</span><br />
&nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">end</span><br />
<br />
<span style="color:#9966CC; font-weight:bold;">end</span></div></div>
</p>
<p style="text-align: justify;">Vamos executar o <a href="http://pt.wikipedia.org/wiki/Teste_de_software" target="_blank">teste</a> e vê-lo falhar.</p>
<div class="codecolorer-container bash vibrant" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">$ spec spec<span style="color: #000000; font-weight: bold;">/</span>bowling_spec.rb <span style="color: #660033;">--format</span> specdoc<br />
.<span style="color: #000000; font-weight: bold;">/</span>spec<span style="color: #000000; font-weight: bold;">/</span>bowling_spec.rb:<span style="color: #000000;">11</span>: uninitialized constant Bowling</div></div>
</p>
<p style="text-align: justify;">Agora, vamos escrever o código apenas o suficiente para fazê-lo passar.</p>
<div class="codecolorer-container ruby vibrant" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="ruby codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color:#008000; font-style:italic;"># bowling - boliche</span><br />
<br />
<span style="color:#9966CC; font-weight:bold;">class</span> Bowling<br />
<br />
&nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">def</span> hit<span style="color:#006600; font-weight:bold;">&#40;</span>pins<span style="color:#006600; font-weight:bold;">&#41;</span><br />
&nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">end</span><br />
<br />
&nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">def</span> score<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#006666;">0</span><br />
&nbsp; &nbsp; <span style="color:#9966CC; font-weight:bold;">end</span><br />
<br />
<span style="color:#9966CC; font-weight:bold;">end</span></div></div>
</p>
<p style="text-align: justify;">Execute o exemplo novamente e veja o teste passar.</p>
<div class="codecolorer-container bash vibrant" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">$ spec spec<span style="color: #000000; font-weight: bold;">/</span>bowling_spec.rb <span style="color: #660033;">--format</span> specdoc<br />
<br />
Bowling<br />
- should score <span style="color: #000000;">0</span> <span style="color: #000000; font-weight: bold;">for</span> gutter game<br />
<br />
Finished <span style="color: #000000; font-weight: bold;">in</span> <span style="color: #000000;">0.002854</span> seconds<br />
<br />
<span style="color: #000000;">1</span> example, <span style="color: #000000;">0</span> failures</div></div>
</p>
<p style="text-align: justify;">Tomar medidas pequenas(<a href="http://www.improveit.com.br/xp/principios/passos_bebe" target="_blank">baby-steps</a>)<br />
Não se apresse em escrever muito código. Em vez disso, adicionar outro exemplo, e deixá-lo guiar dizendo o que você tem que fazer a seguir. E não se esqueça de ter tempo para <a href="http://www.improveit.com.br/xp/praticas/refatoracao" target="_blank">refatorar</a> seu código antes que ele fique confuso. Você deve manter seu código limpo, a cada passo dado no seu caminho.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.herminiotorres.com/blog/2009/08/24/rspec-o-inicio-como-testar-com-rspec/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
