Tony Byrne asks is Exator style acceptance testing useful for Ruby apps?
My short answer is – yes – from the successes I’ve seen using Exactor in enabling ‘business’ people to contribute directly to the Testing/Test Driven Development – simply by letting them work in an arena comfortable to them – eg text files/excel – not some complicated development environment. Maybe the value of getting ‘business’ people more involved in development alone can justify this approach?.
My long answer is – it depends. My personal preference – with Ruby is to stick with ruby for all my kinds of testing. But perhaps you are in a scenario where you have existing acceptance tests in Exactor for a Java project you want to port to Ruby?
Anyways, here’s a quick spike at implementing Exactor style testing in Ruby,
require 'hpricot'
require 'open-uri'
class Command
attr_accessor :context
def initialize(context)
self.context = context
end
end
class OpenWebPage < Command
def run(url)
context[:page] = Hpricot(open(url))
end
end
class CheckTitle < Command
def run(expected_title)
raise "OOOOps" unless expected_title.eql?((context[:page]/"title").inner_html)
end
end
test = File.read("test.txt")
context = {}
test.split("\n").each do |command_line|
parts = command_line.split(" ")
command = eval(parts.slice!(0)).send(:new, context)
args = (parts.size == 1) ? parts[0] : parts
command.run(args)
end
where test.txt contains
OpenWebPage http://www.google.com/ CheckTitle Google
Tony Byrne 25 Jun 12:47
It's amazing just how far down the road you can get with so little code in Ruby. I particularly like your use of 'eval' in instantiating the commands. I ended up jumping some hoops to do what you have managed to do with a single line. My code is somewhat more verbose, partially because I'm still learning Ruby and its idoms, but also because I want to support 'cool' features such as custom test runners, etc. However, I can improve the internals of my code just by digesting the details in your spike. As regards my motivations for implementing an Exactor style framework, for me it's all about learning Ruby through writing a contained project. I have no actual need for this framework, since this is my *only* Ruby project to date. However, your point about porting existing tests is interesting. With a cross platform Exactor, one could use Ruby for rapid prototyping of projects that, for whatever reason, need to go into production in Java or .NET. The tests would be portable between platforms. In theory, you could do this now with FIT, but I've always preferred the elegant simplicty of Exactor. Thanks for the feedback, Darragh!