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
Hi all,

Here's my test
[TestMethod]
public void GetResumeDataReturnsData()
{

var contextMock = MockManager.MockAll<AlexDreskoDataContext>();


var skills = new List<Skill>
{
new Skill()
{
Name = "weeee"
}
};


contextMock.ExpectGetAlways("Skills", skills.AsQueryable());

var data = ResumeManager.GetData();

Assert.IsTrue(data.Skills.Count() == 1);
}


Here's the error

Test method AlexDresko.comTests.ResumeManagerTests.GetResumeDataReturnsData threw exception: TypeMock.TypeMockException:
*** Method get_Skills in type DataManager.AlexDreskoDataContext returns System.Data.Linq.Table`1[[DataManager.Skill, DataManager, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]] and not System.Linq.EnumerableQuery<DataManager.Skill>..


I've been chasing this all day so it would be great if someone could point me in the right direction. I'd greatly appreciate any help!

Thanks!
asked by alexdresko (1.7k points)

3 Answers

0 votes
Hi,

The error that you're getting is due to a mismatching return types.
As far as I can tell the property: Skills return an object of type: Table<Skills>.
While in your test the given return object is of a different type.
EnumerableQuery<Skill> .

I'm not sure exactly how best to help you here, since I'm missing large parts of the code.
I'm guessing that you use the table that is returned by the Skills property in a LINQ query. If that is the case, I think that you should try mocking the entire query, I think that would be much easier to do.
You can also try faking all calls on the return Table<Skills> instance, but I think that will be harder to achieve.

I know this is not much help, so if you can post more of your code (specifically the GetData implementation) I'm sure i can help you more.
answered by lior (13.2k points)
0 votes
Thanks lior. I wrongly assumed that someone would just know what the problem is because of its relation to the LinqToSql DataContext.. Anyway, here's GetData..

public class ResumeManager
{
public static ResumeData GetData()
{
var data = new ResumeData();

var context = DatabaseManager.GetContext();
data.Skills = context.Skills;
data.ProjectOwners = context.ProjectOwners.OrderByDescending(c => c.StartDate);

return data;
}
}


and here's DatabaseManager.GetContext()

public static AlexDreskoDataContext GetContext()
{
return new AlexDreskoDataContext(DatabaseManager.GetConnectionString());
}


As you can see, ResumeManager.GetData() instantiates a DataContext which does indeed return a Table<Skill>. Problem is, I can't figure out how to mock the DataContext so it returns a Table<Skill>. This post (http://www.elilopian.com/2007/08/19/moc ... /#comments) has a comment taht says I should be able to call skill.AsQueryable() as I did in my example, but that's what is throwing the error I posted.

If you look above where data.ProjectOwners is being set, you can see that a more realistic example of what I want to do involves more complicated manipulation of Table<TEntity>. What I want is to simply mock what the DataContext returns so that I can fully test those queries without having to actually go to the database.

You'll be my hero if you can figure this one out, buddy! :)
answered by alexdresko (1.7k points)
0 votes
Alex,

look at the following test:
[Test]
public void GetData_MockContext()
{
    List<ProjectOwner> fakeList = new List<ProjectOwner>();
    fakeList.Add(new ProjectOwner());

    using (RecordExpectations rec = new RecordExpectations())
    {
        AlexDreskoDataContext mockContext = RecorderManager.CreateMockedObject<AlexDreskoDataContext>();
        var dum1 = mockContext.Skills;
        rec.Return(null);

        var dum2 = mockContext.ProjectOwners.OrderByDescending(c => c.StartDate).ToList();
        rec.Return(fakeList);

        DatabaseManager.GetContext();
        rec.Return(mockContext);
    }

    ResumeData actual = ResumeManager.GetData();
    Assert.IsNull(actual.Skills);
    Assert.AreEqual(1, actual.ProjectOwners.Count);
}


I think this does most of what you need.

:arrow: Its really hard creating fake tables. The table class was not meant to be created from user code, and although it is possible, doing so will complicate your test code. The easier way is to try to convert the table into a list based objects as soon as possible. In your case I would do that when creating and populating the ResumaData object.

:!: I know that this test will not work outright on your code (mainly because I'm returning a list where you use a Table<>), but I'm sure that if you can wrap the code that you have into a small solution and send to us, i can tweak this specifically to your needs.

Anyway, I hope this help, but mocking LINQ is quite tricky so feel free to ask more questions if you have any.
answered by lior (13.2k points)
...