ruby - 将 RSpec 用于 iPhone Controller

标签 ruby iphone rspec ruby-cocoa

在周末花了几个小时后,我终于掌握了 RSpec 的窍门。现在我一直在试图弄清楚如何断言参数确实已传递到 Controller 中。我正在关注 Bowled over by Ruby/Cocoa example并针对 iPhone SDK 进行调整。我做了更详细的 writeup of my progress on my blog所以我会推迟整个故事。简而言之,我一直按照教程进行操作,直到您需要将 pin 值从文本字段传递到 Bowling 对象。 RSpec 一直提示说,“‘OSX::BowlingController 中的 Spec::Mocks::MockExpectationError 应该将 pin 值发送到保龄球对象’ 模拟“保龄球”预期 :roll 与 (10) 但收到它与(无参数) ./test/bowling_controller_spec.rb:38:” 即使我确定我正在传递一个值。这是我的代码。谁能告诉我哪里出错了?

bowling_controller_spec.rb

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

require "BowlingController.bundle"
OSX::ns_import :BowlingController

include OSX

describe BowlingController do
  before(:each) do
    @controller = BowlingController.new  
    @bowling = mock('Bowling')
    @text_field = mock('Pins')
    @text_field.stub!(:intValue).and_return(10)
    @controller.pins = @text_field
  end

  it "should roll a ball" do
    @controller.roll
  end

  it "should roll a ball and get the value from the pins outlet" do
    @text_field.should_receive(:intValue).and_return(0)
    @controller.roll
  end

  it "should be an OSX::NSObject" do
    @controller.is_a?(OSX::NSObject).should == true
  end

  it "should have an outlet to a bowling object" do
    @controller.bowling = @bowling
  end

  it "should send the pin value to the bowling object" do
    @controller.bowling = @bowling
    @bowling.should_receive(:roll).with(10)

    @controller.roll
  end
end

BowlingController.h

#import <Foundation/Foundation.h>

@class UITextField;
@class Bowling;

@interface BowlingController : NSObject {
    UITextField* pins;
    Bowling* bowling;
}
@property (nonatomic, retain) UITextField* pins;
@property (nonatomic, retain) Bowling* bowling;

-(void) roll;
@end

BowlingController.m

#import "BowlingController.h"
#import "Bowling.h"


@implementation BowlingController
@synthesize pins;
@synthesize bowling;

-(void) roll{
    [self.bowling roll:[self.pins intValue]];
}

@end

// This initialization function gets called when we import the Ruby module.
// It doesn't need to do anything because the RubyCocoa bridge will do
// all the initialization work.
// The rbiphonetest test framework automatically generates bundles for 
// each objective-c class containing the following line. These
// can be used by your tests.
void Init_BowlingController() { }

保龄球.h

#import <Foundation/Foundation.h>

@interface Bowling : NSObject {

}
- (void) roll:(int) pins;
@end

保龄球.m

#import "Bowling.h"


@implementation Bowling
- (void) roll:(int) pins{
}

@end

// This initialization function gets called when we import the Ruby module.
// It doesn't need to do anything because the RubyCocoa bridge will do
// all the initialization work.
// The rbiphonetest test framework automatically generates bundles for 
// each objective-c class containing the following line. These
// can be used by your tests.
void Init_Bowling() { }

最佳答案

RubyCocoa 在 iPhone 上完全不受支持。没有bridge支持库,我不相信手机上有什么ruby解释器。

您可能能够让它在模拟器中运行,如果您真的尝试它不会阻止您使用仅 OS X 的库,但这仍然不会使其在 iPhone 上运行。

如果你真的想在 iPhone 上使用 RubyCocoa,你需要将 ruby​​ 构建为静态库并将桥接器移植到手机上,这是可行的,但可能会非常困难。

关于ruby - 将 RSpec 用于 iPhone Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/278331/

相关文章:

ruby - 无法安装 mysql gem/ruby-dev

iphone - 如何以编程方式添加按钮以删除 WebView?

rspec - 有没有办法让 Capybara 在浏览器中进行实时测试?

ruby-on-rails - 使用 rr 产生一个 block

ruby-on-rails - Rails 更改区域设置返回当前页面

ruby - 如何处理 ruby​​ 中的 JSON 解析器错误

iphone - 如何使用 ios sdk 从地址簿创建 vCard?

ruby-on-rails - RSpec with Factory_girl - 销毁对象

ruby-on-rails - 如何区分大写和非大写单词?

iPhone 内存管理