Chapter 4 def test_getting_details

2 views
Skip to first unread message

akkdio

unread,
Sep 28, 2008, 10:31:36 PM9/28/08
to Beginning Ruby on Rails E-Commerce
Thanks for the great book.

I have been moving through the chapters and reading the forum for
answers and the errata - however this test is stumping me.

The message I get in the error is similar to a previous post titled
"confusing error":

The error is: Called id for nil, which would mistakenly be 4 -- if you
really wanted the id of nil, use object_id

Jarkko replied that this was bc the object had not been assigned. In
my case that would mean what exactly?

As a note all my other tests run fine up to the point I put in this
the get call show below:

def get_book_details_for(title)
@book = Book.find_by_title(title)
get "/catalog/show/#{@book.id}"
end

I don't bother with the rest of the test (I increment when I have
issues) as this line causes it to give me the error.

Anyone have an idea where to look?

Also I coded the controller and the view and in browser tests of the
book links (example catalog/show/22) everything works fine. The
check_book_links test pass without error.

Thanks for the help,

Andrew



Jarkko Laine

unread,
Sep 29, 2008, 1:45:59 AM9/29/08
to railsec...@googlegroups.com

On 29.9.2008, at 5.31, akkdio wrote:

>
> Thanks for the great book.
>
> I have been moving through the chapters and reading the forum for
> answers and the errata - however this test is stumping me.
>
> The message I get in the error is similar to a previous post titled
> "confusing error":
>
> The error is: Called id for nil, which would mistakenly be 4 -- if you
> really wanted the id of nil, use object_id
>
>
> Jarkko replied that this was bc the object had not been assigned. In
> my case that would mean what exactly?

It means the object for which you call id (maybe implicitly) is nil.
E.g.

@book = Book.find_by_id(params[:id])
@book.id

If Book.find_by_id returns nil, @book will be nil, and the second line
would raise the exception above. That's why you should generally
always make sure that a finder really returned something.

>
>
> As a note all my other tests run fine up to the point I put in this
> the get call show below:
>
> def get_book_details_for(title)
> @book = Book.find_by_title(title)
> get "/catalog/show/#{@book.id}"
> end
>
> I don't bother with the rest of the test (I increment when I have
> issues) as this line causes it to give me the error.
>
> Anyone have an idea where to look?

Please send the whole error stack, containing the line number raising
the exception. If it's the code you pasted above, my explanation above
should explain the problem fairly well.

//jarkko

--
Jarkko Laine
http://jlaine.net
http://dotherightthing.com
http://www.railsecommerce.com
http://odesign.fi


akkdio

unread,
Sep 30, 2008, 1:28:31 AM9/30/08
to Beginning Ruby on Rails E-Commerce
Thanks. The error is message is below:


1) Error:
test_getting_details(BrowsingAndSearchingTest):
RuntimeError: Called id for nil, which would mistakenly be 4 -- if you
really wanted the id of nil, use object_id
test/integration/browsing_and_searching_test.rb:55:in
`get_book_details_for'
test/integration/browsing_and_searching_test.rb:21:in
`test_getting_details'
/Library/Ruby/Gems/1.8/gems/actionpack-2.0.2/lib/action_controller/
integration.rb:547:in `run'

3 tests, 7 assertions, 0 failures, 1 errors
andrew-gellenes-macbook:emporium akkdio$


Its pretty much the same code in the test thats on page 120 (the
#{@book.id}) did not show up in my previous post... looks like it got
escaped (it may escape again).

the two lines are the jill.get_book.details_for "Pride and
Prejudice" (line 21)

and

get "/catalog/show/#{@book.id}" line 55

the object I am calling should be the id of the book called by the
title right? I am assuming this because line 20 sets the instance
variable @book equal to the book title or name and the @book.id
represents that books id. to be shown in the the show action.

I know I mentioned this before but... all the other tests
(check_book_links) work. So the show action is working to display the
book details and passing a book.id from the show action. It just
does not pass this test.









The controller has the same code as page 121.



On Sep 29, 1:45 am, Jarkko Laine <jar...@jlaine.net> wrote:
> On 29.9.2008, at 5.31, akkdio wrote:
>
>
>
> > Thanks for the great book.
>
> > I have been moving through the chapters and reading the forum for
> > answers and the errata - however this test is stumping me.
>
> > The message I get in the error is similar to a previous post titled
> > "confusing error":
>
> > The error is: Called id for nil, which would mistakenly be 4 -- if you
> > really wanted the id of nil, use object_id
>
> > Jarkko replied that this was bc the object had not been assigned. In
> > my case that would mean what exactly?
>
> It means the object for which you call id (maybe implicitly) is nil.
> E.g.
>
> @book = Book.find_by_id(params[:id])
> @book.id
>
> If Book.find_by_id returns nil, @book will be nil, and the second line
> would raise the exception above. That's why you should generally
> always make sure that a finder really returned something.
>
>
>
> > As a note all my other tests run fine up to the point I put in this
> > the get call show below:
>
> > def get_book_details_for(title)
> > @book = Book.find_by_title(title)
> > get "/catalog/show...@book.id}"

Jarkko Laine

unread,
Sep 30, 2008, 2:03:08 AM9/30/08
to railsec...@googlegroups.com

Right, so @book is nil. I don't have the code or the book at hand
right now, but find the line where @book is set (in the test, the
error stack shows that the exception comes from the test, not the
actual app). It's probably something like @book =
Book.find_by_title(title), which means that you don't have a book with
the correct title in the fixtures/db.

akkdio

unread,
Oct 1, 2008, 7:24:27 PM10/1/08
to Beginning Ruby on Rails E-Commerce
Thanks. That was is exactly. I did not have Jane's book loaded in
fixtures... all is well.

thanks for helping.

On Sep 30, 2:03 am, Jarkko Laine <jar...@jlaine.net> wrote:
> On 30.9.2008, at 8.28, akkdio wrote:
>
>
>
>
>
> > Thanks. The error is message is below:
>
> > 1) Error:
> > test_getting_details(BrowsingAndSearchingTest):
> > RuntimeError: Called id for nil, which would mistakenly be 4 -- if you
> > really wanted the id of nil, use object_id
> > test/integration/browsing_and_searching_test.rb:55:in
> > `get_book_details_for'
> > test/integration/browsing_and_searching_test.rb:21:in
> > `test_getting_details'
> > /Library/Ruby/Gems/1.8/gems/actionpack-2.0.2/lib/action_controller/
> > integration.rb:547:in `run'
>
> > 3 tests, 7 assertions, 0 failures, 1 errors
> > andrew-gellenes-macbook:emporium akkdio$
>
> > Its pretty much the same code in the test thats on page 120 (the
> > #...@book.id}) did not show up in my previous post... looks like it got
> > escaped (it may escape again).
>
> > the two lines are the jill.get_book.details_for "Pride and
> > Prejudice" (line 21)
>
> > and
>
> > get "/catalog/show...@book.id}" line 55
>
> > the object I am calling should be the id of the book called by the
> > title right? I am assuming this because line 20 sets the instance
> > variable @book equal to the book title or name and the @book.id
> > represents that books id. to be shown in the the show action.
>
> > I know I mentioned this before but... all the other tests
> > (check_book_links) work. So the show action is working to display the
> > book details and passing a book.id from the show action. It just
> > does not pass this test.
>
> Right, so @book is nil. I don't have the code or the book at hand
> right now, but find the line where @book is set (in the test, the
> error stack shows that the exception comes from the test, not the
> actual app). It's probably something like @book =
> Book.find_by_title(title), which means that you don't have a book with
> the correct title in the fixtures/db.
>
> //jarkko
>
> --
Reply all
Reply to author
Forward
0 new messages