iphone - iPhone 原生应用的测试驱动设计

标签 iphone objective-c ruby tdd

我正在试验 iPhone SDK 并在 Nic 博士的 rbiPhoneTest 项目中做一些 TDD。我想知道有多少人(如果有的话)成功地使用了这个或任何其他 iPhone/Cocoa 测试框架?更重要的是,我想知道如何最好地断言专有的二进制请求/响应协议(protocol)。这个想法是通过网络发送二进制请求并接收二进制响应。请求和响应是使用 byte and'ing 和 or'ing 创建的。我正在使用黄金副本模式来测试我的请求。这是我到目前为止所拥有的。不要笑,因为我是 Objective C 和 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 或二进制协议(protocol),但如果您对 iPhone 上的单元测试感兴趣,您可能想查看 Google Toolbox for Mac .我用它测试我的 OpenGL ES 应用程序非常成功。

关于iphone - iPhone 原生应用的测试驱动设计,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/195820/

相关文章:

iPhone 应用程序试用期

ruby - Byebug 终端输出不一致且有错误

ruby - 如何在 Windows 7 上创建全局 rakefile?

iphone - 如何在 objective-c 中进行惰性实例化?

iphone - 使用 url ios 6 播放视频

ios - iOS是否延迟peripheralManager :didReceiveWriteRequests: and peripheralManager:didReceiveReadRequest:?

Objective-C – 用一个空格替换换行序列

iphone - 对象-C : Creating an object with a String name

ios - 如何在同一框架内访问 Objective-C 中的内部 Swift 类?

ruby - 无法从 Jekyll 插件获取页面数据