我正在試驗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
我同意,這對我測試我的靜態類非常有效。 http://code.google.com/p/google-toolbox-for-mac/wiki/iPhoneUnitTesting – 2009-12-02 04:27:06