Hi all
I introduce a new tests option :after_created for testing UIViewController.
Colin merged my pull request. Thank you Colin.
You will use it next version.
https://github.com/HipByte/RubyMotion/pull/96
There is a MyFriendsTabelViewController.
It has friends attribute which contains names of my friends.
class MyFriendsTableViewController < UITableViewController
attr_accessor :friends # Array
def tableView tableView, numberOfRowsInSection:section
friends.size
end
end
And test code is like this.
describe MyFriendsTableViewController do
tests MyFriendsTableViewController
before do
controller.friends = ["Amuro Ray", "Char Aznable", "Matilda Ajan"]
end
it "should have 3 rows" do
controller.tableView(controller.tableView, numberOfRowsInSection:0).should == 3
end
end
tests method generates MyFriendsTableViewController object.
In before block, set friends. But this time controller already set as a main view controller of window.
tableView:numberOfRowsInSection: was called before execute before block.
Then you got an error.
- should have 3 rows2013-06-14 08:20:03.879 my_friends[2502:c07] *** Terminating app due to uncaught exception 'NoMethodError', reason: 'my_friends_table_view_controller.rb:in `tableView:numberOfRowsInSection:': undefined method `size' for nil:NilClass (NoMethodError)
It's solved using :after_created option.
Specify :after_created with method you want to callback after controller was created.
describe MyFriendsTableViewController do
tests MyFriendsTableViewController, after_created: :after_created_controller
def after_created_controller controller
controller.friends = ["Amuro Ray", "Char Aznable", "Matilda Ajan"]
end
it "should have 3 rows" do
controller.tableView(controller.tableView, numberOfRowsInSection:0).should == 3
end
end
Then pass test.
MyFriendsTableViewController
- should have 3 rows
The whole source code is here.
https://github.com/katsuyoshi/sample_of_after_created
----------------------
Katusyoshi Ito
ki...@itosoft.com