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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T02:23:47+00:00 2026-05-18T02:23:47+00:00

say I have a function: public List<Car> GetFourWheelDrives() { return dataContext.Cars.Where(c => c.IsFourWheelDrive ==

  • 0

say I have a function:

public List<Car> GetFourWheelDrives()
{
   return dataContext.Cars.Where(c => c.IsFourWheelDrive == true).ToList();
}

Firstly it’s kind of obvious what I’m trying to do here. Any suggestions on how I’m doing this? Any improvements?

So now I want to unit test this. Well is it a unit test? A unit test isn’t suppose to touch the database right? So this is a functional test?

So say I write a test

[Test]
public void GetFourWheelDrives_NoParameters_ReturnAListOfOnlyCarsThatAreFourWheelDrives
{
   Assert.Greater(dataContext.Cars.Where(c => c.IsFourWheelDrive == false).ToList().Count, 0);

   var cars = GetFourWheelDrives();

   Assert.Greater(cars.Count, 0);

   Assert.AreEqual(cars.Count, cars.Where(c => c.IsFourWheelDrive == true).ToList().Count);
}

So first Assert I’m making sure cars that aren’t 4WD exist in the database otherwise it wouldn’t be a valid test.

Second Assert I’m making sure at least one was returned.

Third Assert I’m making sure that all the cars returned were in fact 4WD.

I know this isn’t a good test because I’m using the same logic in the test as the function I’m testing. However this is the only way I can currently think of to do this.

Can anybody tell me how to test this properly?

Thanks!

  • 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-18T02:23:48+00:00Added an answer on May 18, 2026 at 2:23 am

    Well, according to one school of TDD, that’s exactly what you should do; write tests that incorporate the logic you’re developing, then refactor the logic out of the test and into a production class.

    However, I would not call what you are doing a unit test, because unit tests by definition should not touch external resources like databases, file systems, and other “integration points”. Those are “integration tests”, and though they’re just as useful, you should always try to write the most side-effect-free tests you can that will prove the code works as designed.

    Look at what you’re testing. Basically, you want to ensure that the function queries your Linq datasource for cars that are four-wheel drive, and not any other type. Assume, for just a minute, that Linq2SQL knows full well how to turn this Linq statement into a SQL query against your DB. You don’t need to test that; what you DO need to test is that the correct query is made from your code against the datasource.

    To do this, you should implement a mock. A mock is a testing object that stands in for an external resource, and can be told to expect certain calls to its methods and behave in a certain way. Here, you should mock the datasource object. In order to be able to do this, the class containing GetFourWheelDrives() must be given this object from outside its scope; if it new()s one up, you’re “tightly coupled” to the Linq2SQL provider and cannot write a true unit test (you could still write it as an integration test if you can point Linq2SQL at a testing DB). Given that you can pass in the datasource used, you should give it a mock that expects to have its Cars property accessed, and returns an array of Cars objects as an IQueryable. You can define this array, meaning you have control over the data the query is executing against, and thus you can test that the query returned exactly the objects it should return.

    Using Rhino.Mocks, your code might look like this:

    [Test] 
    public void GetFourWheelDrives_NoParameters_ReturnAListOfOnlyCarsThatAreFourWheelDrives 
    { 
       var dataSource = MockRepository.GenerateMock<IDataContext>();
       var carsArray = new []{new Car{IsFourWheelDrive = false}, new Car{IsFourWheelDrive = true}};
    
       dataSource.Expect(ds=>ds.Cars).Return(carsArray.AsQueryable()).Repeat.AtLeastOnce();
    
       var objectToTest = new TestObject(dataSource); 
       var cars = objectToTest.GetFourWheelDrives(); 
    
       Assert.AreEqual(carArray.Count(c=>c.IsFourWheelDrive), cars.Count);
    
       foreach(var car in cars)
            Assert.IsTrue(car.IsFourWheelDrive);
    } 
    

    This test effectively proves that given a set of cars of arbitrary size, your function will return only, and exactly, those cars that are 4WD, without needing a real DB to do so. This is what your unit tests should look like in cases where the class you’re testing has dependencies on external code.

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

Sidebar

Related Questions

Let's say we have the following method declaration: Public Function MyMethod(ByVal param1 As Integer,
Let's say we have a method signature like public static function explodeDn($dn, array &$keys
I have a function: public static List<T> EntityCache<T>(this System.Linq.IQueryable<T> q, ObjectContext dc, string CacheId)
let say i have function like below function doSomethingNow(){ callSomethingInFutureNotExistNow(); } at the moment
Say I have a function func(i) that creates an object for an integer i,
Say I have a function foo: (defun foo (x y &rest args) ...) And
Say I have a function foo that I want to call n times. In
Lets say I have a function where the parameter is passed by value instead
Lets say I have a function f[x_, y_] , and two lists l1 ,
Let's say you have a function that returns a date: Date myFunc(paramA, paramB){ //conditionally

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.