代码之家  ›  专栏  ›  技术社区  ›  Cliff

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

  •  16
  • Cliff  · 技术社区  · 17 年前

    我正在试验iPhone SDK,并做一些TDD-ala博士的rbiPhoneTest项目。我想知道有多少人(如果有的话)成功地使用了这个或任何其他iPhone/Cocoa测试框架?更重要的是,我想知道如何最好地维护专有的二进制请求/响应协议。其思想是通过网络发送二进制请求并接收二进制响应。请求和响应是使用字节和“ing”和“or”ing创建的。我正在使用黄金复制模式来测试我的请求。以下是我到目前为止的情况。不要笑,因为我是btoh 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
    
    2 回复  |  直到 17 年前
        1
  •  11
  •   zoul    17 年前

    我对Ruby或二进制协议了解不多,但如果你对iPhone上的单元测试感兴趣,你可能想看看 Google Toolbox for Mac .我用它测试我的OpenGLES应用程序非常成功。

        2
  •  6
  •   Dr Nic    17 年前

    克里夫,从长远来看,你最好把时间花在纯ObjC TDD工具上。我已经在fmdb migration manager中成功地使用了我自己的rbiphonetest库,但其用途可能仅限于库等。即使如此,毫无疑问,仍有足够多的“在Cocoa中工作,但在UIKit中失败”场景使rbiphonetest难以使用。我希望有一天RubyCocoa能够在Intel UIKit库的基础上构建,然后我认为它将非常有用和坚固。