callback for UIViewController test

59 views
Skip to first unread message

Katsuyoshi Ito

unread,
Jun 13, 2013, 8:06:40 PM6/13/13
to rubym...@googlegroups.com
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

Colin Thomas Arnold Gray

unread,
Jun 13, 2013, 8:15:00 PM6/13/13
to rubym...@googlegroups.com
When I have another second, I'd like to add the ability to pass a block to either `tests` or `after_created:` in lieu of a method name.

#colinta
> --
>
> ---
> You received this message because you are subscribed to the Google Groups "RubyMotion - Ruby for iOS" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to rubymotion+...@googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>

Katsuyoshi Ito

unread,
Jun 14, 2013, 2:29:34 AM6/14/13
to rubym...@googlegroups.com
I's a nice idea.

Pass a block to `tests` is simple I think.
I don't have to care about a name of the option.
Reply all
Reply to author
Forward
0 new messages