Solved

first time debugging

Posted on 2008-10-17
25
436 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
 
LVL 14

Expert Comment

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

Author Comment

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

Author Comment

by:locke1994
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Hold on, wesgarrison, since we are updating a movie, should we pass the new, the old or both titles of the movie?
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 14

Expert Comment

by:wesgarrison
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Do not give up! We are going to solve it!!!
0
 

Author Comment

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

Author Comment

by:locke1994
Comment Utility
IT WORKED!!!

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

Expert Comment

by:wesgarrison
Comment Utility
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
Comment Utility
We typed the solution at the same minute!!! lol
0
 

Author Closing Comment

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

Expert Comment

by:wesgarrison
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Routing in Rails 3 3 678
Rails 3.0 Routing 3 611
Grails dropdown results from <g:select> 4 1,643
What does  =~ mean in Ruby 1 469
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…
Article by: narshlob
If you've ever programmed in Ruby and have come across either a proc or a lambda, you might have been wondering what the difference is between the two and when you would use one over the other. This article will try to explain the difference between…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

762 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now