Note that there are some explanatory texts on larger screens.

plurals
  1. POResolving dependencies via UnityAutoMoqContainer
    text
    copied!<p>I have started using the UnityAutoMoqContainer <a href="https://github.com/thedersen/unityautomoq" rel="nofollow">Here is the Link</a> and I have below 2 questions in particularly around container.GetMock() call.</p> <ol> <li><p>I would expect the below Assert to succeed however it throws an exception.</p> <pre><code>private UnityAutoMoqContainer container; [SetUp] public void SetUp() { container = new UnityAutoMoqContainer(); } [Test] public void Are_mocks_Same(){ var serviceMock = new Mock&lt;IService&gt;(); var getMock = container.GetMock&lt;IService&gt;(); Assert.AreSame(getMock, serviceMock); } </code></pre></li> </ol> <blockquote> <p>Error 1 Test 'UnityAutoMoq.Tests.UnityAutoMoqContainerFixture.Are_mocks_Same' failed: Expected: same as at UnityAutoMoq.Tests.UnityAutoMoqContainerFixture.Are_mocks_Same() in C:\Users…….</p> </blockquote> <p>So why when "Expected" and the "But was" are same it still throws an exception?</p> <p>The difference I see is that <em>GetMock</em> uses the Unity to resolve i,e <em>Resolve()</em> dependencies where the <em>new Mock</em> doesn’t. But I cannot explain my self the cause of this exception.</p> <ol> <li><p>Resolving abstract types:</p> <p>I use the Moq.Mock to resolve an abstract type as below.</p> <pre><code> var httpContextBaseMock = new Mock&lt;HttpContextBase&gt;(); </code></pre></li> </ol> <p>However the below call to the UnityAutoMoqContainer throws the exception:</p> <pre><code> var mock = container.GetMock&lt;HttpContextBase&gt;(); </code></pre> <blockquote> <p>Resolution of the dependency failed, type = "System.Web.HttpContextBase", name = "(none)". Exception occurred while: while resolving. Exception is: InvalidOperationException - The type HttpContextBase cannot be constructed. You must configure the container to supply this value.</p> </blockquote> <p>The question is why the container does not facilitate to return a mocked abstract type?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload