Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Firstly you should probably create an interface for <code>A</code> as well, but if this is just a simple POCO data class, it might be better to just make its properties <code>virtual</code> instead to allow for mocking. You have 3 options I think:</p> <ol> <li><p>Inject A to the constructor of <code>B</code> if it is a class that will be used often (e.g. a logging class or something). Then you can create a mock version in your test to check how <code>A</code> is used (remember, mocking is for testing behaviour with dependencies).</p> <pre><code>public class A : IA { ... } public class B : IB { private readonly A a; public B(IA a) { this.a = a; } public void Func() { //... use this.a ... } } [Test] public void Func_AHasValue1_DoesAction1() { Mock&lt;IA&gt; mock = new Mock&lt;IA&gt;(); mock.Setup(a =&gt; a.somevalue).Returns("something"); B sut = new B(mock.Object); sut.Func(); mock.Verify(m =&gt; m.SomethingHappenedToMe()); } </code></pre></li> <li>Pass <code>A</code> to the method if it is something that <code>B</code> needs to work with (as it seems here). You can still create a mock version for use in your tests. This is the same code as the above, but <code>mock</code> is passed to the method instead of the constructor. This is the better method if <code>A</code> is some data class generated at runtime instead of a class with behaviour.</li> <li><p>Create a factory class for <code>A</code> and inject that into the constructor. Change the method to get <code>A</code> instances from the factory and inject a mock factory in your tests to verify the behaviour.</p> <pre><code>public class AFactory : IAFactory { public IA Create() { ... } } public class B : IB { private readonly IAfactory factory; public B(IAFactory factory) { this.factory = factory; } public void Func() { IA a = factory.Create(); //... use this.a ... } } [Test] public void Func_AHasValue1_DoesAction1() { Mock&lt;IAFactory&gt; mockFactory = new Mock&lt;IAFactory&gt;(); Mock&lt;IA&gt; mock = new Mock&lt;IA&gt;(); mockFactory.Setup(f =&gt; f.Create()).Returns(mock.Object); mock.Setup(a =&gt; a.somevalue).Returns("something"); B sut = new B(mockFactory.Object); sut.Func(); mock.Verify(m =&gt; m.SomethingHappenedToMe()); } </code></pre> <ul> <li>Option 1 is the standard approach for classes that can be built without any runtime information (e.g. logging classes). </li> <li>Option 2 is better for when the input is only a data class that is generated at runtime (e.g. a user fills in a form and you have a POCO data class representing the form input). </li> <li>Option 3 is better when <code>A</code> is something that does have behaviour but can't be created without something generated at runtime. </li> </ul></li> </ol> <p>You'll have to look at your application to see which is most applicable.</p>
    singulars
    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.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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