<?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 Mariano Sosto on Medium]]></title>
        <description><![CDATA[Stories by Mariano Sosto on Medium]]></description>
        <link>https://medium.com/@sostomariano?source=rss-ccfcbf665655------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*jcMzwB0UckOrF1CEwXBI4g@2x.jpeg</url>
            <title>Stories by Mariano Sosto on Medium</title>
            <link>https://medium.com/@sostomariano?source=rss-ccfcbf665655------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Wed, 02 Dec 2026 03:15:03 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@sostomariano/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[Go: How to unit test factory methods]]></title>
            <link>https://sostomariano.medium.com/go-how-to-unit-test-factory-methods-908b50fd38e8?source=rss-ccfcbf665655------2</link>
            <guid isPermaLink="false">https://medium.com/p/908b50fd38e8</guid>
            <category><![CDATA[golang-development]]></category>
            <category><![CDATA[go]]></category>
            <category><![CDATA[golang-tutorial]]></category>
            <category><![CDATA[golang]]></category>
            <category><![CDATA[testing]]></category>
            <dc:creator><![CDATA[Mariano Sosto]]></dc:creator>
            <pubDate>Wed, 19 May 2021 02:31:38 GMT</pubDate>
            <atom:updated>2021-05-19T14:47:38.542Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/400/0*RewVdxFUkvNoPW11.png" /></figure><p>Let’s first clarify what we refer to when saying <em>factory methods.</em> A factory method in Go is a method which creates some object and returns it, no more than that.<br>Let’s see the following example:</p><pre>type EmployeeService struct {<br>   Version                string<br>   saveEmployee           saveEmployeeRepository // type func<br>   getEmployee            getEmployeeRepository // type func<br>   updateAddress          updateAddressRepository // type interface<br>}</pre><pre>type saveEmployeeRepository func(employee Employee) error<br>type getEmployeeRepository  func(employeeId string) (Employee, error)</pre><pre>type updateAddressRepository interface{ <br>     update(address Address) error<br>}</pre><pre>// Factory Method<br>func NewEmployeeService() *EmployeeService {<br>   return &amp;EmployeeService{<br>      saveEmployee: func(employee Employee) error {<br>                 return db.Save(employee.Id,employee)<br>      },<br>      getEmployee: func(employeeId string) (Employee, error) {<br>                 emp,err := db.Get(employee.Id)<br>                 if err == nil {<br>                     return parseEmployee(emp),nil<br>                 }<br>                 return Employee{},err<br>      },<br>      updateAddress: AdressRepository{},</pre><pre>   }<br>}</pre><p>So .. What do we have here ? It’s a struct which has 2 string fields, 2 func fields and 1 interface field.</p><p>What is it that we want to assert on the output of this method ? We want to check that all the fields of EmployeeServiceare being set. Unfortunately, we can’t distinguish that for those fields of primitive types such as (string,int64,float64,etc), but, for those fields which accept null ( in this example funcand interface) we can check that.<br>In order to do that, we are going to use package reflect.</p><pre>func Test_NewEmployeeService(t *testing.T) {<br>   instance := NewEmployeeService()<br>   typeOf := reflect.TypeOf(instance)<br>   for i := 0; i &lt; typeOf.NumField(); i++ {<br>      field := typeOf.Field(i)<br>      if field.Type.Kind() == reflect.<em>Func </em>|| (field.Type.Kind() == reflect.<em>Interface</em>) {<br>         fieldValue := reflect.ValueOf(instance).FieldByName(field.Name)<br>         if fieldValue.IsNil() {<br>            assert.Fail(t, &quot;Missing value for field: &quot;+field.Name)<br>         }<br><br>      }<br>   }<br>}</pre><p>What we are doing in the above code is the following. Through reflection, we are inspecting all the fields on the output of NewEmploteeService() method, and then evaluating each of the fields. If the field is of type func or interface, then we validate if it is null or not.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=908b50fd38e8" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>