<?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 Marcelo Veloso on Medium]]></title>
        <description><![CDATA[Stories by Marcelo Veloso on Medium]]></description>
        <link>https://medium.com/@sid_marcelo?source=rss-b66e27a1d00e------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*IjoS1IYvKAtuztOg.jpg</url>
            <title>Stories by Marcelo Veloso on Medium</title>
            <link>https://medium.com/@sid_marcelo?source=rss-b66e27a1d00e------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 24 Nov 2026 23:20:33 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@sid_marcelo/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[Testando o Método Create de um Controller com Nested Attributes]]></title>
            <link>https://medium.com/@sid_marcelo/testando-o-m%C3%A9todo-create-de-um-controller-com-nested-attributes-fd3e8ee3781f?source=rss-b66e27a1d00e------2</link>
            <guid isPermaLink="false">https://medium.com/p/fd3e8ee3781f</guid>
            <category><![CDATA[rspec]]></category>
            <category><![CDATA[ruby-on-rails]]></category>
            <category><![CDATA[tdd]]></category>
            <dc:creator><![CDATA[Marcelo Veloso]]></dc:creator>
            <pubDate>Tue, 15 Aug 2017 02:37:41 GMT</pubDate>
            <atom:updated>2017-08-15T02:43:00.636Z</atom:updated>
            <content:encoded><![CDATA[<p>Estava eu essa semana tentando terminar os scripts de teste de um projeto. Tudo estava dando certo, terminei de cobrir todos os testes dos <em>models</em> e comecei a trabalhar nos <em>controllers</em>, até que me deparei com o teste do método <em>create</em> de um <em>controller</em> que utiliza <em>nested_attributes</em> para cadastro de um <em>model</em> e suas associações. A estrutura dos <em>models</em> e <em>controller</em> é semelhante a essa:</p><pre><em># app/models/author.rb</em><br><strong>class</strong> <strong>Author</strong> <strong>&lt;</strong> ActiveRecord<strong>::</strong>Base<br>  has_many :posts, dependent: :destroy</pre><pre>  accepts_nested_attributes_for :posts, allow_destroy: <strong>true</strong></pre><pre>  .<strong>.</strong>.<br><strong>end</strong></pre><pre><em># app/models/post.rb</em><br><strong>class</strong> <strong>Post</strong> <strong>&lt;</strong> ActiveRecord<strong>::</strong>Base<br>  belongs_to :author</pre><pre>  .<strong>.</strong>.<br><strong>end</strong></pre><pre><em># app/controllers/authors_controller.rb</em><br><strong>class</strong> <strong>AuthorsController</strong> <strong>&lt;</strong> ApplicationController<br>  .<strong>.</strong>.</pre><pre>  <strong>def</strong> create<br>    @author <strong>=</strong> Author.<strong>new</strong> author_params</pre><pre>    <strong>if</strong> @author.<strong>save</strong><br>      flash[:success] <strong>=</strong> &#39;Autor cadastrado com sucesso!&#39;<br>      redirect_to @author<br>    <strong>else</strong><br>      render &#39;new&#39;<br>    <strong>end</strong><br>  <strong>end</strong></pre><pre>  .<strong>.</strong>.</pre><pre>  <strong>private</strong></pre><pre>  <strong>def</strong> <strong>author_params</strong><br>    params.<strong>require</strong>(:author).<strong>permit</strong>(:name, posts_attributes: [<br>                                            :title, :content,<br>                                            :id, :_destroy<br>                                          ])<br>  <strong>end</strong></pre><pre>  .<strong>.</strong>.<br><strong>end</strong></pre><p>Eu utilizo FactoryGirl para facilitar meus testes, a principio pesquisei por algum método mágico dessa gema que convertesse a Hash dos atributos exatamente para o aceito pelo parâmetro do controller (com <em>nested_attributes</em>). Passei longos 30 minutos e não achei nada, então resolvi fazer a seguinte gambiarra no script de teste:</p><pre>require &#39;rails_helper&#39;</pre><pre>RSpec.<strong>describe</strong> AuthorsController, type: :controller <strong>do</strong><br>  .<strong>.</strong>.</pre><pre>  <strong>describe</strong> &#39;POST #create&#39; <strong>do</strong><br>    context &#39;with valid attributes&#39; <strong>do</strong><br>      let!(author_params) <strong>do</strong><br>        FactoryGirl.<strong>attributes_for</strong>(:author)<br>                   .<strong>merge</strong>(posts_attributes: [FactoryGirl.<strong>attributes_for</strong>(:post)])<br>      <strong>end</strong></pre><pre>      it &#39;creates a new author&#39; <strong>do</strong><br>        expect {<br>          post :create, author: author_params<br>        }.<strong>to</strong> change { Author.<strong>count</strong> }.<strong>by</strong>(1)<br>      <strong>end</strong></pre><pre>      it &#39;redirects to :show view&#39; <strong>do</strong><br>        post :create, author: author_params<br>        expect(response).<strong>to</strong> redirect_to(Author.<strong>last</strong>)<br>      <strong>end</strong><br>    <strong>end</strong></pre><pre>    .<strong>.</strong>.<br>  <strong>end</strong></pre><pre>  .<strong>.</strong>.<br><strong>end</strong></pre><p>Acredito que este não seja a forma mais correta de testar esse tipo de situação, mas pra mim deu certo.</p><p>leia mais em: <a href="http://www.marceloperini.com.br/">www.marceloperini.com.br</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=fd3e8ee3781f" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>