This is in answer to a question than came up in a course I am teaching. There do not appear to be any good examples of testing navigation bar button items, so I ran up this quick example. Two things of note:
-
You need to explicitly call your controller's "on_load" or similar method that actually adds the UI items, in your before block if you actually want to test those items. In the case of the default Red Potion app, we create the nav bar buttons in
HomeScreen.on_load
, so we need to call that method in the before block of our spec, to ensure the nav buttons are present. -
The navigation bar is accessed from your
UIViewController
asnavigationItem
, and it is aUINavigationItem
. You should check out the docs for these classes.
https://developer.apple.com/documentation/uikit/uiviewcontroller?language=objc https://developer.apple.com/documentation/uikit/uinavigationitem?language=objc