针对iPhone Native应用程序的测试驱动设计 [英] Test Driven Design for iPhone Native apps
问题描述
我正在试验iPhone SDK并做一些TDD ala Dr. Nic的rbiPhoneTest项目。我想知道有多少(如果有的话)成功使用这个或任何其他测试框架的iPhone / Cocoa?更重要的是,我想知道如何最好地断言专有的二进制请求/响应协议。想法是通过网络发送二进制请求并接收二进制响应。使用byte和'ing和oring创建请求和响应。我正在使用黄金副本模式来测试我的请求。这是我到目前为止所拥有的。不要笑,因为我是btoh Objective C和Ruby的新手:
I'm experimenting with the iPhone SDK and doing some TDD ala Dr. Nic's rbiPhoneTest project. I'm wondering how many, if any, have been successful using this or any other testing framework for iPhone/Cocoa? More important, I'd like to know how to best assert a proprietary binary request/response protocol. The idea is to send a binary request over the network and receive a binary response. Requests and responses are created using byte and'ing and or'ing. I'm using the golden copy pattern to test my request. Here's what I have so far. Don't laugh as I'm new to btoh Objective C and Ruby:
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
推荐答案
我对Ruby或二进制协议知之甚少,但如果您对iPhone上的单元测试感兴趣,可能需要查看 Google Toolbox for Mac 。我用它测试我的OpenGL ES应用程序非常成功。
I don’t know much about Ruby or binary protocols, but if You’re interested in unit testing on iPhone, You might want to check out the Google Toolbox for Mac. I am having great success testing my OpenGL ES application with it.
这篇关于针对iPhone Native应用程序的测试驱动设计的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!