This project is archived and is in readonly mode.

#5116 ✓invalid
lakshmanan

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

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

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...

Comments and changes to this ticket

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

<h2 style="font-size: 14px">Tickets have moved to Github</h2>

The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>

People watching this ticket

Pages