Note that there are some explanatory texts on larger screens.

plurals
  1. POCan this be mocked with Moq?
    primarykey
    data
    text
    <p>I am working on mocking some external dependencies and am having trouble with one 3rd party class that takes in it's constructor an instance of another 3rd party class. Hopefully the SO community can give me some direction.</p> <p>I want to create a mock instance of <code>SomeRelatedLibraryClass</code> that takes in it's constructor a mock instance of <code>SomeLibraryClass</code>. How can I mock <code>SomeRelatedLibraryClass</code> this way?</p> <p><strong>The repo code...</strong></p> <p>Here is a Main method that I am using in my test console application.</p> <pre><code>public static void Main() { try { SomeLibraryClass slc = new SomeLibraryClass("direct to 3rd party"); slc.WriteMessage("3rd party message"); Console.WriteLine(); MyClass mc = new MyClass("through myclass"); mc.WriteMessage("myclass message"); Console.WriteLine(); Mock&lt;MyClass&gt; mockMc = new Mock&lt;MyClass&gt;("mock myclass"); mockMc.Setup(i =&gt; i.WriteMessage(It.IsAny&lt;string&gt;())) .Callback((string message) =&gt; Console.WriteLine(string.Concat("Mock SomeLibraryClass WriteMessage: ", message))); mockMc.Object.WriteMessage("mock message"); Console.WriteLine(); } catch (Exception e) { string error = string.Format("---\nThe following error occurred while executing the snippet:\n{0}\n---", e.ToString()); Console.WriteLine(error); } finally { Console.Write("Press any key to continue..."); Console.ReadKey(); } } </code></pre> <p>Here is a class that I have used to wrap one third party class and allow it to be Moq'd:</p> <pre><code>public class MyClass { private SomeLibraryClass _SLC; public MyClass(string constructMsg) { _SLC = new SomeLibraryClass(constructMsg); } public virtual void WriteMessage(string message) { _SLC.WriteMessage(message); } } </code></pre> <p>Here are two examples of 3rd party classes I am working with (<strong>YOU CAN NOT EDIT THESE</strong>):</p> <pre><code>public class SomeLibraryClass { public SomeLibraryClass(string constructMsg) { Console.WriteLine(string.Concat("SomeLibraryClass Constructor: ", constructMsg)); } public void WriteMessage(string message) { Console.WriteLine(string.Concat("SomeLibraryClass WriteMessage: ", message)); } } public class SomeRelatedLibraryClass { public SomeRelatedLibraryClass(SomeLibraryClass slc) { //do nothing } public void WriteMessage(string message) { Console.WriteLine(string.Concat("SomeRelatedLibraryClass WriteMessage: ", message)); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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