我正在为我的模型编写单元测试一段时间。之后我一直在调整并再次继续编写单元测试。
早些时候我所有的单元测试都成功了。但现在我运行它们,它给了我
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>
我的测试用例是
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
答案 0 :(得分:1)
如果它正在工作但现在不行,我会说你可能在你的帖子模型中引入了某种错误。没有回溯和你的帖子模型中的代码很难说,但如果你在控制台中运行相同的代码,你会得到同样的错误吗?
post = Post.new(:body=>"Some content")
post.save
从那里你应该得到一个回溯和一个地方来追捕错误。
编辑:
接下来,我会尝试使用--trace
标志运行rake来获得回溯。
另一个想法是,它可能是猴子修补断言的东西。我没有看到任何关于测试单元改变断言方法以将哈希作为第二个参数。
首先,进入你的控制台并键入assert false, "error message"
,你应该得到一个no方法错误。如果你得到另一个响应,那么你需要找到断言方法的位置,否则 - 试试这个:
require 'test/unit'
include Test::Unit::Assertions
assert false, "error message"
您应该看到:
Test::Unit::AssertionFailedError: this is a message.
<false> is not true.
否则,我也会尝试在断言结束时删除该消息,看看是否收到同样的错误。