How can we help you today? How can we help you today?
datacentricity
Simon, just a thought - if you're having issues with FakeTable it might be worth starting a new thread. FakeTable itself is pretty reliable in my experience, but schema-level tasts can introduce some interesting questions around how we do certain types of test. / comments
Simon, just a thought - if you're having issues with FakeTable it might be worth starting a new thread. FakeTable itself is pretty reliable in my experience, but schema-level tasts can introduce so...
0 votes
Simon, Ok so we know that [SetUp] is being called. And, if you've corrected the schema references, we need to find out whether the table is actually created and if it is, at point it disappears again. Try adding this to the end of your SetUp procedure: IF OBJECT_ID(N'BoardSummary.Actual') > 0 RAISERROR('SetUp: [Actual] table exists in BoardSummary schema', 0, 1) WITH NOWAIT; ELSE IF OBJECT_ID(N'dbo.Actual') > 0 RAISERROR('SetUp: [Actual] table not found in BoardSummary schema but deos exist in dbo schema', 0, 1) WITH NOWAIT; ELSE RAISERROR('SetUp: [Actual] table not found in BoardSummary schema', 0, 1) WITH NOWAIT; Then add this at the top of the test procedure IF OBJECT_ID(N'BoardSummary.Actual') > 0 RAISERROR('In Test: [Actual] table exists in BoardSummary schema', 0, 1) WITH NOWAIT; ELSE RAISERROR('In Test: [Actual] table not found in BoardSummary schema', 0, 1) WITH NOWAIT; and then add this just before you try to insert the Actual table: IF OBJECT_ID(N'BoardSummary.Actual') > 0 RAISERROR('Before Assert: [Actual] table exists in BoardSummary schema', 0, 1) WITH NOWAIT; ELSE RAISERROR('Before Assert: [Actual] table not found in BoardSummary schema', 0, 1) WITH NOWAIT; Then just run the one test with tSQLt.Run and see what the output is. Also, you were correct in naming the set-up procedure [SetUp]. tSQLt looks for this before running each test. If you'd left it as [test SetUp], it would have got called once because it was treated as just a regular test. Also, in case it wasn't clear earlier, the reason why I recommend putting the [actual] and [expected] tables in the test schema rather than dbo is because one day you might actually be testing a database that has a table named like that. / comments
Simon, Ok so we know that [SetUp] is being called. And, if you've corrected the schema references, we need to find out whether the table is actually created and if it is, at point it disappears aga...
0 votes
Simon, In terms of achieving DRY, you do seem to have a lot of setup here, And that setup seems to be calling production procedures. Obviously I don't know about the underlying logic but if it were me, I would ask myself whether it really is necessary to call all those, or even any of them. If I understand this correctly, the procedure under test is apPROMsBoardSummaryReport. I am presuming that this manipulates data from a number of tables (presumably populated by the previous procedure calls). If I wanted to isolate this test from everything else, I wopuld endeavour to just populate the tables and columns that are required for this one test. As a simple example, if I had an [OrderDetail] table with [Price] and [Quantity] columns, and the procedure was supposed to generate a [Cost] column, then at the simplest level these are the only columns that need to be populated to fulfil that test. Only populate the tables columns that are essential to the test, use tSQLt.FakeTable to achieve this where necessary. If you do use tSQLt.FakeTable, all columns on the mock table are defined as nullable - precisely to allow you to populate only the columns required for the test. Where this really helps is when another NOT NULL column is added to [OrderDetail], your existing tests won't break because they're not populating that column. Test Driven Development requires a different mindset, look for ways to break functionality down to the lowest common denominator. If you need three or four tables to contain data in order to test apPROMsBoardSummaryReport, than add rows to those directly. As it stands, if the test you demonstrate here fails, how do you know where the failure is? Looking at your test, it could just as easily be in apSaveUploadedScoreCardDetailsNew, apSaveUploadedScoreCardResults or apAddUploadedScorecardHeader. Those should be subject to their own tests that assert they do everything they should. Otherwise, what you have here is an integration test - which is still useful but much harder to identify the cause of any failure. There is pattern called Test Data Builder used in the .Net and Java world that can be used to simplify this kind of set up. For example Adapting the Test Data Builder Pattern for T-SQL. Using TDB, you could write a procedure that might take all the inputs from the production procedure calls you are making and puts the correct values in the appropriate tables and columns. Then you have just one procedure call which can be reused in other tests passing different values. Even if the production procedures you are calling in this test are douing complex calculations, the end result is probably a set of numbers in some rows in some tables - it is those simple numbers you need to set up for this test - not all the intevening logic. If you're familiar with DRY, you will also know about SRP - Single Responsibility Principle. Sorry if this reply goes on a bit but this is a complex subject. TDD really does change how you look at code - resulting in more robust, better designed code. We just have to get through that learning curve first :-) / comments
Simon, In terms of achieving DRY, you do seem to have a lot of setup here, And that setup seems to be calling production procedures. Obviously I don't know about the underlying logic but if it wer...
0 votes
Simon, I'm glad my tutorials have inspired you. The difficulty is that once you start using TDD, you'll struggle to work without it. NB: Odd, I didn't see the refactored code when I first looked Looking at the example code you posted, I'm assuming that this is before refactoring creation of the actual and expected table out into the setup method. The most obvious thing to check is that you also remove the DROP TABLE statements at the beginning of [BoardSummary].[test GetBoardSummary_2ScorecardsAdded_MaxMinUsedForRanges]. In fact arguably the DROPS aren't really required since the CREATES get rolled back at the end of each test. It's my OCD that makes me do it in my code :-) If that's not the problem, try creating a [BoardSummary].[SetUp] that just does RAISERROR('Setup called!', 0, 1) WITH NOWAIT; then run just one test tSQL.Run '[BoardSummary].[test GetBoardSummary_2ScorecardsAdded_MaxMinUsedForRanges]' in another window to see if SetUp does actually get called. Then build things up from there. Another thing to check (although a longshot) if you happen to be running on a case-sensitive database is that you are running the latest version of the tSQLt framework SELECT * FROM tSQLt.Info() should yield 1.0.4504.21220. There was a small issue in an earlier version WRT case-sensitivity of the SetUp call. Mind you if you've installed it via SQL Test this shouldn't be a problem. If these suggestions don't work, let me know and I'll see what else I can come up with / comments
Simon, I'm glad my tutorials have inspired you. The difficulty is that once you start using TDD, you'll struggle to work without it. NB: Odd, I didn't see the refactored code when I first looked L...
0 votes
Yes this does still exist. I blogged about it a while back Unit Testing Databases with tSQLt Part 6 – testing a SELECT stored procedure under the heading "Automatic Setup". If that doesn't help, perhaps you could post your code. / comments
Yes this does still exist. I blogged about it a while back Unit Testing Databases with tSQLt Part 6 – testing a SELECT stored procedure under the heading "Automatic Setup". If that doesn't help, ...
0 votes
An alternative view might be that if one uses cursors so rarely as to need a snippit to remeber all the correct syntax that's a good thing. / comments
An alternative view might be that if one uses cursors so rarely as to need a snippit to remeber all the correct syntax that's a good thing.
0 votes