ShowMeTheMoney-13 - More of the same

Iteration2 Day 1/10
Note: Nothing extraordinary in this post.. one of those when you just have to get your head down and get the work done.. Today might be boring.. it was boring for me to type it all in.. All the source code is listed in the next post

I forgot to add a task for exploratory testing as decided in our iteration review. We’ll do that intermittently throughtout this iteration – we’ll plan for it from the next iteration : we don’t have a dedicated-n-free tester.
Let’s begin by writing the acceptance tests for tracking expenses. This should be similar to what we’ve already done. Instead of creating a new wiki page. Lets continue with the flow of the same page. We have already added 3 credit entries and deleted one. Lets create a new page TestsForTrackingBalance. Copy the existing fixtures over there and extend them. First we transfer the fixtures to the new page…. Done.
The changes are in bold. Created a copy of the fixture ruby class. Renamed it to track_balance.rb – Changed a few method names and voila! Green! Now let’s add our extensions to add expense records at the end of the action-fixture-table. We will add 3 expense records and delete the second one. And then in the row-fixture-table, we retrieve the expense records

Ok so now the acceptance tests have been written. Lets make it happen. So like before, we create the expenses table schema, which is identical to the credits table. We update our 3 DBs with the new schema file. We create a test class for the model (\test\unit\expense_test.rb) and add the test_create method.
def test_create
sDescription = 'Rent'
fAmount = 10000

obExpenseEntry =
obExpenseEntry.description = sDescription
obExpenseEntry.amount = fAmount
assert, obExpenseEntry.errors.full_messages.join("; ")

assert_equal 1, Expense.count

assert_not_nil obExpenseEntry.created_at
assert_equal sDescription, obExpenseEntry.description
assert_equal fAmount, obExpenseEntry.amount

We create a model class Expense

class Expense < ActiveRecord::Base; end

We also add a empty fixtures yml file (\test\fixtures\expenses.yml) That makes it pass. Let’s add test_retrieve
def test_retrieve
obExpense = getFirstEntry
assert_kind_of Expense, obExpense
assert_equal expenses(:rent_expense_entry).id,
assert_equal expenses(:rent_expense_entry).description, obExpense.description
assert_equal expenses(:rent_expense_entry).amount, obExpense.amount
assert_equal expenses(:rent_expense_entry).created_at, obExpense.created_at
def getFirstEntry

Add records to the fixtures file
id: 1
created_at: 2008-01-19 00:00:00
description: rent
amount: 10000
id: 2
created_at: 2008-02-20 00:00:00
description: A pair of Jeans
amount: 1000.50
I have to update the previous test to expect 3 records after adding a new one. That passes both tests.

Now to add the controller tests. This time we’ll not use the auto-generated scaffolding – we’ll build it one action at a time

>Ruby script/generate controller Outflow new
Next on the list test_new. Make it pass. Lets see how it looks in a browser. Whaa... dummy page?! I'll write up the view using the InflowController's view as reference. But we'll use a table so that the label and its associated field are on the same line. We'll add a note to do the same for the inflow view.
Since we'll need our ResourceManager to lookup strings, we move that to application_helper.rb
Also we need a shorter way to say ApplicationHelper::ResourceManager.getString( :stringID )... something like rails h().. I'll settle for getAppString(:stringID)

Iteration2 Day 2/10
Next we write tests for the list, index(redirects to list) actions so that we can see the records once we add them. Make em pass.
Next we attempt test_create. Make that pass too.
Now we can try out the webpage. We don’t have the styles applied yet… other than that looks just fine. I guess we can that fix that easy. If I rename the controller specific layout ‘inflow.rhtml’ to ‘application.rhtml’, Rails is smart enough to apply it to all controllers. And it works just like that!!
I also make a small change to make the second image-link direct the user to outflow/list action… now that we have it implemented.

Next lets move on to validating the add actions. So we need tests for
  • No description
  • No amount
  • No description and amount (should be covered by above two)
  • Amount not numeric
  • Amount negative or 0
To the model !!
Before that.. a tiny lil refactoring. Hmm I see a recurring ‘obExpense = getFirstEntry’ at the top of more than one test. Time for a setup method and a new instance var in the test class @firstExpense = getFirstEntry. Now I can do away with those duplicate lines …

Ok Model tests done. On to the controller.
Done.. with some refactoring.
Added @fixtureRecordCount to controller and unit test classes to remove magic numbers in the tests

Finally Delete to wrap up this story. Model… test_destroy just passes - coz ATCO by Rails.

Write up a controller test for delete. There.. All Green. That’s just sometime over End of Day2 – Nice work. Lets try out our outflow controller…
First thing we have no entry point for delete. We need to update the outflow/list view to add links for delete. And it works.

Lets take a break!

No comments:

Post a Comment