Note that there are some explanatory texts on larger screens.

plurals
  1. POWhat is the way to go to fake my database layer in a unit test?
    primarykey
    data
    text
    <p>I have a question about unit testing.</p> <p>Say I have a controller with one create method which puts a new customer in the database:</p> <pre><code>//code a bit shortened public actionresult Create(Formcollection formcollection){ client c = nwe client(); c.Name = formcollection["name"]; ClientService.Save(c); { </code></pre> <p>Clientservice would call a datalayer object and save it in the database.</p> <p>What I do now is create a database testscript and set my database in a know condition before testing. So when I test this method in the unit test, I know that there must be one more client in the database, and what its name is.In short:</p> <pre><code>ClientController cc = new ClientController(); cc.Create(new FormCollection (){name="John"}); //i know i had 10 clients before assert.areEqual(11, ClientService.GetNumberOfClients()); //the last inserted one is John assert.areEqual("John", ClientService.GetAllClients()[10].Name); </code></pre> <p>So I've read that unit testing should not be hitting the database, I've set up an IOC for the database classes, but then what? I can create a fake database class, and make it do nothing.</p> <p>But then of course my assertions will not work because if I say <code>GetNumberOfClients()</code> it will always return X because it has no interaction with the fake database class used in the Create Method.</p> <p>I can also create a List of Clients in the fake database class, but as there will be two different instances created (one in the controller action and one in the unit test), they will have no interaction.</p> <p>What is the way to make this unit test work without a database?</p> <p>EDIT: The clientservice doesn't connect directly to the DB. It calls a ClientDataClass which will connect to the database. So the ClientDatabaseClass will be replaced with a fake</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload