Mocking System.Data.Linq.Table

3 posts, 0 answers
  1. Alanm
    Alanm avatar
    18 posts
    Member since:
    Mar 2007

    Posted 10 Dec 2012 Link to this post

    I'm trying to Mock my Linq To Sql database context, with little luck:

    System.InvalidCastException:
    'System.Collections.Generic.List`1[My.Models.Person]' to type 'System.Data.Linq.Table`1[My.Models.Person]'

    var logic = new PersonLogic();
    var context = Mock.Create<DataContext>();
     
    var persons= new List<Person>();
    persons.Add(new Person());
     
    Mock.Arrange(() => (IEnumerable<Person>)context.Persons).Returns(persons);
    Mock.NonPublic.Arrange<DataContext>(logic, "context").Returns(context);
     
    Person returned = logic.GetPerson(1);
    Assert.Equals(returned.ID, 1);

    How do I mock System.Data.Linq.Table? Where am I going wrong.
  2. Alanm
    Alanm avatar
    18 posts
    Member since:
    Mar 2007

    Posted 10 Dec 2012 Link to this post

    Found the solution, you have to use ReturnsCollection() not Returns() which is only availble with an import of Telerik.JustMock.Helpers.

    Not obvious from intellisense.

  3. DevCraft R3 2016 release webinar banner
  4. Kaloyan
    Admin
    Kaloyan avatar
    872 posts

    Posted 10 Dec 2012 Link to this post

    Hello Alanm,

    Thank you for reaching Telerik support.

    It is good to know that you have found the solution by yourself. 

    Do not hesitate to ask if there is anything else bothering you, about JustMock.
     
    Kind regards,
    Kaloyan
    the Telerik team
    Share what you think about JustTrace & JustMock with us, so we can become even better! You can use the built-in feedback tool inside JustTrace, our forums, or our JustTrace or JustMock portals.
Back to Top