【发布时间】:2009-04-22 06:51:00
【问题描述】:
我正在为我公司编写的软件开发一个测试框架。我们的产品是基于网络的,在我运行 RESTful 请求后,我想处理结果。我希望能够在每个命令类中进行 activerecord 类型验证,以便在运行后自动针对所有“验证”测试结果。但是,我不知道该怎么做。我的代码看起来像这样(简化以显示重要部分)。
class CodesecureCommand
def execute
result = RestClient.post("http://#{codesecure.host_name_port}#{path}", post_data)
return parse(result) #parse simple returns a Hpricot document
end
end
class RunScan < CodesecureCommand
#What I have now
#I have to override the execute function so that it calls the local success method
#to see if it failed or not.
def execute()
result = super()
if success(result)
return true
else
end
end
def success(result)
result.search('div.transaction-message') do |message|
if message.innerHTML.scan(/Configure abuse setting for domain users successfully\./).length == 1
return true
end
end
end
#What I would like is to be able to call execute (without having to override it).
#then after it runs it calls back to this class to check
#if the regex matches the command was successful and returns true
test_success /regex/
#if test_success fails then these are called
#the idea being that I can use the regex to identify errors that happened then
#report them to the user
identify_error /regex/, "message"
identify_error /regex/, "message"
end
end
我想要的是,在调用执行方法之后,会自动调用 test_success 和 identify_error,就像在 activerecord 中的验证一样。谁能告诉我该怎么做?谢谢
【问题讨论】:
标签: ruby-on-rails ruby activerecord