gpt4 book ai didi

ruby-on-rails - 未知的方法 key ? Rails 2.3.8单元测试中的错误

转载 作者:行者123 更新时间:2023-12-04 03:38:18 25 4
gpt4 key购买 nike

我有一段时间为模型编写单元测试。之后,我进行了调整,然后再次继续编写单元测试。

较早之前,我所有的单元测试都已成功进行。但是现在我运行它们,它给了我

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

最佳答案

如果它正在运行,但现在却不行,我想您可能在您的帖子模型中引入了某种错误。没有post模型中的回溯和代码很难说,但是如果您要在控制台中运行相同的代码,是否会得到相同的错误?

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


从那里,您应该获得回溯和寻找错误的地方。

编辑:

接下来,我将尝试使用 --trace标志运行rake以获取回溯。

我的另一个想法是,这可能是猴子修补的断言。我没有看到任何关于测试单元更改assert方法以将哈希作为第二个参数的信息。

首先,进入您的控制台并键入 assert false, "error message",您应该会得到一个no method错误。如果收到其他响应,则需要查找assert方法可能在的位置,否则-尝试以下操作:

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


您应该看到:

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


否则,我还将尝试删除断言末尾的消息,看看是否遇到相同的错误。

关于ruby-on-rails - 未知的方法 key ? Rails 2.3.8单元测试中的错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3236972/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com