2008-11-10 22 views
3

在週末花了幾個小時後,我終於得到了RSpec的懸念。現在,我試圖弄清楚如何斷言參數確實傳遞到控制器。我正在關注Bowled over by Ruby/Cocoa example並將其改編爲iPhone SDK。我已經做了更詳細的writeup of my progress on my blog,所以我會推遲整個故事。總之,我一直遵循教程,直到需要將文本字段中的引腳值傳遞到Bowling對象。 RSpec不斷抱怨,「OSX :: BowlingController中的Spec :: Mocks :: MockExpectationError應該將PIN值發送給保齡球對象' Mock'Bowling'expected:roll with(10),but received with(no args) ./test/bowling_controller_spec.rb:38:」即使我敢肯定,我傳遞一個值這裏是我的代碼誰能告訴我,我要去哪裏錯了使用RSpec for iPhone控制器

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() { } 

Bowling.h

#import <Foundation/Foundation.h> 

@interface Bowling : NSObject { 

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

Bowling.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() { } 

回答

3

RubyCocoa根本不支持iPhone。沒有橋樑支持庫,我不相信電話上有任何Ruby解釋器。

您可能可以在模擬器中使用它,但如果您真的嘗試使用它,它不會阻止您使用僅限OS X的庫,但仍然無法使其在iPhone上工作。

如果你真的想在iPhone上使用RubyCocoa,你需要將ruby作爲一個靜態庫來構建,並將橋接到手機,這是可行的,但可能會非常困難。

0

你好!儘管我不太瞭解Ruby/Cocoa如何封裝外部方法調用 - 或者Objective C與此相關,但在測試中可能斷開的第一個地方是將Ruby模擬傳遞給本地實現的控制器。在保齡球教程中,ruby控制器代理將其接口暴露給Cocoa橋,而在此實現中,代理包裝一個暴露的Cocoa接口。可能有一個問題,那就是,當用ruby模擬替代本地域,而用紅寶石模擬替代ruby域時。

雖然引腳的roll()測試成功,所以有可能消息正確傳遞,但參數被損壞或丟失。

這可能沒有多大幫助,但這是一個有趣的問題。項目祝你好運!