Very new to typemock(6hours) and somewhat new to c# as well, so apologies in advance for any stupid questions.
Im trying to mock a datacontext without success.
I have read a couple of other threads on this forum on the subject, but the solutions do not seem to work for me.
I have two major problems.
1)
When trying to mock a linq query, i create a List<T> of stuff, and expect a return of that list.AsQueryable() from my query;
However shortly after the query is performed, a query.Count() call is made, and an ArgumentException is thrown.
Here's the test setup:
var fake = new List<DerivedMessage> { fakeMessage, fakeMessage, fakeMessage }.AsQueryable();
MQDataContext context = new MQDataContext();
using (RecordExpectations recorder = RecorderManager.StartRecording())
{
var mockedResult = from msg in context.Messages.OfType<DerivedMessage>()
where msg.MessageId == "x"
select msg;
recorder.Return(fake);
}
When debugging i can see that the query is correctly mocked, returning what i asked it to, yet when .Count() is called the exception is thrown with the message
System.ArgumentException: Argument expression is not valid
at System.Linq.EnumerableQuery`1.System.Linq.IQueryProvider.Execute<S>(Expression expression)
at System.Linq.Queryable.Count<TSource>(IQueryable`1 source)
Needless to say, this does not occur when things are running normally without mocking.
2)
As an alternative to the above, i tried mocking a table ( or property) of the datacontext.
var fake = new List<Message> { fakeMessage, fakeMessage, fakeMessage };
Mock mqdc = MockManager.Mock(typeof(MQDataContext));
mqdc.ExpectGetAlways("Messages", fake);
When trying to run it, i get the following exception:
Method get_Messages in type DataAccess.MessageQueueDataContext
returns System.Data.Linq.Table`1[[DataAccess.Entities.Message, DataAccess, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]
and not System.Collections.Generic.List<DataAccess.Entities.Message>.
Now the exception makes sense i suppose. The only reason i ask is several of the posts ive read from this forum seem to do the exact same thing, passing in a regular list or an array.
Hope someone can help me ;)