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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T13:49:59+00:00 2026-05-10T13:49:59+00:00

I’ve heard that unit testing is totally awesome, really cool and all manner of

  • 0

I’ve heard that unit testing is ‘totally awesome’, ‘really cool’ and ‘all manner of good things’ but 70% or more of my files involve database access (some read and some write) and I’m not sure how to write a unit test for these files.

I’m using PHP and Python but I think it’s a question that applies to most/all languages that use database access.

  • 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. 2026-05-10T13:50:00+00:00Added an answer on May 10, 2026 at 1:50 pm

    I would suggest mocking out your calls to the database. Mocks are basically objects that look like the object you are trying to call a method on, in the sense that they have the same properties, methods, etc. available to caller. But instead of performing whatever action they are programmed to do when a particular method is called, it skips that altogether, and just returns a result. That result is typically defined by you ahead of time.

    In order to set up your objects for mocking, you probably need to use some sort of inversion of control/ dependency injection pattern, as in the following pseudo-code:

    class Bar {     private FooDataProvider _dataProvider;      public instantiate(FooDataProvider dataProvider) {         _dataProvider = dataProvider;     }      public getAllFoos() {         // instead of calling Foo.GetAll() here, we are introducing an extra layer of abstraction         return _dataProvider.GetAllFoos();     } }  class FooDataProvider {     public Foo[] GetAllFoos() {         return Foo.GetAll();     } } 

    Now in your unit test, you create a mock of FooDataProvider, which allows you to call the method GetAllFoos without having to actually hit the database.

    class BarTests {     public TestGetAllFoos() {         // here we set up our mock FooDataProvider         mockRepository = MockingFramework.new()         mockFooDataProvider = mockRepository.CreateMockOfType(FooDataProvider);          // create a new array of Foo objects         testFooArray = new Foo[] {Foo.new(), Foo.new(), Foo.new()}          // the next statement will cause testFooArray to be returned every time we call FooDAtaProvider.GetAllFoos,         // instead of calling to the database and returning whatever is in there         // ExpectCallTo and Returns are methods provided by our imaginary mocking framework         ExpectCallTo(mockFooDataProvider.GetAllFoos).Returns(testFooArray)          // now begins our actual unit test         testBar = new Bar(mockFooDataProvider)         baz = testBar.GetAllFoos()          // baz should now equal the testFooArray object we created earlier         Assert.AreEqual(3, baz.length)     } } 

    A common mocking scenario, in a nutshell. Of course you will still probably want to unit test your actual database calls too, for which you will need to hit the database.

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

Sidebar

Related Questions

No related questions found

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.