<?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: Rajmond Burgaj</title>
    <description>The latest articles on DEV Community by Rajmond Burgaj (@rajmondburgaj).</description>
    <link>https://dev.to/rajmondburgaj</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%2F92413%2F3f90074a-5641-41f7-a812-046362fbc5d7.jpg</url>
      <title>DEV Community: Rajmond Burgaj</title>
      <link>https://dev.to/rajmondburgaj</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/rajmondburgaj"/>
    <language>en</language>
    <item>
      <title>How to setup methods which return a built-in type and implement IEnumerable interface in Moq ?</title>
      <dc:creator>Rajmond Burgaj</dc:creator>
      <pubDate>Mon, 10 Sep 2018 11:59:29 +0000</pubDate>
      <link>https://dev.to/rajmondburgaj/how-to-setup-methods-which-return-a-built-int-type-and-implement-ienumerable-interface-in-moq--20of</link>
      <guid>https://dev.to/rajmondburgaj/how-to-setup-methods-which-return-a-built-int-type-and-implement-ienumerable-interface-in-moq--20of</guid>
      <description>

&lt;p&gt;Recently I was trying to make some unit testing and at some point I realized something was going wrong with my design but at the same time could not change it. So, the idea is I am using entity framework and executing stored procedures using this framework. All these procedures were encapsulated inside a service called &lt;code&gt;IProcedureService&lt;/code&gt;.So, a normal method would look like this:&lt;/p&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;DbRawSqlQuery&amp;lt;UserVesselPermissionsResult&amp;gt; GetUserVesselPermissions(Guid userId, DateTime date);
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;DbRawSqlQuery&lt;/code&gt; is an class from entity framework which I can not modify. The question for me was, how I am going to make this &lt;code&gt;Moq&lt;/code&gt; setup code work:&lt;/p&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;_procedureService.Setup(x =&amp;gt; x.GetUserVesselPermissions(It.IsAny&amp;lt;Guid&amp;gt;(), It.IsAny&amp;lt;DateTime&amp;gt;()))
        .Returns((DbRawSqlQuery&amp;lt;UserVesselPermissionsResult&amp;gt;) /*...what*/);
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The real problem was due to class itself having only one internal constructor which could not be instantiated in &lt;code&gt;Moq&lt;/code&gt;. That's because &lt;code&gt;Moq&lt;/code&gt; can not instantiate classes only with internal constructor. Further more this class could have constructors with some parameters which I can not supply at this point(&lt;em&gt;an example could be a DbConnection or something similar&lt;/em&gt;). In this case I tried to decompile the source and I could see another class named &lt;code&gt;DbSqlQuery&lt;/code&gt; which was inheriting from &lt;code&gt;DbRawSqlQuery&lt;/code&gt; and provided a normal constructor besides the internal one. At this point, I found an idea how to make this work. The idea was to create a new class and inherit from &lt;code&gt;DbSqlQuery&lt;/code&gt;. My class now would look like this:&lt;/p&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public class TestDbSqlQuery&amp;lt;T&amp;gt; : DbSqlQuery&amp;lt;T&amp;gt; where T : class
{
    private readonly List&amp;lt;T&amp;gt; _innerList; 

    public TestDbSqlQuery(List&amp;lt;T&amp;gt; innerList)
    {
        _innerList = innerList;
    }

    public override IEnumerator&amp;lt;T&amp;gt; GetEnumerator()
    {
        return _innerList.GetEnumerator();
    }
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;As you can from the code now I have achieved two things:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;I do have a class inheriting from &lt;code&gt;DbRawSqlQuery&lt;/code&gt; which is what I need.&lt;/li&gt;
&lt;li&gt;Secondly I do have a class which can be instantiated and furthermore does accept a parameter in constructor which then can be tricked internally.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;By tricked internally I mean this: - Since my methods all returned &lt;code&gt;DbRawSqlQuery&lt;/code&gt; which at the end is just an &lt;code&gt;IEnumerable&lt;/code&gt; type, then I thought all of these will be executing &lt;code&gt;GetEnumerator()&lt;/code&gt; method to get the result. At this point I have provided a list as a parameter in the constructor from where I can easily return all the elements by overriding the &lt;code&gt;GetEnumerator()&lt;/code&gt; method as I did above. I have a &lt;code&gt;List&amp;lt;&amp;gt;&lt;/code&gt; as a parameter but this can be easily switched accordingly to &lt;code&gt;IEnumerable&lt;/code&gt; or whatever type that supports &lt;code&gt;GetEnumerator()&lt;/code&gt; method.&lt;/p&gt;

&lt;p&gt;Now my test method setup would look like this:&lt;/p&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;_procedureService.Setup(x =&amp;gt; x.GetUserVesselPermissions(It.IsAny&amp;lt;Guid&amp;gt;(), It.IsAny&amp;lt;DateTime&amp;gt;()))
.Returns(new TestDbSqlQuery&amp;lt;UserVesselPermissionsResult&amp;gt;(new List&amp;lt;UserVesselPermissionsResult&amp;gt;
{
    new UserVesselPermissionsResult
    {
        PermissionId = 1
    }
}));
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;and I will get the expected result at the end.&lt;/p&gt;

&lt;p&gt;Hopefully this idea will help somebody struggling with setting up the methods which do return &lt;code&gt;IEnumerable&lt;/code&gt; types. &lt;/p&gt;


</description>
      <category>csharp</category>
      <category>unittesting</category>
      <category>moq</category>
    </item>
  </channel>
</rss>
