So, I'm trying to stub a database connector in order to write tests for it. The problem is that the database connector is a pretty thin layer, and the queries to it are somewhat open-ended. I want my code to be able to ask for a variable from the database, and the connector should be OK with this. The signature for this call looks something like this:
dim = getDimension(self,dimensionName,otherIndentifyingInformation)
(This is all in MATLAB - hopefully the answer is language-agnostic, or at least not infeasible in MATLAB.)
When I use it in my code, dimensionName
is one of a few things that are stored in the database. If the code calls it, and it doesn't exist, that's fine. The problem here is that I want to test some code that makes two calls to getDimension
, something like:
alt = conn.getDimension('alt',otherID);
str = conn.getDimension('str',otherID);
For (hopefully) obvious reasons, alt
and str
are not guaranteed to be the same. In fact, they're typically not.
So, my question. If I want to stub getDimension to return good test values, how can I do this? Creating getDimensionAlt
seems kludgy, since the number of things that could come out of the database is somewhat unbounded, and that would be a pain to maintain. Is there a better way than putting logic in my stub objects? That just seems like the wrong way to go about this...
EDIT: Setting up a testDB was suggested. Wouldn't I have to set up a testDB for each test case, then? And in each test, I'd have to create a DB connection, return it as a stub, run the test, and then clean up the DB connection. This seems like it would be a lot of overhead for each test, especially when it isn't even the system I'm testing.
I suppose it's possible to set up a testDB, and fill it with the appropriate values each time. Is that good practice?
EDIT 2: Perhaps my question is unclear. I have a small piece of code I'm trying to test. It's not a lot more complicated than those two lines above, and I'd like to test it cleanly. The problem is that stubbing the getDimension
call is dependent on the arguments. I don't need to reuse this stub with other tests.
I think the answer might be "It's OK to have simple logic in your stubs." This is all confounded by the fact that there's no anonymous classes or stubbing framework in MATLAB, so that's hard, but I want to make sure what I'm doing is conceptually clear before I go off and write a stubbing framework in MATLAB.