Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7561061
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T13:04:12+00:00 2026-05-30T13:04:12+00:00

Been busting my head on this one – time to give it up to

  • 0

Been busting my head on this one – time to give it up to the crowd: anyone know how to successfully set a Mole (or any other unit testing work around) for a Join in Linq?

Specifically, this particular project is using Linq to Sql. In fact, it is the first time I’ve used Linq to Sql and I’m trying to demonstrate effective and correct unit testing. I have a method that pulls TableA and TableB, which are linked by a foreign key, to create a data transfer object basically modeled on TableA. Code is in-exact, as I had to leave it at work.

public List<TableADto> GetTableA()
{
    using (MyDataContext context = new MyDataContext)
    {
        var query = from a in context.a
                join b in context.b on a.ForeignId equals b.ForeignId
                select MyBuilderClass.CreateTableADto(a, b);

        return query.ToList();
    }
}

I find the code to be quite elegant in its way, and it works beautifully in system testing. But I can’t figure out how to unit test it. I’ve a veteran user of Moles for setting up detours. For a query from a single table, I can simply put a mole on

System.Linq.Data.Moles.MTable<TableA>.AllInstances.GetEnumerator = ...

For multiple tables, I find that I also need to create a stub IQueryProvider, and I need to stub out the CreateQuery methods. But even doing so, I also get an error message saying that CreateExpression is not stubbed. I’ve tried

  • MTable<TableA>.AllInstances.CreateQueryExpression = (Expression e) => { return listA.AsQueryable().Provider; }
  • MTable<TableA>.AllInstances.CreateQueryExpression01(Expression e => listA.AsQueryable().Provider; }
  • MTable<TableA>.AllInstances.CreateQueryExpression<TableB> = (Expresion e) => { return listB.AsQueryable().Provider; }
  • // MTable<TableA>.AllInstances.CreateQueryExpression<Tablea> = (Expresion e) => { return listA.AsQueryable().Provider; } /* REDUNDANT WITH THE FIRST ONE */
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T13:04:14+00:00Added an answer on May 30, 2026 at 1:04 pm

    Here’s the solution: MTable can be instantiated. That helps immensely. I needed to set a stub on two different methods for each of my types. Following code worked perfectly:

    // Note: typeAList = List<TypeA> with values populated in the test itself
    
    System.Data.Linq.Moles.MTable<TypeA> typeATable = new System.Data.Linq.Moles.MTable<TypeA>();
    typeATable.Bind(typeAList);
    typeATable.ProviderSystemLinqIQueryableget = () => typeAList.AsQueryable().Provider;
    typeATable.ExpressionSystemLinqIQueryableget = () => typeAList.AsQueryable().Expression;
    MyLibrary.Data.Moles.MMyDataContext.AllInstances.TypeAsGet = (c) => { return typeATable; };
    
    System.Data.Linq.Moles.MTable<TypeB> typeBTable = new System.Data.Linq.Moles.MTable<TypeB>();
    typeBTable.Bind(typeBList);
    typeBTable.ProviderSystemLinqIQueryableget = () => typeBList.AsQueryable().Provider;
    typeBTable.ExpressionSystemLinqIQueryableget = () => typeBList.AsQueryable().Expression;
    MyLibrary.Data.Moles.MMyDataContext.AllInstances.TypeBsGet = (c) => { return typeBTable; };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've been busting my head on this, and not so sure what to do.
i have been busting my head trying to figure this out. I found this
Been a while since I've dealt with ASP.NET and this is the first time
Been having lot's of trouble with this one. Let's say I had the following
Been googling for this and haven't found anything ... Does anybody know if there
Been having major issues trying to solve this issue, I'll be happy to give
I've been butting my head against this problem in an assignment I've been working
Been running into this problem lately... When debugging an app in VS.Net 2005, breakpoints
Been reading up on MitB attacks and some things worry me about this. From
Been stumbling me for over an hour now and that means time to ask

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.