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 8733969
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T09:45:09+00:00 2026-06-13T09:45:09+00:00

There is a IEnumerable<IDomainInterface> passed into my class and I want to test whether

  • 0

There is a IEnumerable<IDomainInterface> passed into my class and I want to test whether a specific method was called for every item:

// in class
var result = _items.SelectMany (x => x.Get (something));

// in test
domainInterfaceMock.Expect(x => x.Get (something));

The test only passes if I append ToArray() to the SelectMany() statement…

How should I deal with such situations?

  • 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-06-13T09:45:10+00:00Added an answer on June 13, 2026 at 9:45 am

    This is because Get() is only called when the LINQ expression is evaluated (i.e. iterated over). If your method in your class is calling Get() it must be doing something more than just

    var result = _items.SelectMany (x => x.Get (something));
    

    … as this in itself will not result in any calls to Get(). You should modify the test to reflect this.

    Update:
    You’re testing the method, so you should mock up the inputs to the method and pass them to the actual method, and test the expectations on these inputs and the result. If the method evaluates the LINQ expression itself then you’ll find it calls Get(), however if it doesn’t evaluate the LINQ expression itself you’re doing the right thing by forcing the evaluation with a .ToArray().

    More accurately by calling .ToArray() you’re trying to demonstrate the return value of the method was what you expected, but to do this you directly you would have to analyse the returned expression. If the only fact you want to prove is that the expression calls Get() for all its members then what you’re doing achieves that.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

ExecuteQuery() method returns an IEnumerable but is there a way to make it return
Suppose I have an IEnumerable<int> and I want these to be converted into their
Is there any method / extension method on IEnumerable that allows me to find
I have a treenode collection as IEnumerable<treenode> nodes. Is there any method to create
There is a Contains() extension method on IEnumerable; In VB I am able to
Possible Duplicate: Why is there not a ForEach extension method on the IEnumerable interface?
Possible Duplicate: Why is there not a ForEach extension method on the IEnumerable interface?
If I expose an IEnumerable<T> as a property of a class, is there any
Possible Duplicate: LINQ equivalent of foreach for IEnumerable<T> I'm wondering whether there is a
Is there an inverse/complement of IEnumerable.SelectMany ? That is, is there a method of

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.