我正在爲我公司編寫的軟件測試框架。我們的產品是基於Web的,在我運行RESTful請求後,我想處理結果。我希望能夠在每個命令類中進行activerecord類型驗證,以便在運行後自動針對所有「驗證」對結果進行測試。但是,我不知道如何做到這一點。我的代碼看起來像這樣(簡化以顯示重要部分)。如何在activerecord之外創建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的驗證。任何人都可以告訴我如何做到這一點?由於
感謝您回答我正在尋找的答謝。一個問題,它需要ActiveSupport?謝謝。 – 2009-04-23 04:57:25
它使用`Hash#blank?`(在`valid?`方法中)。但是就是這樣,嘿。不應該太難以放棄active_support。我只是假定它已經在那裏了,因爲你無論如何都處在rails的環境中。 – 2009-04-23 07:44:02