Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

Rails Unit Testing Fails with NoMethodError: undefined method `key?' for #<String:0x1034f5098>

#5116

In Rails 2.3.8, When I write a test like

require 'test_helper'
require 'test/unit'
include Test::Unit::Assertions


class PostTest < ActiveSupport::TestCase

  def test_find_the_bug
    assert true
  end
  
  def test_presence_of_title
    post = Post.new(:body=>"Some content")
    assert !post.save,"Saved post without title"
  end
  
  
  def test_presence_of_body
    post = Post.new(:title=>"Some title")
    assert !post.save,"saved post without body"
  end
  
  
  def test_presence_of_body_and_title
    post = Post.new(:title=>"Some title",:body=>"")
    assert !post.save,"Saved Post without body"
    
    post = Post.new(:title => "",:body=>"Some body")
    assert !post.save,"Saved Post without title"
    
    post = Post.new(:title =>"",:body=>"")
    assert !post.save,"Saved Post with title and body"
    
    
  end
  
  
  def test_title_minimum_width_3
    post1 = Post.new(:title=>"a",:body=>"This will not be saved")
    assert !post1.save,"Saved post with title length less than 3"
    
    post2 = Post.new(:title=>"abcd",:body=>"This will be saved")
    assert post2.save,"Couldnot save a valid post record"
  
    post3 = Post.new(:title=>"abc",:body=>"This will be saved")
    assert post3.save,"Could not save a valid record"
  end
end

and run the file from test directory, here is what I get.. is this a bug

Loaded suite unit/post_test
Started
test_find_the_bug(PostTest): E
test_presence_of_body(PostTest): E
test_presence_of_body_and_title(PostTest): E
test_presence_of_title(PostTest): E
test_title_minimum_width_3(PostTest): E

Finished in 0.17441 seconds.

  1) Error:
test_find_the_bug(PostTest):
NoMethodError: undefined method `key?' for #<String:0x1034f5098>
    

  2) Error:
test_presence_of_body(PostTest):
NoMethodError: undefined method `key?' for #<String:0x1034b8a58>
    

  3) Error:
test_presence_of_body_and_title(PostTest):
NoMethodError: undefined method `key?' for #<String:0x10348adb0>
    

  4) Error:
test_presence_of_title(PostTest):
NoMethodError: undefined method `key?' for #<String:0x10345d108>
    

  5) Error:
test_title_minimum_width_3(PostTest):
NoMethodError: undefined method `key?' for #<String:0x10342f460>
    

5 tests, 0 assertions, 0 failures, 5 errors

Here is the discussion thread associated with it

http://stackoverflow.com/questions/3236972/unknown-method-key-error...

Reported by lakshmanan · July 15th, 2010 @ 06:51 AM

State: invalid
Milestone: none
Assigned to: nobody
Importance: Low

Activity

  1. lakshmanan
    lakshmanan

    Sorry. the mistake is in my part and not on rails...

    July 15th, 2010 @ 07:44 AM

  2. Neeraj Singh
    Neeraj Singh
    • State changed from new to invalid
    • Importance changed from to Low

    July 15th, 2010 @ 09:30 AM