I am working on a testing framework for the software that my company writes. Our product is web based and After I run a RESTful request I want to process the results. I want to be able to have activerecord type validations in each command class so that after it is run the results are automatically tested against all the ‘validations’. However, I am not sure how to do this. My code looks like this (simplified to show the important parts).
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
What I want is that after the execute method is called the test_success and identify_error are automatically called like the validations in activerecord. Can anybody tell me how to do this? Thanks
Without having looked much at your code, here’s my take on implementing validation class methods:
It assumes that ActiveSupport is around, which it is in a Rails environment. You might want to extend it to allow multiple errors per attribute, with
instance.errors[attribute] << 'the message', but I left out obscurities like that in order to keep this short sample as simple as possible.Here’s a short usage example: