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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T09:29:11+00:00 2026-06-12T09:29:11+00:00

I’m trying to write some unit tests for a service which returns collections of

  • 0

I’m trying to write some unit tests for a service which returns collections of complex objects (IQueriable<MyObject> to give an idea) and I’m not sure how to approach the problem.

Consider this:

public IQueriable<MyObject> GetMyObjects(int someParameterA, int someParameterB)
{...}

Basically, a method that, depending on the passed parameters, will output a collection of rather complex objects.

Since the application is under development, I can use a local SQL instance and I’ve created a method which will populate this DB with some sample data, so the service has something to work with.

I don’t have much experience in writing unit tests. OK, I’ll be honest, I don’t have ANY experience in writing unit tests. -_- And most of the examples I saw were for methods that do some calculations and return a single result. It is thus possible to attach a data source for the test with test cases. If a method was to return the sum of two numbers, the data source would consist of three columns: the two numbers and the expected result.

So, with my limited knowledge about unit tests, I’m trying to apply the same logic to test a method that returns a collection… and I’ve no idea what the test should consist of (I know I could easily test the number of returned elements), let alone what the test data source with test cases should look like.

I know the VS2010 has CollectionAssert, but I guess my problem is that I don’t know how to create the “expected” values in this case.

What’s the best way to approach unit tests for such methods?

  • 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-12T09:29:12+00:00Added an answer on June 12, 2026 at 9:29 am

    Create a mock collection for the data source. Either full-blown objects, or if they are complex to create in tests then look at using a mocking framework perhaps, such as Moq, or Rhino Mocks.

    As long as your data source contains a sufficient range of objects to cover positive, negative and exception cases then you can write a suite of tests against this data source to cover all expected outcomes from the different input states that are possible in GetMyObjects.

    You want this data to be as it would be when the application is running, so even if you don’t have ALL the data you would have normally, you’ve just got to have enough in there to cover the eventualities you want to test against, such as:

    • What happens when there’s no data in the source.
      • Do we get an empty collection, or an exception?
    • What happens when there’s data in the source, but doesn’t match the params you pass in
    • What happens when there’s data in the source, but only one of the params match
    • What happens when there’s data in the source and you pass in params that should return data
    • What happens when you pass in erroneous data to the method?
    • etc….
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
I'm trying to select an H1 element which is the second-child in its group
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I used javascript for loading a picture on my website depending on which small
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am trying to render a haml file in a javascript response like so:
I would like to run a str_replace or preg_replace which looks for certain words

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.