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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T16:29:27+00:00 2026-05-14T16:29:27+00:00

i’ve started unit testing a while ago and as turned out i did more

  • 0

i’ve started unit testing a while ago and as turned out i did more regression testing than unit testing because i also included my database layer thus going to the database verytime.

So, implemented Unity to inject a fake database layer, but i of course want to store some data, and the main opinion was: “create an in-memory database”

But what is that / how do i implement that?

Main question is: i think i have to fake the database layer, but doesn’t that make me create a ‘simple database’ myself or: how can i keep it simple and not rebuilding Sql Server just for my unit tests 🙂

At the end of this question i’ll give an explanation of the situation i got in on the project i just started on, and i was wondering if this was the way to go.

Michel

Current situation i’ve seen at this client is that testdata is contained in XML files, and there is a ‘fake’ database layer that connects all the xml files together.
For the real database we’re using the entity framework, and this works very simple.
And now, in the ‘fake’ layer, i have top create all kind of classes to load, save, persist etc. the data.
It sounds weird that there is so much work in the fake layer, and so little in the real layer.

I hope this all makes sense 🙂

EDIT:
so i know i have to create a separate database layer for my unit test, but how do i implement it?

  • 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-14T16:29:28+00:00Added an answer on May 14, 2026 at 4:29 pm

    Uhhhh…… If you’re storing all your test data in XML files. You’ve just changed one database for another. That is not an in memory database. In PHP you would use something like this.

    class MemoryProductDB {
    
        private $products;
    
        function MemoryProductDB() {
            $this->products = array();
        }
    
        public function find($index) {
            return $this->products[$index];
        }
    
        public function save($product) {
            $this->products[$product['index']] = $product;
        }
    }
    

    You notice that all my data is stored in a memory array and is retrieved from a memory array. This is a simple In Memory Database.

    IMHO, if you’re using XML to store test data then you really haven’t disconnected the dependencies from the model and the database effectively. No matter how complex your business rules are, when they touch the database, all they really are doing is CRUD (create, retrieve, update, and delete) functionality.

    If you what your dealing with in the model is multiple objects from the database then maybe you need to compose all those objects into a single object and have the model use that one object. An example would be an order composed of products. Don’t be retrieving products then saving products. Retrieve orders then save orders and have your model work on orders. The model shouldn’t know anything about products.

    This is called granularity of abstraction.

    [Edit]
    There was a very good question in the comments. When testing with an In Memory Database we don’t care about how the select works in a database. The controller, first off, has to have functionality on the database to count the number of possible records that could be accessed for paging. The IMDb (in memory database) should just send a number. The controller should never care what that number is. Same with the actual records. Hopefully all your controller is doing is displaying what it gets back from the IMDb.

    [EDit]
    You should never be unit testing your controllers with a live model and imdb. The setup code for the imdb will have a lot of friction. Instead when unit testing a controller, you need to unit test a mock, stub, fake model. The best use of an imdb is during an integration test or when unit testing a model. Isn’t an imdb a fake?

    My scenario is:

    1. In my client I use a plug in for a table. DataTables. Server side processing.
    2. Client GET requests items in table product.get(5,10). The return data will be encoded JSON.

    The model will be responsible for forming the JSON from retrieving information from the gateway to the database. The gateway is just a facade over the database. I’m a mocker so my gateway is a mock not an in memory gateway.

    public function testSkuTable() {
        $skus = array(
                array('id' => '1', 'data' => 'data1'),
                array('id' => '2', 'data' => 'data2'),
                array('id' => '3', 'data' => 'data3'));
    
        $names = array(
                'id',
                'data');
        $start_row = $this->parameters['start_row'];
        $num_rows = $this->parameters['num_rows'];
        $sort_col = $this->parameters['sort_col'];
        $search = $this->parameters['search'];
        $requestSequence = $this->parameters['request_sequence'];
        $direction = $this->parameters['dir'];
        $filterTotals = 1;
        $totalRecords = 1;
    
        $this->gateway->expects($this->once())
                ->method('names')
                ->with($this->vendor)
                ->will($this->returnValue($names));
    
        $this->gateway->expects($this->once())
                ->method('skus')
                ->with($this->vendor, $names, $start_row, $num_rows, $sort_col, $search, $direction)
                ->will($this->returnValue($skus));
    
        $this->gateway->expects($this->once())
                ->method('filterTotals')
                ->will($this->returnValue($filterTotals));
    
        $this->gateway->expects($this->once())
                ->method('totalRecords')
                ->with($this->vendor)
                ->will($this->returnValue($totalRecords));
    
        $expectJson = '{"sEcho": '.$requestSequence.', "iTotalRecords": '.$totalRecords.', "iTotalDisplayRecords": '.$filterTotals.', "aaData": [ ["1","data1"],["2","data2"],["3","data3"]] }';
        $actualJson = $this->skusModel->skuTable($this->vendor, $this->parameters);
    
        $this->assertEquals($expectJson, $actualJson);
    }
    

    You will notice that with this unit test that I’m not concerned what the data looks like. $skus doesn’t even look anything like that actual table schema. Just that I return records. Here is the actual code for the model:

    public function skuTable($vendor, $parameterList) {
        $startRow = $parameterList['start_row'];
        $numRows = $parameterList['num_rows'];
        $sortCols = $parameterList['sort_col'];
        $search = $parameterList['search'];
        if($search == null) {
            $search = "";
        }
        $requestSequence = $parameterList['request_sequence'];
        $direction = $parameterList['dir'];
    
        $names = $this->propertyNames($vendor);
        $skus = $this->skusList($vendor, $names, $startRow, $numRows, $sortCols, $search, $direction);
        $filterTotals = $this->filterTotals($vendor, $names, $startRow, $numRows, $sortCols, $search, $direction);
        $totalRecords = $this->totalRecords($vendor);
    
        return $this->buildJson($requestSequence, $totalRecords, $filterTotals, $skus, $names);
    }
    

    The first part of the method breaks the individual parameters from the $parameterList that I get from the get request. The rest are calls to the gateway. Here is one of the methods:

    public function skusList($vendor, $names, $start_row, $num_rows, $sort_col, $search, $direction) {
        return $this->skusGateway->skus($vendor, $names, $start_row, $num_rows, $sort_col, $search, $direction);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 432k
  • Answers 432k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Given that the primary key is an opaque data type… May 15, 2026 at 2:35 pm
  • Editorial Team
    Editorial Team added an answer You probably want to create some 'fields on related fields'.… May 15, 2026 at 2:35 pm
  • Editorial Team
    Editorial Team added an answer Use FireBug to find out why. Perhaps the second time… May 15, 2026 at 2:35 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.