DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone >

How to setup a Moq method to return IOrderedQueryable

Melissa Irby user avatar by
Melissa Irby
·
Nov. 30, 14 · · Interview
Like (0)
Save
Tweet
4.08K Views

Join the DZone community and get the full member experience.

Join For Free

Here’s something that stumped me for a while today. I’ve got the following Linq query in my repository (this is using the ORM from DevExpress, XPO, but the basic idea is the same)

internal virtual IOrderedQueryable GetMyData(string keyVal)
{
return (from MyEntity ent in new XPQuery(Context)
where ent.Key == keyVal
orderby ent.SortCol
select end);
}

The problem I was having was in mocking the return value from this method. One cannot create an interface so I could not create a list of items to return from the mocked method.

I finally hit on this magic combination of linq queries that lets me return a set built by hand for the mock.

var emptyLst = new List();
var lst = (from d in emptyLst select d).AsQueryable().OrderBy(x => x.Key );
_mockRepo.Setup(r => r.MyMockedEvent).Returns(lst);

This seems to work like a charm

Database Charm (programming language) Interface (computing) Repository (version control)

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • A First Look at CSS When and Else Statements
  • Building a Login Screen With React and Bootstrap
  • What Is Cloud-Native Architecture?
  • Why Performance Projects Fail

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo