Solved

first time debugging

Posted on 2008-10-17
25
441 Views
Last Modified: 2013-11-13
While debugging, I got an error and a failure.


def method_missing(selector, *args)
  return @controller.send!(selector, *args) if ActionController::Routing::Routes.named_routes.helpers.include?(selector)
  return super
end

Error:
test_should_create_movie(MoviesControllerTest):
NameError: undefined local variable or method `title' for #<MoviesControllerTest:0x2cc5e64>
    c:/ruby/lib/ruby/gems/1.8/gems/actionpack-2.1.1/lib/action_controller/test_process.rb:467:in `method_missing'
    ./test/functional/movies_controller_test.rb:17:in `test_should_create_movie'
    c:/ruby/lib/ruby/gems/1.8/gems/activesupport-2.1.1/lib/active_support/core_ext/test/unit/assertions.rb:41:in `assert_difference'
    ./test/functional/movies_controller_test.rb:16:in `test_should_create_movie'
    c:/ruby/lib/ruby/gems/1.8/gems/activesupport-2.1.1/lib/active_support/testing/setup_and_teardown.rb:33:in `__send__'
    c:/ruby/lib/ruby/gems/1.8/gems/activesupport-2.1.1/lib/active_support/testing/setup_and_teardown.rb:33:in `run'


def test_should_update_movie
  put :update, :id => movies(:one).id, :movie => { }
  assert_redirected_to movie_path(assigns(:movie))
end

Failure:
test_should_update_movie(MoviesControllerTest)
    [./test/functional/movies_controller_test.rb:35:in `test_should_update_movie'
     c:/ruby/lib/ruby/gems/1.8/gems/activesupport-2.1.1/lib/active_support/testing/setup_and_teardown.rb:33:in `__send__'
     c:/ruby/lib/ruby/gems/1.8/gems/activesupport-2.1.1/lib/active_support/testing/setup_and_teardown.rb:33:in `run'
Expected response to be a <:redirect>, but was <200>


This is complicated!!! What is going on???
0
Comment
Question by:locke1994
  • 16
  • 9
25 Comments
 
LVL 14

Expert Comment

by:wesgarrison
ID: 22749212
Key lines for the first error:

NameError: undefined local variable or method `title' for #<MoviesControllerTest:0x2cc5e64>
 ./test/functional/movies_controller_test.rb:17:in `test_should_create_movie'
 
 Looks like your test is calling @movie.title, but you don't have a title field in your database.  Have you run migrations or added the field in your migration?


For the second error:
test_should_update_movie(MoviesControllerTest)
    [./test/functional/movies_controller_test.rb:35:in `test_should_update_movie'
Expected response to be a <:redirect>, but was <200>
Line 35 is:
put :update, :id => movies(:one).id, :movie => { }
This is saying that your update action didn't redirect.  It gave a "200" status, which is a normal OK status.  My guess is that either:
  • You have some validation that failed because you passed an empty :movie, so the error was rendered instead of a page redirect.
  • Or, you set it to just say "Record updated" instead of redirecting.
Just fire up your application and try updating a record and see what happens, then you can change the test or the application to do the Right Thing.
0
 

Author Comment

by:locke1994
ID: 22750347
The error stopped happening, but the failure still happens. The debug output is:

uninitialized constant ApplicationController
      C:/www/movielib/app/controllers/movies_controller.rb:1
C:/www/movielib/app/controllers/movies_controller.rb:1: uninitialized constant ApplicationController (NameError)
0
 

Author Comment

by:locke1994
ID: 22750380
I am following the instructions of the Komodo IDE Ruby on Rails tutorial. Starting from "If we rerun the test, we're now down to one failure.", I am having problems. (I can not stop having the failure.) There is a copy of the tutorial on the internet. Please, take a look.

http://docs.activestate.com/komodo/4.4/tutorial/railstut.html
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 14

Expert Comment

by:wesgarrison
ID: 22752908
Did you remove the application.rb file from your controllers directory?
0
 

Author Comment

by:locke1994
ID: 22753282
No, I did not. Do I have to remove? Why did you ask that???
0
 

Author Comment

by:locke1994
ID: 22761735
flash[:notice] = 'Movie was successfully updated.'
format.html { redirect_to(@movie) }
format.xml  { head :ok }

The action has to redirect (to @movie)...
0
 

Author Comment

by:locke1994
ID: 22761841
I had to turn...

post :create, :movie => { }

...into...

post :create, :movie => { :title => "movie 3" }


Maybe, I have to turn...

put :update, :id => movies(:one).id, :movie => { }

...into...

put :update, :id => movies(:one).id, :movie => { :title => "movie 3" }


I am trying! Are you trying?
0
 
LVL 14

Expert Comment

by:wesgarrison
ID: 22762401
Did you remove the application.rb file from your controllers directory?

No, I did not. Do I have to remove? Why did you ask that???
You're getting an error that says it can't find it:

uninitialized constant ApplicationController
      C:/www/movielib/app/controllers/movies_controller.rb:1
Are you still getting that error, or are you still getting the original error?  Give me a quick refresh on where you're at.

But, yes, if the movie model has a required title field, then you'd need to add that title to the update test as well or it'll fail.  The model won't get saved (because of the validation) and so the page won't be redirected.
0
 

Author Comment

by:locke1994
ID: 22763013
I am not getting:

uninitialized constant ApplicationController
     C:/www/movielib/app/controllers/movies_controller.rb:1


I am getting:

Failure:
test_should_update_movie(MoviesControllerTest)
    [./test/functional/movies_controller_test.rb:35:in `test_should_update_movie'
     c:/ruby/lib/ruby/gems/1.8/gems/activesupport-2.1.1/lib/active_support/testing/setup_and_teardown.rb:33:in `__send__'
     c:/ruby/lib/ruby/gems/1.8/gems/activesupport-2.1.1/lib/active_support/testing/setup_and_teardown.rb:33:in `run'
Expected response to be a <:redirect>, but was <200>


I do not know what is going on...
0
 
LVL 14

Accepted Solution

by:
wesgarrison earned 250 total points
ID: 22763056
In your tutorial, it says that you added these lines:

  validates_presence_of :title
   validates_uniqueness_of :title
Did you?  If so, then you need to pass a title:

put :update, :id => movies(:one).id, :movie => { :title => "movie 3" }

.... would work (as long as you don't have a movie with that title in the movies.yml fixture file!)
0
 

Author Comment

by:locke1994
ID: 22763137
I turned:

put :update, :id => movies(:one).id, :movie => { }

into

put :update, :id => movies(:one).id, :movie => { :title => "movie 4" }

then I got

Error:
test_should_update_movie(MoviesControllerTest):
NameError: undefined local variable or method `title' for #<MoviesControllerTest:0x3733ce8>
    c:/ruby/lib/ruby/gems/1.8/gems/actionpack-2.1.1/lib/action_controller/test_process.rb:467:in `method_missing'
    ./test/functional/movies_controller_test.rb:34:in `test_should_update_movie'
    c:/ruby/lib/ruby/gems/1.8/gems/activesupport-2.1.1/lib/active_support/testing/setup_and_teardown.rb:33:in `__send__'
    c:/ruby/lib/ruby/gems/1.8/gems/activesupport-2.1.1/lib/active_support/testing/setup_and_teardown.rb:33:in `run'
0
 

Author Comment

by:locke1994
ID: 22763149
Hold on, wesgarrison, since we are updating a movie, should we pass the new, the old or both titles of the movie?
0
 
LVL 14

Expert Comment

by:wesgarrison
ID: 22763187
You're passing the id, so that's how it's looking up the old/existing record.  You need to send the new value for the title field.

Can you paste in your MovieController::update action and the entire failing test "test_should_update_movie"?
0
 

Author Comment

by:locke1994
ID: 22763346
I am guessing that you are asking for:

# PUT /movies/1
# PUT /movies/1.xml
def update
  @movie = Movie.find(params[:id])

  respond_to do |format|
    if @movie.update_attributes(params[:movie])
      flash[:notice] = 'Movie was successfully updated.'
      format.html { redirect_to(@movie) }
      format.xml  { head :ok }
    else
      format.html { render :action => "edit" }
      format.xml  { render :xml => @movie.errors, :status => :unprocessable_entity }
    end
  end
end

and for:

def test_should_update_movie
  put :update, :id => movies(:one).id, :movie => { title => "movie 4" }
  assert_redirected_to movie_path(assigns(:movie))
end
0
 

Author Comment

by:locke1994
ID: 22763397
The failure is supposed to happen!!!

"If we rerun the test, we're now down to one failure. Click on the red failure button to move to the failed test, then double-click on the line starting with test/functional/movies_controller_test.rb:35:in `test_should_update_movie' in the Details window to see the failed code.

  def test_should_update_movie
    put :update, :id => movies(:one).id, :movie => { }
    assert_redirected_to movie_path(assigns(:movie))
  end
 

It's not obvious why this test is failing. Rails tests are just Ruby code, so let's use the debugger (Komodo IDE only) to have a closer look, with the following steps:

    * Set a breakpoint at line 62 of movies_controller.rb, in the update routine.
    * Bring up movies_controller_test.rb
    * Start the debugger, with the Debug|Go/Continue menu item
    * In the Debugging Options box, make sure the Directory field consists of the top-level directory of your project (the directory containing the movielib.kpf file, not the test file).
    * Press OK."


When I press OK, the debug output is:

"no such file to load -- test_helper
      c:/ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:in `gem_original_require'
      c:/ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:in `require'
      C:/www/movielib/test/functional/movies_controller_test.rb:1
c:/ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:in `gem_original_require': no such file to load -- test_helper (LoadError)
      from c:/ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:27:in `require'
      from C:/www/movielib/test/functional/movies_controller_test.rb:1"
0
 

Author Comment

by:locke1994
ID: 22763403
Do not give up! We are going to solve it!!!
0
 

Author Comment

by:locke1994
ID: 22763411
How do I know if the debugger is stopping at a line? The debug output is like I said...
0
 

Author Comment

by:locke1994
ID: 22763424
IT WORKED!!!

I had written:"title", not ":title" (it was missing an ":")!!!
0
 
LVL 14

Expert Comment

by:wesgarrison
ID: 22763422
You've got a typo in your test:

put :update, :id => movies(:one).id, :movie => { title => "movie 4" }

"title" should be a Symbol (beginning with ":") or it'll look for a variable called 'title'.  So, that's why we were getting an 'undefined local variable "title"' error!

You pasted it in correctly above, but apparently your code doesn't exactly match, so make title into :title and try it out.
0
 

Author Comment

by:locke1994
ID: 22763428
We typed the solution at the same minute!!! lol
0
 

Author Closing Comment

by:locke1994
ID: 31507365
It is better to try movie 4, because movie 3 is tried in test_should_create_movie
0
 
LVL 14

Expert Comment

by:wesgarrison
ID: 22763453
Ha!  Yeah, the exact same minute.

Glad you got it working.  I've never used Komodo (or any debugger, actually...)
0
 
LVL 14

Expert Comment

by:wesgarrison
ID: 22763471
And just so you know, if you created a "Movie 3" in your create test, you could update a movie to the same title in a different test.  The database gets restored between tests, so changes from test don't carry over to another test.
0
 

Author Comment

by:locke1994
ID: 22763473
wesgarrison... just a thought... it looks like the Komodo debugger does not work... it does not stop at line 62... I do not see anything different, anything special happening... what is a breakpoint supposed to do???
0
 
LVL 14

Expert Comment

by:wesgarrison
ID: 22764352
It's supposed to suspend the execution of the program at that point. The idea is that you can look at the program's state at that instant in time.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Ruby on Rails/Postgres --- Looks to be a query issue? 7 714
Create an application - what tools to use? 4 615
=> syntax in ruby 1 346
return files 6 111
I recently rediscovered rails when I needed a holiday project and decided to build a management dashboard for the company where I work.  With it being a project done in my free time, I could focus my time on learning the basics rather than trying to…
Recently I spent hours debugging an issue in a Rails project where ActiveRecord was causing MySQL errors trying to create a User object of a class at the top level of a Single Table Inheritance model structure.  It turns out `.create` behaves differ…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

679 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question