2008-10-12 39 views
16

我正在試驗iPhone SDK並做一些TDD ala Dr. Nic的rbiPhoneTest項目。我想知道有多少人(如果有的話)成功使用iPhone/Cocoa的這個或任何其他測試框架?更重要的是,我想知道如何最好地維護專有的二進制請求/響應協議。這個想法是通過網絡發送二進制請求並接收二進制響應。請求和響應是使用字節和'和'來創建的。我正在使用金色複製模式來測試我的請求。這是迄今爲止我所擁有的。別笑,我是新來的BTOH目標C和Ruby:針對iPhone的測試驅動設計原生應用

require File.dirname(__FILE__) + '/test_helper' 
require 'fileutils' 
require 'io' 

require "MyModel.bundle" 
OSX::ns_import :MyModel 

module MyTestExtensions 
    def is_absolute_path(path) 
    return /^\/.*/.match(path) 
    end 

    def parent_directory(file) 
    dir = file 
    if(! is_absolute_path(dir)) 
     dir = File.expand_path(dir) 
    end 
    dir = File.dirname(dir) 
    assert is_absolute_path(dir), "Expecting an absolute path with #{dir}" 
    return dir 
    end 

    def assert_NSData_contains_bytes_from_file(file, data) 
    assert_not_nil data, "Data should not be nil." 
    assert data.bytes, "data should have bytes" 
    data.length.times { |i| 
     expected = file.getc 
     assert_not_nil expected, "Expected only #{i} bytes. Actual data contains more." 
     actual = data.bytes.int8_at(i) 
     assert_equal expected, actual, "Bytes should be equal at offset #{i} expected #{expected.chr} but was #{actual.chr}" 
    } 
    expected = file.getc 
    raise AssertionFailedError, "Expecting #{expected.chr} at offset #{data.length}" unless expected == nil 
    end 

end 

class TestMyModel < Test::Unit::TestCase 
include OSX 
include MyTestExtensions 

    def this_files_dir 
    return parent_directory(__FILE__) 
    end 

    def setup 
    @expectedReq = File.new("#{this_files_dir}/ExpectedMyReq") 
    # @expectedReq = File.new("#{this_files_dir}/hello.txt") 
    assert File.exist?("#{this_files_dir}/ExpectedMyReq"), "The file [#{@expectedReq.path}] should exist." 
    end 

    def test_my_model_class_exists 
    MyModel 
    end 

    def test_can_init_instance 
    assert MyModel.instancesRespondToSelector(:init), "MyModel Should define :init" 
    end 

    def test_my_model_can_request_my_data 
    myModel = MyModel.alloc.init 
    data = myModel.requestMyData 'Some query text' 
    assert_NSData_contains_bytes_from_file @expectedReq, data 
    end 

end 

回答

11

我不知道很多關於Ruby或二進制協議,但如果你有興趣在iPhone上的單元測試,你可能想檢查出Google Toolbox for Mac。我用它測試了我的OpenGL ES應用程序,取得了巨大的成功。

+0

我同意,這對我測試我的靜態類非常有效。 http://code.google.com/p/google-toolbox-for-mac/wiki/iPhoneUnitTesting – 2009-12-02 04:27:06

6

懸崖,長期來說,你最好在純粹的ObjC TDD工具上投入時間。我已經成功地在fmdb-migration-manager中使用了我自己的rbiphonetest庫,但它的用處可能僅限於庫等。即使這樣,在Cocoa中工作無疑已經足夠了,但在UIKit的場景下卻無法使rbiphonetest可疑地使用。希望有一天RubyCocoa可以針對英特爾UIKit庫進行構建,然後它會非常有用和堅固,我認爲。