Spec Your Widgets with rspec-apotomo!

{{{
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.
}}}

Advertisement

6 thoughts on “Spec Your Widgets with rspec-apotomo!

  1. Hey Nick! Thanks for your gems, but you gotta get your email verified by PayPal before we can donate. I tried to send you some bux but it failed, saying your email has not been verified by PP. Rock on!

    Like

  2. Thanks for the update Nick! There might be something simple that I’m missing, but is there a way to attach event data to the #trigger? For example, I’m trying to test a form POST event that’s accompanied by an evt hash – without the hash the widget response doesn’t do much. Thanks!

    Like

  3. Nick, can we use something like Mocha to test a widget fires a trigger? I would like to do something like this:
    root.find_widget(‘me’).expects(:trigger).with(:update_client).once

    Of course, it would be great to have an #assert_trigger method. But I think the Mocha approach is better and more powerful.

    I would love to test the widget in isolation, instead of testing that another widget responds to the event. For that we have integration tests. πŸ˜‰

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s