chevron-thin-right chevron-thin-left brand cancel-circle search youtube-icon google-plus-icon linkedin-icon facebook-icon twitter-icon toolbox download check linkedin phone twitter-old google-plus facebook profile-male chat calendar profile-male
Welcome to Typemock Community! Here you can ask and receive answers from other community members. If you liked or disliked an answer or thread: react with an up- or downvote.
0 votes
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 ;)
asked by zzrbit (600 points)

1 Answer

0 votes
Hi
I couldn't reproduce the problem from the code you posted.
Especially I'm missing the actual call to the code under test.
Can you please post the full test code?
If you can please add the relevant implementation of the code under test.
answered by ohad (35.4k points)
...