Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If it is needed to test delegates I usually apply them to any test object. In order to test PersonService I'd rather use the following code that contains examples of two tests.</p> <pre><code>[TestClass] public class UnitTest2 { private Mock&lt;IRepository&gt; moqRepository; private PersonService service; [TestInitialize] public void TestInitialize() { moqRepository = new Mock&lt;IRepository&gt;(); service = new PersonService(moqRepository.Object); } [TestMethod] public void TestMethod3() { // arrange var persons = new List&lt;Person&gt; { new Person { Age = 0 }, new Person { Age = 1 }, new Person { Age = 17 }, new Person { Age = 18 }, new Person { Age = 19 }, new Person { Age = 100 } }; moqRepository .Setup(x =&gt; x.GetForExpression(It.IsAny&lt;Expression&lt;Func&lt;Person, bool&gt;&gt;&gt;())) .Returns&lt;Expression&lt;Func&lt;Person, bool&gt;&gt;&gt;(expr =&gt; persons.Where(expr.Compile()).ToList()); // act var result = service.GetAllPersonsWith18More(); // assert moqRepository.VerifyAll(); result.Should().BeEquivalentTo(persons.Where(x =&gt; x.Age &gt; 18)); } [TestMethod] public void TestMethod4() { // arrange Expression&lt;Func&lt;Person, bool&gt;&gt; criteria = x =&gt; x.Age &gt; 18; moqRepository .Setup(x =&gt; x.GetForExpression(It.IsAny&lt;Expression&lt;Func&lt;Person, bool&gt;&gt;&gt;())) .Returns(new List&lt;Person&gt;()) .Callback&lt;Expression&lt;Func&lt;Person, bool&gt;&gt;&gt;(expr =&gt; { var persons = new List&lt;Person&gt; { new Person { Age = 0 }, new Person { Age = 1 }, new Person { Age = 17 }, new Person { Age = 18 }, new Person { Age = 19 }, new Person { Age = 100 } }; persons.Where(expr.Compile()).Should().BeEquivalentTo(persons.Where(criteria.Compile())); }); // act service.GetAllPersonsWith18More(); // assert moqRepository.VerifyAll(); } } </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.
    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