Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unknown Method key? error in Rails 2.3.8 Unit testing

I was writing unit tests for my models for a while. After that I was tweaking around and again continued writing unit tests.

Earlier all my unit tests were working - successfully. But now WHen I run them, it gives me

Loaded suite unit/post_test
Started
EEEE
Finished in 0.112698 seconds.

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


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


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


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

And my test cases are

class PostTest < ActiveSupport::TestCase


  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
like image 584
Anand Avatar asked May 21 '26 13:05

Anand


1 Answers

If it was working and now it is not, I would say that you might have introduced some kind of error in your post model. It is difficult to say without a backtrace and code from your post model, but if you were to run the same code in your console, do you get the same error?

post = Post.new(:body=>"Some content")
post.save

From there you should get a backtrace and a place to hunt the error down.

EDIT:

Next I would try running rake with the --trace flag to get a backtrace.

The other thought I had was that it might be something monkey patching assert. I don't see anything about test unit changing assert method to take a hash as the second argument.

first, go into your console and type, assert false, "error message" and you should get a no method error. If you get another response, then you need to find where the assert method might be, otherwise - try with this:

require 'test/unit'
include Test::Unit::Assertions
assert false, "error message"

You should see:

Test::Unit::AssertionFailedError: this is a message.
<false> is not true.

Otherwise, I would also try removing the message at the end of your assert and see if you get the same error.

like image 179
Geoff Lanotte Avatar answered May 23 '26 04:05

Geoff Lanotte