views:

571

answers:

2

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() { }
+3  A: 

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.

Louis Gerbarg
A: 

Howdy! Though I'm not familiar with how Ruby/Cocoa wraps foreign method calls- or with Objective C for that matter- the first place that would seem likely to disconnect under test is passing in a Ruby mock to the natively implemented controller. In the bowling tutorial, the ruby controller proxy is exposes its interface to the Cocoa bridge while in this implementation the proxy wraps an exposed Cocoa interface. There might be an issue, then, when substituting a ruby mock for a native field versus a ruby mock for a ruby field.

The roll() test for the pins succeeds, though, so it's possible that messages are being passed correctly but arguments are mangled or dropped.

This probably isn't much help, but it's an interesting problem. Good luck with the project!