This is a migrated thread and some comments may be shown as answers.

Mocking System.Data.Linq.Table

2 Answers 140 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Alanm
Top achievements
Rank 2
Alanm asked on 10 Dec 2012, 12:20 PM
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 Answers, 1 is accepted

Sort by
0
Alanm
Top achievements
Rank 2
answered on 10 Dec 2012, 01:12 PM
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.

0
Kaloyan
Telerik team
answered on 10 Dec 2012, 01:56 PM
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.
Tags
General Discussions
Asked by
Alanm
Top achievements
Rank 2
Answers by
Alanm
Top achievements
Rank 2
Kaloyan
Telerik team
Share this question
or