Skip to content

RSpec, Apotomo, and Devise

adamstrickland edited this page Mar 27, 2013 · 4 revisions

Y U NO LIKE MY TESTING?!!

Right, so we happen to be using Apotomo widgets in an application using Devise that we're testing with RSpec. Further, the data that our Apotomo widgets display is user-dependent, driving off of current_user, which is supplied by Devise by including Devise::Controllers::Helpers in our Widgets. Testing them proved to be a bit of a bear; in the end, we used an RSpec shared_context to simplify writing our tests, so in case you're using a similar setup (or a similar, derivative case), so for your testing pleasure, please enjoy this gist. Cheers!

Usage

Create a shared context from the gist. I usually put these in spec/support/shared_contexts, but that's up to you.

Create your spec.

require "spec_helper"

describe FooWidget do
  include_context "widget"
end

In your spec, you can have Apotomo render your widget by using render_widget(widget_name). Similarly, you can use a before block to do some additional set-up, a la

let(:name) { "Ford Prefect" }
before {
  current_user.stub(:name).and_return(name)
}
subject { render_widget(widget_name) }
context "should display my" do
  it { should have_content(name) }
end

Other Variables

In case you can't read ruby source code (in which case, why are you here???):

  • widget_klass - the widget's class, pulled from the top-line describe
  • widget_name - the key that Apotomo uses to find the widget; defaults to the underscored class name, less '_widget'
  • widget_instance - the actual instance of the widget
  • current_user - duh