Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <blockquote> <p>i have two methods that i want to test "LoadAllaEscorts" and "SelectedEscorts"</p> </blockquote> <p>Those are methods on an interface. You don't write tests against an interface, or against mock objects. You write tests against concrete classes.</p> <p>Somewhere you have an EscortRepository that implements IEscortRepository. I'm assuming that hits the database. Write integration tests against that.</p> <p>Elsewhere in your code you probably have a class (call it "Foo") that has an IEscortRepository dependency injected into it (such as via a constructor parameter). When you want to write tests against the Foo class, you would use Moq to create a mock IEscortRepository returning fixed test data and pass that mock object into your Foo instance.</p> <p>Another issue is that your IEscortRepository methods are returning (or taking as a parameter) List&lt;PartialPerson&gt;. Those should be IList&lt;IPartialPerson&gt; (or IEnumerable&lt;T&gt;, ICollection&lt;T&gt;, or ReadOnlyCollection&lt;T&gt;). The most important part is that the collection items should be an interface type (IPartialPerson).</p> <p>+1 for magnifico, who had the code right:</p> <pre><code>using System; using System.Collections.Generic; using Moq; namespace ConsoleApplication1 { internal class Program { private static void Main(string[] args) { var newProduct = new Mock&lt;IPartialPerson&gt;(); newProduct.SetupGet(p =&gt; p.FirstName).Returns("FirstName"); newProduct.SetupGet(p =&gt; p.MiddleName).Returns("MiddleName"); newProduct.SetupGet(p =&gt; p.LastName).Returns("LastName"); newProduct.SetupGet(p =&gt; p.EmailAddress).Returns("EmailAddress@hotmail.com"); newProduct.SetupGet(p =&gt; p.UserID).Returns("UserID"); var mockEscortRepository = new Mock&lt;IEscortRepository&gt;(); mockEscortRepository .Setup(p =&gt; p.LoadAllEscorts()) .Returns(new List&lt;IPartialPerson&gt; {newProduct.Object}); IEscortRepository repository = mockEscortRepository.Object; IList&lt;IPartialPerson&gt; escorts = repository.LoadAllEscorts(); foreach (IPartialPerson person in escorts) { Console.WriteLine(person.FirstName + " " + person.LastName); } Console.ReadLine(); // Outputs "FirstName LastName" } } public interface IPartialPerson { string FirstName { get; } string MiddleName { get; } string LastName { get; } string EmailAddress { get; } string FullName { get; } string UserID { get; } } public interface IEscortRepository { IList&lt;IPartialPerson&gt; LoadAllEscorts(); IList&lt;IPartialPerson&gt; SelectedEscorts(IList&lt;IPartialPerson&gt; selectedEscorts); } } </code></pre> <p>(The above example is <strong>not</strong> a unit test; it just shows that Moq works.)</p> <p>Note that you don't have to use SetupGet for properties; Setup works as well.</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.
    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