johnreilly

www.flickr.com
john.reilly's items Go to john.reilly's photostream

Posts tagged "error"

new to obj-c

I’m working my way through the Pragmatic iPhone screencasts, and my complete newbosity to Objective-C is thoroughly kicking my ass.

Basically, there’s a main application delegate that has a property on it for data shared amongst other controllers. The controllers “should” be able to access it… as far as I can tell I’ve set up all my connections and @synthesize’ed it correctly, and the property appears in Xcode’s autocomplete. But, the compiler? Not so happy.

Objective-C error

Whenever I try to access the delegate’s property (in this case, the property is called “recipes”) in a controller, Xcode spits out an error:

Request for member ‘recipes’ in something not a structure or union

To me, this seems like an old school C pointer de-referencing problem, which I thought Obj-C’s @property and @synthesize bits would have done away with.

I’m at a loss. Any suggestions?

Update

Turns out I wasn’t including the header files for the app delegate class. So, the particular implementation I was working in had no idea what properties/methods were available on the app delegate.

If you look at the screenshot above, I simply added the following line just below the first #import statement:

#import "RecipesAppDelegate.h"

In hindsight, this was a rather silly mistake on my part (but hopefully forgivable since this is my first day with Obj-C…). But what strikes me as weird is that Bill (the screencast’s author) never added the header file during the screencast.

Was it somehow added automagically in his project when connecting up the delegate outlet in Interface Builder? Who knows. At least I’ve got it running now… On to the next section. :)

Comments (View)

rspec is mocking me

Grr. I hate situations where it works here, but doesn’t work there, and I can’t see where the difference is. Something has to be different, I just can’t for the life of me figure out what. Maybe someone can help.

Here’s what’s up.

I’ve got rspec and rspec-rails installed as git submodules. Running the latest edge rails. I’m gonna generate up a quick rspec_scaffold:

$ script/generate rspec_scaffold Bike name:string sku:string

This generates the following controller test (abridged):


describe BikesController do

  def mock_bike(stubs={})
    @mock_bike ||= mock_model(Bike, stubs)
  end
  
  describe "responding to GET index" do

    it "should expose all bikes as @bikes" do
      Bike.should_receive(:find).with(:all).and_return([mock_bike])
      get :index
      assigns[:bikes].should == [mock_bike]
    end

  end
end

Looks fine. BUT. In my project, this spec fails, because it is somehow loading up the view and the name and sku calls are tripping up the mock, as it isn’t expecting them to be called in this controller test.

Note, however, that I am NOT calling integrate_views inside my controller spec, so I don’t have any idea why the view code is being run.

% rake spec:controllers
(snip)
ActionView::TemplateError in 'BikesController responding to GET index should expose all bikes as @bikes'
Mock 'Bike_1017' received unexpected message :name with (no args)
On line #11 of app/views/index.html.erb

8: 
9: <% for bike in @bikes %>
10:   <tr>
11:     <td><%=h bike.name %></td>
12:     <td><%=h bike.model %></td>
13:     <td><%= link_to 'Show', bike %></td>
14:     <td><%= link_to 'Edit', edit_bike_path(bike) %></td>

