FrankでRubyからiOSアプリを操作してテストする

昨日の話の続きです。


こんな感じでテストを書くことができます。

ENV['RAILS_ENV'] = 'development'

require File.expand_path('../../server/config/environment', __FILE__)
require 'rails/test_help'

require 'frank-cucumber/frank_helper'

class FranklyTest < ActiveSupport::TestCase
  include Frank::Cucumber::FrankHelper
  self.use_transactional_fixtures = false
  
  setup do
    wait_for_frank_to_come_up
    Counter.destroy_all
  end

  test "Tap incr button increment" do
    assert_equal 0, Counter.get.count
    
    touch("button marked:'Reload'")
    assert_equal ["0"], frankly_map("label marked:'Counter'", 'text')
    
    touch("button marked:'Incr'")
    assert_equal ["1"], frankly_map("label marked:'Counter'", 'text')
    
    touch("button marked:'Incr'")
    assert_equal ["2"], frankly_map("label marked:'Counter'", 'text')
    
    assert_equal 2, Counter.get.reload.count
  end

  test "Reload load counter from server" do
    assert_equal 0, Counter.get.count
    touch("button marked:'Reload'")
    assert_equal ["0"], frankly_map("label marked:'Counter'", 'text')
    
    Counter.get.incr!
    Counter.get.incr!
    Counter.get.incr!
    
    assert_equal 3, Counter.get.count
    touch("button marked:'Reload'")
    assert_equal ["3"], frankly_map("label marked:'Counter'", 'text')
  end

  test "Reset button reset counter" do
    Counter.get.incr!
    Counter.get.incr!
    Counter.get.incr!
    assert_equal 3, Counter.get.count
    
    touch("button marked:'Reset'")
    assert_equal ["0"], frankly_map("label marked:'Counter'", 'text')
    assert_equal 0, Counter.get.count
  end
end

実行するとこんな感じになります。

$ ruby -I server/test -I ../Frank/gem/lib/ test/test.rb
Run options: 

# Running tests:

...

Finished tests in 2.717210s, 0.3680 tests/s, 1.4721 assertions/s.

3 tests, 12 assertions, 0 failures, 0 errors, 0 skips

いつものmini-testのテストですね。


強調したいことは次の2つです。

  • 全部Ruby
  • Railsの操作とiOSアプリの操作が両方記述されている

全部Ruby

Objective-Cでテストを書く必要はありません。

touchflankly_mapは、iOSアプリの操作です。touchとするとボタンがタップされて、flanky_mapでビューのプロパティを取得します。

あとは、assert_equalなりで、テストすればそれでok。

Railsの操作とiOSアプリの操作が両方記述されている

上で説明したように、touchflankly_mapiOSアプリの操作ですが、その他はRailsのモデルの操作です。

development環境でRailsアプリをロードして、通常通りのモデルの操作を行います*1。サーバの環境のリセットをRailsで行い(Counter.destroy_all)、iOSアプリの操作を行います。iOSアプリの操作の結果が正しいことを、iOSアプリ内部と、Railsサーバの内部状態の両方でテストしています。

これ、すごい楽ですね。


続きます。せっかくなのでFrankを使い始めるまでのセットアップについて説明しましょう。

*1:development環境を使用しているのは手抜き