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.
!|fit.ActionFixture|
|start|AcceptanceTests.TrackBalance|
|enter|description|PayDay|
|enter|amount|5000|
|press|add_credit_entry|
|check|currentBalance|5000|
|enter|description|Interest|
|enter|amount|1000.50|
|press|add_credit_entry|
|check|currentBalance|6000.50|
|enter|description|PayDay|
|enter|amount|5000|
|press|add_credit_entry|
|check|currentBalance|11000.50|
|enter|select_entry|3|
|press|delete_credit_entry|
|check|currentBalance|6000.50|
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
...
|enter|description|Rent|
|enter|amount|2550.00|
|press|add_expense_entry|
|check|currentBalance|3450.50|
|enter|description|Apparel|
|enter|amount|450.00|
|press|add_expense_entry|
|check|currentBalance|3000.50|
|enter|description|Apparel|
|enter|amount|500.00|
|press|add_expense_entry|
|check|currentBalance|2500.50|
|enter|select_entry|2|
|press|delete_expense_entry|
|check|currentBalance|2950.50|

!|AcceptanceTests.GetOutflowRecords|
|description|amount|
|Rent|2550.00|
|Apparel|500.00|
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 = Expense.new
obExpenseEntry.description = sDescription
obExpenseEntry.amount = fAmount
assert obExpenseEntry.save, obExpenseEntry.errors.full_messages.join("; ")

assert_equal 1, Expense.count
obExpenseEntry.reload

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


We create a model class Expense
file:\app\models\expense.rb

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, obExpense.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
end
def getFirstEntry
Expense.find(1)
end


Add records to the fixtures file
rent_expense_entry:
id: 1
created_at: 2008-01-19 00:00:00
description: rent
amount: 10000
apparel_expense_entry:
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