app/views/bikes/index.html.erb:11
app/views/bikes/index.html.erb:9:in `each'
app/views/bikes/index.html.erb:9

This happens for 10 of the 18 scaffolded specs.

I can fix the errors by adding expectations to the mock like so:


it "should expose all bikes as @bikes" do
  Bike.should_receive(:find).with(:all).and_return([mock_bike])
  mock_bike.should_receive(:name)      
  mock_bike.should_receive(:sku)
     
  get :index
  assigns[:bikes].should == [mock_bike]
end

But that kind of defeats the purpose of having a controller-only test, as it’s now testing the view code as well.

What’s weird is, I started a brand new project, and all the tests passed with flying colors!

% rails rspectest
% cd rspectest 
% rake db:create:all
% script/plugin install git://github.com/dchelimsky/rspec.git
% script/plugin install git://github.com/dchelimsky/rspec-rails.git
% script/generate rspec
% script/generate rspec_scaffold Bike name:string sku:string  
% rake db:migrate
% rake spec
(in /Users/john/src/test/rspectest)
.....................................

Finished in 1.006109 seconds

37 examples, 0 failures

So a brand new project runs the rspec_scaffold specs in isolation, but my project somehow doesn’t.

I’m tearing my hair out on this one. I can’t figure out what I’ve done to my project to cause these specs to stop working.

Anyone out there have any ideas/suggestions?

Update!

Wow. Through the magic of git bisect, I’ve determined that the problem occurs because of a change in edge rails. Turns out, this commit causes the specs to fail. Everything before it passes, everything after fails.

I have no idea how to fix this (or if it is rails’ or rspec’s fault), but I’m getting closer. :-)

PS, if you’ve never used git bisect before, you really haven’t lived.

Update 2!

The latest commits to rspec and rspec-rails fix the problem. Bravo rspec team.

Comments (View)

sqlite3-ruby-1.2.2 and rails == no good

Tonight I fired up a new rails app to play with… did my initial git stuff, ran script/console for the first time, fired up localhost:3000, etc etc.

Everything was going so smoothly.  Until I hit the “About your app’s environment” link on the default index page.  Observe the pinkish horror:

Here’s the relevant error text (for google to index):

MissingSourceFile in Rails/infoController#properties

no such file to load -- sqlite3/database
RAILS_ROOT: /Users/john/src/trms/cam

This error occurred while loading the following files:
   sqlite3
   sqlite3/database

Nasty.

Turns out, this isn’t a huge deal to fix.  Just prior to railsing up my new app, I had updated all of my installed gems, which included the latest sqlite3-ruby gem (ver 1.2.2). After doing a bit of research, I discovered 1.2.2 doesn’t play nicely with rails.

Oookay fine, downgrading is easy:

$ sudo gem uninstall sqlite3-ruby -v=1.2.2
Password:
Successfully uninstalled sqlite3-ruby-1.2.2

This put me back to 1.2.1. Running script/console once again proved the workingness had returned.

Bottom line, make sure your latest and greatest sqlite3-ruby gem is no higher than version 1.2.1. I’m not sure what the problem is with 1.2.2, but this worked for me, hopefully it helps someone else as well.

Update Aug 28 2008

Word on the street is that the recently released sqlite-ruby-1.2.3 fixes this issue. Haven’t had a chance to try it myself, but people report success.

Comments (View)

strange activerecord error

So I’m using the ActiveRecord gem (outside of rails) to translate some data from our old forum database to YAML, so it can be imported into a different system.  But I’m getting an error on one of my models that I just can’t figure out.  Here are the details:

class Topic < ActiveRecord::Base
  set_table_name  "forum_topics"
  set_primary_key "TOPIC_ID"
  
  has_many    :replies, :foreign_key => "TOPIC_ID"
  belongs_to  :forum,   :foreign_key => "FORUM_ID"
  belongs_to  :member,  :foreign_key => "T_AUTHOR"
end

class Reply < ActiveRecord::Base
  set_table_name  "forum_replies"
  set_primary_key "REPLY_ID"
  
  belongs_to :member, :foreign_key => "R_AUTHOR"
  belongs_to :topic,  :foreign_key => "TOPIC_ID"
  belongs_to :forum,  :foreign_key => "FORUM_ID"
end

So, we’ve got two models, Topic and Reply (well, there are more, but they aren’t interesting to the discussion here). The associations are as you’d expect, and I’m telling AR about the legacy DB’s schema.

Everything you’d expect to work with Topics does indeed work:

>> Topic.count 
=> 828
>> Topic.first.T_SUBJECT
=> "Rave Review"

But Replies are screwy for some reason. The associations all work, but trying to get a field’s value barfs out. Example:

>> Reply.count 
=> 2278
>> Reply.first.topic.T_SUBJECT 
=> "Rave Review"
>> Reply.first.R_DATE
ArgumentError: wrong number of arguments (1 for 0)
    from (irb):374:in `method_missing`
    from (irb):374
    from :0
>> Reply.first['R_DATE']
=> "20041206085839"

I’m at a loss. Why can’t I get values from Replies? Topics work fine. For the moment, I’m using the Reply.first['R_DATE'] syntax, but I’d rather not.  Anyone out there have any ideas?

Comments (View)