Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using RSpec for iPhone Controllers

I'm finally getting the hang of RSpec after spending a couple of hours over the weekend. Now I'm stuck trying to figure out how to assert that parameters are indeed passed into the controller. I'm following the Bowled over by Ruby/Cocoa example and adapting it for the iPhone SDK. I've done a more detailed writeup of my progress on my blog so I'll defer there for the entire story. In short I've followed the tutorial all the way up to where you need to pass the pin value from the text field into the Bowling object. RSpec keeps complaining that, "Spec::Mocks::MockExpectationError in ‘OSX::BowlingController should send the pin value to the bowling object’ Mock ‘Bowling’ expected :roll with (10) but received it with (no args) ./test/bowling_controller_spec.rb:38:” Even as I'm certain that I'm passing a value in. Here's my code. Can someone tell me where I'm going wrong?

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() { }
like image 596
Cliff Avatar asked Jul 11 '26 13:07

Cliff


1 Answers

RubyCocoa is not supported at all on the iPhone. There is no bridge support library, and I do not believe there is any ruby interpreter on the phone.

You might be able to get it working in the simulator, it will not stop you from using OS X only libraries if you really try, but that still will not make it work on the iPhone.

If you really want to use RubyCocoa on the iPhone you will need to build ruby as a static library and port the bridge to the phone, which is doable, but would probably be very difficult.

like image 194
Louis Gerbarg Avatar answered Jul 14 '26 03:07

Louis Gerbarg



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!