Rspec Cells

Spec your Cells.

This plugin allows you to test your cells easily using RSpec. Basically, it adds a cells example group with a #render_cell helper.

Cells is Rails' popular view components framework.

Installation

This gem runs with RSpec2 and Rails >= 3.x, so just add it to your app's Gemfile.

group :development, :test do
  gem "rspec-cells"
end

Usage

Simply put all your specs in the spec/cells directory or add type: :cell to the describe block. However, let the cell generator do that for you!

rails g cell blog_post show -t rspec

will create an exemplary spec/cells/blog_post_cell_spec.rb for you.

API

Old API

In your specs you can use render_cell to assert the rendered markup (or whatever your state is supposed to do). This goes fine with Webrat matchers.

it "renders posts count" do
  expect(render_cell(:posts, :count)).to have_selector("p", :content => "4 posts!")
end

You can create a cell instance using the cell method, and then render afterwards. This is helpful when you're planning to stub things or if you need to pass arguments to the cell constructor.

it "renders empty posts list" do
  posts = cell(:posts)
  posts.stub(:recent_posts).and_return([])

  expect(posts.render_state(:count)).to have_selector("p", :content => "No posts!")
end

After preparing the instance you can use render_state for triggering the state.

View Models

With the introduction of cells [view models](github.com/apotonick/cells#view-models) you get a slightly different API for your specs (which is identical to your app's one).

it "renders empty posts list" do
  expect(cell(:posts).count.to be_zero
end

It's pretty simple, you use `#cell` to instantiate a view model instance and then call the state method on it.

Capybara

If you want Capybara's string matchers be sure to bundle at least capybara 0.4.1 in your Gemfile.

group :development, :test do
  gem "capybara", "~> 0.4.1"
end

In order to make the cells test generator work properly, capybara needs to be in both groups.

You can then use capybara's matchers in your cell spec.

describe PostsCell do
  describe "search posts" do
    let(:search) { render_cell(:posts, :search) }

    it "should have a search field" do
      expect(search).to have_field("Search by Title")
    end

    it "should have a search button" do
      expect(search).to have_button("Search")
    end
  end

  describe "latest posts" do
    subject { render_cell(:posts, :latest) }

    it { is_expected.to have_css("h3.title", :text => "Latest Posts") }
    it { is_expected.to have_table("latest_posts") }
    it { is_expected.to have_link("View all Posts") }
    it { is_expected.to_not have_button("Create Post") }
    it { is_expected.to_not have_field("Search by Title") }
  end
end

You can see all capybara matchers and finders here.

Running the specs

Run your examples with

rake spec:cells

If you need more helpers, matchers and stuff, just let us know.

Test cells with caching

By default your code for caching code is not run if you set ActionController::Base.perform_caching = false That's a reasonable default but you might want to increase coverage by running caching code at least once. Here is an example:

describe SomeCell do
  describe 'caching' do
    enable_cell_caching!
    # Code for testing...
  end
end

Contributors

  • Jorge Calás Lozano <calas@qvitta.net> (Cleanup, capybara string matchers)

  • Abdelkader Boudih <@seuros>

*

LICENSE

Copyright © 2010, Nick Sutterer

Copyright © 2008-2009, Dmytro Shteflyuk <kpumuk@kpumuk.info> kpumuk.info

Released under the MIT License.