Open source is a great thing. I love the fact that you can simply ignore “feature requests by users”:http://groups.google.com/group/cells-and-apotomo/browse_thread/thread/6f50cb2ea5c62ff4 and anybody will give you a hard time about it – hey, *it’s _just_ open source*. Even better, people will start implementing things on their own if you make them wait long enough.
This is what happened with “Christian Höltje”:https://github.com/docwhat and “Jake Goulding”:https://github.com/shepmaster who initiated a first version of “rspec-apotomo”:https://github.com/apotonick/rspec-apotomo. Thank you, guys!
It’s now possible to *test your Apotomo widgets in isolation using RSpec*. Yay! Let’s see how that works.
h3. Using the Gem
Bundler is your friend, just add this one-liner to your Gemfile.
group :test do gem 'rspec-apotomo' end
Whoops, this wasn’t a one-liner.
h3. I’m your Generator!
To create a sample spec, just run the generator shipped with the gem.
$ rails g rspec:widget comments create spec/widgets/comments_widget_spec.rb
h3. Spec it!
Even the spec is pretty self-explanatory.
describe CommentsWidget do has_widgets do |root| root << widget(:comments) end it "should render :display" do render_widget(:comments).should == "No Comments!" end end
All we have to do is setting up a widget tree with @#has_widgets@, just as know from controllers. The @#render_widget@ method does exactly the same as in your application views – it renders and returns the markup. Easy.
h3. Testing Events
Now, “Apotomo”:http://apotomo.de would be boring without its interactivity.
describe CommentsWidget do # .. it "should respond to :post events" do trigger(:post, :comments).should == ["Thanks!"] end end
@#trigger@ first fires the event (first argument) from the source (second argument). It then returns _an array_ of all return values from widgets responding to that event.
We’re now waiting for proposals how to simplify testing multiple responses – maybe some comfy matcher? I’m excited.
Let us know what you think of this new gem.
BTW- Cells and Apotomo is a lot of work, if you like my gems “consider donating”:http://pledgie.com/campaigns/16068.